Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add devcontainer #3

Merged
merged 2 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions .devcontainer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Developing with Visual Studio Code + devcontainer

The easiest way to get started with custom integration development is to use Visual Studio Code with devcontainers. This approach will create a preconfigured development environment with all the tools you need.

In the container you will have a dedicated Home Assistant core instance running with your custom component code. You can configure this instance by updating the `./devcontainer/configuration.yaml` file.

**Prerequisites**

- [git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
- Docker
- For Linux, macOS, or Windows 10 Pro/Enterprise/Education use the [current release version of Docker](https://docs.docker.com/install/)
- Windows 10 Home requires [WSL 2](https://docs.microsoft.com/windows/wsl/wsl2-install) and the current Edge version of Docker Desktop (see instructions [here](https://docs.docker.com/docker-for-windows/wsl-tech-preview/)). This can also be used for Windows Pro/Enterprise/Education.
- [Visual Studio code](https://code.visualstudio.com/)
- [Remote - Containers (VSC Extension)][extension-link]

[More info about requirements and devcontainer in general](https://code.visualstudio.com/docs/remote/containers#_getting-started)

[extension-link]: https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers

**Getting started:**

1. Fork the repository.
2. Clone the repository to your computer.
3. Open the repository using Visual Studio code.

When you open this repository with Visual Studio code you are asked to "Reopen in Container", this will start the build of the container.

_If you don't see this notification, open the command palette and select `Remote-Containers: Reopen Folder in Container`._

### Tasks

The devcontainer comes with some useful tasks to help you with development, you can start these tasks by opening the command palette and select `Tasks: Run Task` then select the task you want to run.

When a task is currently running (like `Run Home Assistant on port 9123` for the docs), it can be restarted by opening the command palette and selecting `Tasks: Restart Running Task`, then select the task you want to restart.

The available tasks are:

| Task | Description |
| ------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------- |
| Run Home Assistant on port 9123 | Launch Home Assistant with your custom component code and the configuration defined in `.devcontainer/configuration.yaml`. |
| Run Home Assistant configuration against /config | Check the configuration. |
| Upgrade Home Assistant to latest dev | Upgrade the Home Assistant core version in the container to the latest version of the `dev` branch. |
| Install a specific version of Home Assistant | Install a specific version of Home Assistant core in the container. |
| Serve Documentation on port 8000 | Serve MKDocs on port 8000 to review your changes |

### Step by Step debugging

With the development container,
you can test your custom component in Home Assistant with step by step debugging.

You need to modify the `configuration.yaml` file in `.devcontainer` folder
by uncommenting the line:

```yaml
# debugpy:
```

Then launch the task `Run Home Assistant on port 9123`, and launch the debugger
with the existing debugging configuration `Python: Attach Local`.

For more information, look at [the Remote Python Debugger integration documentation](https://www.home-assistant.io/integrations/debugpy/).

### Test out this component

First we need to install some dependencies to get a basic Home Assistant instance working.

**Prerequisites**

```bash
# start terminal in contianer
pip install -r requirements_dev.txt
# install pre-commit
pre-commit install

# Configure git in devcontainer
git config user.name "John Doe"
git cofnig user.email "john_doe@gmail.com"

# Deps for pre-commit
sudo apt update
sudo apt install ruby
```

#### Install HACS

> Make sure running Task / Container is stopped

```bash
# Install deps
apt update; apt install unzip

# Install Hacs
wget -O - https://get.hacs.xyz | bash -
```

#### Add Integrations

- Run task: `Run Home Assistant on port 9123` described in [Tasks](#tasks)
- Head to: <http://localhost:9123>
- create user
- Add HACS integration
- Go to HACS
- Wait until Hacs is finished starting up.
- Install `browser_mod` and optionally `Virtual Components`.
- Add lovelace-minimalist-ui integration.
- To get the best experience with the themes that come with the integration:
- Go to Configuration -> Blueprints
- Create an automation from `System Set Default Theme`, and select what theme you want. And after restart it will be set.
- Restart home assitant to make sure everything is located
13 changes: 13 additions & 0 deletions .devcontainer/configuration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
default_config:

automation ui: !include "automations.yaml"

logger:
default: "info"
logs:
custom_components.prixCarburant: "debug"

sensor:
platform: "prixCarburant"
maxDistance: 20
49 changes: 49 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"image": "ghcr.io/ludeeus/devcontainer/integration:latest",
"name": "Prix Carburant integration development",
"context": "..",
"appPort": ["9123:8123"],
"postCreateCommand": "bash .devcontainer/post-create.sh",
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance",
"github.vscode-pull-request-github",
"ryanluker.vscode-coverage-gutters",
"visualstudioexptteam.vscodeintellicode",
"redhat.vscode-yaml",
"esbenp.prettier-vscode"
],
"settings": {
"files.eol": "\n",
"editor.tabSize": 4,
"terminal.integrated.shell.linux": "/bin/bash",
"python.pythonPath": "/usr/bin/python",
"python.analysis.autoSearchPaths": false,
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"python.linting.pylintArgs": ["--disable", "import-error"],
"python.formatting.provider": "black",
"editor.formatOnPaste": false,
"editor.formatOnSave": true,
"editor.formatOnType": true,
"files.trimTrailingWhitespace": true,
"terminal.integrated.profiles.linux": {
"zsh": {
"path": "/usr/bin/zsh"
}
},
"terminal.integrated.defaultProfile.linux": "zsh",
"files.associations": {
"*.yaml": "yaml",
"*.yml": "yaml"
},
"yaml.customTags": [
"!input scalar",
"!secret scalar",
"!include_dir_named scalar",
"!include_dir_list scalar",
"!include_dir_merge_list scalar",
"!include_dir_merge_named scalar"
]
}
}
41 changes: 41 additions & 0 deletions .devcontainer/post-create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env bash

source /opt/container/helpers/common/paths.sh
mkdir -p /config

# Required to get automations to work
echo "Creatin automations.yaml"
touch /config/automations.yaml

# source: /opt/container/helpers/commons/homeassistant/start.sh
if test -d "custom_components"; then
echo "Symlink the custom component directory"

if test -d "custom_components"; then
rm -R /config/custom_components
fi

ln -sf "$(workspacePath)custom_components/" /config/custom_components || echo "Could not copy the custom_component" exit 1
elif test -f "__init__.py"; then
echo "Having the component in the root is currently not supported"
fi

# Install
echo "Install home assistant"
container install

echo "Installing Requirements_dev.txt"
pip install -r $(workspacePath)requirements_dev.txt


# pre-commit
echo "Install pre-commit dependencies"
sudo apt update; apt install -y ruby vim

echo "Install Pre-Commit in this repo"
pre-commit install

# Custom Cards & Modules
echo "Installing NodeJS"
curl -fsSL https://deb.nodesource.com/setup_16.x | bash -
apt-get install -y nodejs
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
**/__pycache__
.DS_Store
__pycache__
pythonenv*
.python-version
.coverage
venv
.venv
56 changes: 56 additions & 0 deletions .markdownlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
# Base Markdownlint configuration
# Extended Markdownlint configuration in doc/.markdownlint/
default: true

MD001: false

MD024:
allow_different_nesting: true

MD025: false

MD028: false

first-header-h1: false

no-bare-urls: false

header-style:
style: "atx"

ul-style:
style: "dash"

ul-indent:
indent: 4

no-trailing-spaces: false

line-length: false

no-duplicate-header:
allow_different_nesting: true

no-trailing-punctuation:
punctuation: ".,;:!。,;:!?"

ol-prefix:
style: "ordered"

no-inline-html: false

hr-style:
style: "---"

no-emphasis-as-heading: false

first-line-h1: false

code-block-style:
style: ["fenced"]

proper-names:
names: []

code_blocks: false
83 changes: 83 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
---
repos:

- repo: "https://github.com/pre-commit/pre-commit-hooks"
rev: "v4.1.0"
hooks:
- id: "check-executables-have-shebangs"
- id: "check-json"
- id: "check-merge-conflict"
- id: "check-yaml"
args: ["--unsafe"]
- id: "detect-private-key"
- id: "no-commit-to-branch"
- id: "requirements-txt-fixer"
- id: "debug-statements"
- id: "end-of-file-fixer"
- id: "trailing-whitespace"
- id: "mixed-line-ending"

- repo: "https://github.com/adrienverge/yamllint.git"
rev: "v1.26.3"
hooks:
- id: "yamllint"
args: ["--format", "parsable", "--strict"]
# exclude: ".pre-commit-config.yaml"
exclude: |
(?x)^(
.pre-commit-config.yaml
)$

- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.30.0
hooks:
- id: markdownlint
args: ["--fix"]

- repo: "https://github.com/jumanjihouse/pre-commit-hook-yamlfmt"
rev: "0.1.1"
hooks:
- id: "yamlfmt"
args: ["--mapping", "2", "--sequence", "4", "--offset", "2", "--preserve-quotes"]
exclude: ".*"

- repo: "https://github.com/asottile/pyupgrade"
rev: "v2.29.1"
hooks:
- id: "pyupgrade"
args: ["--py39-plus"]

- repo: "https://github.com/pre-commit/mirrors-isort"
rev: "v5.10.1"
hooks:
- id: "isort"

- repo: "https://github.com/psf/black"
rev: "21.12b0"
hooks:
- id: "black"
args:
- "--safe"
- "--quiet"
<<: &python-files-with-tests
files: '^((custom_components|tests)/.+)?[^/]+\.py$'

- repo: "https://github.com/PyCQA/flake8"
rev: "4.0.1"
hooks:
- id: "flake8"
additional_dependencies:
- "flake8-docstrings==1.6.0"
- "pydocstyle==6.1.1"
<<: &python-files
files: '^(custom_components/.+)?[^/]+\.py$'

- repo: "https://github.com/PyCQA/bandit"
rev: "1.7.1"
hooks:
- id: "bandit"
args:
- "--quiet"
- "--format=custom"
# - "--configfile=bandit.yaml"
<<: *python-files-with-tests
29 changes: 29 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Run Home Assistant on port 9123",
"type": "shell",
"command": "container start",
"problemMatcher": []
},
{
"label": "Run Home Assistant configuration against /config",
"type": "shell",
"command": "container check",
"problemMatcher": []
},
{
"label": "Upgrade Home Assistant to latest dev",
"type": "shell",
"command": "container install",
"problemMatcher": []
},
{
"label": "Install a specific version of Home Assistant",
"type": "shell",
"command": "container set-version",
"problemMatcher": []
}
]
}
Loading