From 94c46762c81d347381ec8c6242fac1c8c5ae3d80 Mon Sep 17 00:00:00 2001 From: Tore Amundsen Date: Thu, 18 Jul 2024 22:33:44 +0000 Subject: [PATCH] Upgrade container and linting --- .devcontainer.json | 41 +++++++++ .devcontainer/README.md | 43 --------- .devcontainer/configuration.yaml | 15 ---- .devcontainer/devcontainer.json | 32 ------- .gitattributes | 1 + .github/workflows/lint.yml | 32 +++++++ .github/workflows/pull.yml | 35 -------- .github/workflows/push.yml | 38 -------- .gitignore | 147 +++---------------------------- .ruff.toml | 26 ++++++ .vscode/settings.json | 18 ---- .vscode/tasks.json | 22 +---- config/configuration.yaml | 16 ++++ requirements.txt | 4 + scripts/develop | 20 +++++ scripts/lint | 8 ++ scripts/setup | 7 ++ setup.cfg | 39 -------- 18 files changed, 169 insertions(+), 375 deletions(-) create mode 100644 .devcontainer.json delete mode 100644 .devcontainer/README.md delete mode 100644 .devcontainer/configuration.yaml delete mode 100644 .devcontainer/devcontainer.json create mode 100644 .gitattributes create mode 100644 .github/workflows/lint.yml delete mode 100644 .github/workflows/pull.yml delete mode 100644 .github/workflows/push.yml create mode 100644 .ruff.toml delete mode 100644 .vscode/settings.json create mode 100644 config/configuration.yaml create mode 100644 requirements.txt create mode 100644 scripts/develop create mode 100644 scripts/lint create mode 100644 scripts/setup delete mode 100644 setup.cfg diff --git a/.devcontainer.json b/.devcontainer.json new file mode 100644 index 0000000..2b67dd4 --- /dev/null +++ b/.devcontainer.json @@ -0,0 +1,41 @@ +// See https://aka.ms/vscode-remote/devcontainer.json for format details. +{ + "image": "mcr.microsoft.com/devcontainers/python:3.12", + "postCreateCommand": "scripts/setup", + "forwardPorts": [ + 8123 + ], + "portsAttributes": { + "8123": { + "label": "Home Assistant", + "onAutoForward": "notify" + } + }, + "customizations": { + "vscode": { + "extensions": [ + "charliermarsh.ruff", + "ms-python.python", + "ms-python.vscode-pylance", + "github.vscode-pull-request-github" + ], + "settings": { + "files.eol": "\n", + "editor.tabSize": 4, + "editor.formatOnPaste": true, + "editor.formatOnSave": true, + "editor.formatOnType": false, + "files.trimTrailingWhitespace": true, + "terminal.integrated.shell.linux": "/bin/bash", + "python.analysis.typeCheckingMode": "basic", + "python.analysis.autoImportCompletions": true, + "python.defaultInterpreterPath": "/usr/local/bin/python", + "[python]": { + "editor.defaultFormatter": "charliermarsh.ruff" + } + } + } + }, + "remoteUser": "vscode", + "features": {} +} \ No newline at end of file diff --git a/.devcontainer/README.md b/.devcontainer/README.md deleted file mode 100644 index 752c007..0000000 --- a/.devcontainer/README.md +++ /dev/null @@ -1,43 +0,0 @@ -## 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 compnent 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 devcontainter 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 compnent 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 Assitant core version in the container to the latest version of the `dev` branch. -Install a spesific version of Home Assistant | Install a specific version of Home Assistant core in the container. diff --git a/.devcontainer/configuration.yaml b/.devcontainer/configuration.yaml deleted file mode 100644 index 3d1f66e..0000000 --- a/.devcontainer/configuration.yaml +++ /dev/null @@ -1,15 +0,0 @@ -#default_config: - -automation: -config: -frontend: -system_health: - -logger: - default: info - logs: - custom_components.victorsmartkill: debug - -debugpy: - start: true - wait: true diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json deleted file mode 100644 index 058efd3..0000000 --- a/.devcontainer/devcontainer.json +++ /dev/null @@ -1,32 +0,0 @@ -// See https://aka.ms/vscode-remote/devcontainer.json for format details. -{ - "image": "ghcr.io/ludeeus/devcontainer/integration:stable", - "context": "..", - "appPort": [ - "9123:8123" - ], - "postCreateCommand": "container install", - "extensions": [ - "ms-python.python", - "ms-python.vscode-pylance", - "github.vscode-pull-request-github", - ], - "settings": { - "files.eol": "\n", - "editor.tabSize": 4, - "terminal.integrated.shell.linux": "/bin/bash", - "python.pythonPath": "/usr/bin/python3", - "python.linting.pylintEnabled": true, - "python.linting.enabled": true, - "python.linting.flake8Enabled": true, - "python.linting.mypyEnabled": true, - "python.formatting.provider": "black", - "editor.formatOnPaste": false, - "editor.formatOnSave": true, - "editor.formatOnType": true, - "files.trimTrailingWhitespace": true - }, - "remoteEnv": { - "PATH": "${containerEnv:PATH}:/root/.local/bin/", - } -} \ No newline at end of file diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..94f480d --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto eol=lf \ No newline at end of file diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..17602b3 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,32 @@ +name: "Lint" + +on: + push: + branches: + - "main" + pull_request: + branches: + - "main" + +jobs: + ruff: + name: "Ruff" + runs-on: "ubuntu-latest" + steps: + - name: "Checkout the repository" + uses: "actions/checkout@v4.1.7" + + - name: "Set up Python" + uses: actions/setup-python@v5.1.0 + with: + python-version: "3.12" + cache: "pip" + + - name: "Install requirements" + run: python3 -m pip install -r requirements.txt + + - name: "Lint" + run: python3 -m ruff check . + + - name: "Format" + run: python3 -m ruff format . --check \ No newline at end of file diff --git a/.github/workflows/pull.yml b/.github/workflows/pull.yml deleted file mode 100644 index a9de139..0000000 --- a/.github/workflows/pull.yml +++ /dev/null @@ -1,35 +0,0 @@ -name: Pull actions - -on: - pull_request: - -jobs: - validate: - runs-on: ubuntu-latest - name: Validate - steps: - - uses: actions/checkout@v4 - - - name: HACS validation - uses: hacs/action@main - with: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - CATEGORY: integration - - - name: Hassfest validation - uses: home-assistant/actions/hassfest@master - - style: - runs-on: ubuntu-latest - name: Check style formatting - steps: - - uses: actions/checkout@v4 - - - uses: actions/setup-python@v5 - with: - python-version: 3.12 - cache: pip - - - run: pip install black - - - run: black . diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml deleted file mode 100644 index b658c51..0000000 --- a/.github/workflows/push.yml +++ /dev/null @@ -1,38 +0,0 @@ -name: Push actions - -on: - push: - branches: - - master - - dev - -jobs: - validate: - runs-on: ubuntu-latest - name: Validate - steps: - - uses: actions/checkout@v4 - - - name: HACS validation - uses: hacs/action@main - with: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - CATEGORY: integration - - - name: Hassfest validation - uses: home-assistant/actions/hassfest@master - - style: - runs-on: ubuntu-latest - name: Check style formatting - steps: - - uses: actions/checkout@v4 - - - uses: actions/setup-python@v5 - with: - python-version: 3.12 - cache: pip - - - run: pip install black - - - run: black . diff --git a/.gitignore b/.gitignore index 5fa63a1..dc73b19 100644 --- a/.gitignore +++ b/.gitignore @@ -1,141 +1,18 @@ -# Byte-compiled / optimized / DLL files -__pycache__/ -*.py[cod] -*$py.class +# artifacts +__pycache__ +.pytest* +*.egg-info +*/build/* +*/dist/* -# C extensions -*.so -# Distribution / packaging -.Python -build/ -develop-eggs/ -dist/ -downloads/ -eggs/ -.eggs/ -lib/ -lib64/ -parts/ -sdist/ -var/ -wheels/ -share/python-wheels/ -*.egg-info/ -.installed.cfg -*.egg -MANIFEST - -# PyInstaller -# Usually these files are written by a python script from a template -# before PyInstaller builds the exe, so as to inject date/other infos into it. -*.manifest -*.spec - -# Installer logs -pip-log.txt -pip-delete-this-directory.txt - -# Unit test / coverage reports -htmlcov/ -.tox/ -.nox/ +# misc .coverage -.coverage.* -.cache -nosetests.xml +.vscode coverage.xml -*.cover -*.py,cover -.hypothesis/ -.pytest_cache/ -cover/ - -# Translations -*.mo -*.pot - -# Django stuff: -*.log -local_settings.py -db.sqlite3 -db.sqlite3-journal - -# Flask stuff: -instance/ -.webassets-cache - -# Scrapy stuff: -.scrapy - -# Sphinx documentation -docs/_build/ - -# PyBuilder -.pybuilder/ -target/ - -# Jupyter Notebook -.ipynb_checkpoints - -# IPython -profile_default/ -ipython_config.py - -# pyenv -# For a library or package, you might want to ignore these files since the code is -# intended to run in multiple environments; otherwise, check them in: -# .python-version - -# pipenv -# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. -# However, in case of collaboration, if having platform-specific dependencies or dependencies -# having no cross-platform support, pipenv may install dependencies that don't work, or not -# install all needed dependencies. -#Pipfile.lock - -# PEP 582; used by e.g. github.com/David-OConnor/pyflow -__pypackages__/ - -# Celery stuff -celerybeat-schedule -celerybeat.pid - -# SageMath parsed files -*.sage.py - -# Environments -.env -.venv -env/ -venv/ -ENV/ -env.bak/ -venv.bak/ - -# Spyder project settings -.spyderproject -.spyproject - -# Rope project settings -.ropeproject - -# mkdocs documentation -/site - -# mypy -.mypy_cache/ -.dmypy.json -dmypy.json - -# Pyre type checker -.pyre/ - -# pytype static type analyzer -.pytype/ +.ruff_cache -# Cython debug symbols -cython_debug/ -# Mac -.DS_Store +# Home Assistant configuration +config/* +!config/configuration.yaml diff --git a/.ruff.toml b/.ruff.toml new file mode 100644 index 0000000..8ea6a71 --- /dev/null +++ b/.ruff.toml @@ -0,0 +1,26 @@ +# The contents of this file is based on https://github.com/home-assistant/core/blob/dev/pyproject.toml + +target-version = "py312" + +[lint] +select = [ + "ALL", +] + +ignore = [ + "ANN101", # Missing type annotation for `self` in method + "ANN401", # Dynamically typed expressions (typing.Any) are disallowed + "D203", # no-blank-line-before-class (incompatible with formatter) + "D212", # multi-line-summary-first-line (incompatible with formatter) + "COM812", # incompatible with formatter + "ISC001", # incompatible with formatter +] + +[lint.flake8-pytest-style] +fixture-parentheses = false + +[lint.pyupgrade] +keep-runtime-typing = true + +[lint.mccabe] +max-complexity = 25 \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 28e897d..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "[python]": { - "editor.codeActionsOnSave": { - "source.organizeImports": true - } - }, - "python.linting.pylintArgs": [ - "--init-hook", - "import sys; sys.path.append(\"/workspaces/victorsmartkill-homeassistant\")" - ], - "python.linting.enabled": true, - "python.linting.flake8Enabled": true, - "python.linting.mypyEnabled": true, - "python.testing.promptToConfigure": false, - "python.testing.pytestEnabled": false, - "python.testing.unittestEnabled": false, - "python.testing.nosetestsEnabled": false, -} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 1922501..54ba9c4 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -2,27 +2,9 @@ "version": "2.0.0", "tasks": [ { - "label": "Run Home Assistant on port 9123", + "label": "Run Home Assistant on port 8123", "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 spesific version of Home Assistant", - "type": "shell", - "command": "container set-version", + "command": "scripts/develop", "problemMatcher": [] } ] diff --git a/config/configuration.yaml b/config/configuration.yaml new file mode 100644 index 0000000..e93692f --- /dev/null +++ b/config/configuration.yaml @@ -0,0 +1,16 @@ +# https://www.home-assistant.io/integrations/default_config/ +default_config: + +# https://www.home-assistant.io/integrations/homeassistant/ +homeassistant: + debug: true + +# https://www.home-assistant.io/integrations/logger/ +logger: + default: info + logs: + custom_components.victorsmartkill: debug + +debugpy: + start: true + wait: true diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..3b010b5 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +colorlog==6.8.2 +homeassistant==2024.6.0 +pip>=21.3.1 +ruff==0.5.1 \ No newline at end of file diff --git a/scripts/develop b/scripts/develop new file mode 100644 index 0000000..89eda50 --- /dev/null +++ b/scripts/develop @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +set -e + +cd "$(dirname "$0")/.." + +# Create config dir if not present +if [[ ! -d "${PWD}/config" ]]; then + mkdir -p "${PWD}/config" + hass --config "${PWD}/config" --script ensure_config +fi + +# Set the path to custom_components +## This let's us have the structure we want /custom_components/integration_blueprint +## while at the same time have Home Assistant configuration inside /config +## without resulting to symlinks. +export PYTHONPATH="${PYTHONPATH}:${PWD}/custom_components" + +# Start Home Assistant +hass --config "${PWD}/config" --debug diff --git a/scripts/lint b/scripts/lint new file mode 100644 index 0000000..c13df0e --- /dev/null +++ b/scripts/lint @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -e + +cd "$(dirname "$0")/.." + +ruff format . +ruff check . --fix \ No newline at end of file diff --git a/scripts/setup b/scripts/setup new file mode 100644 index 0000000..141d19f --- /dev/null +++ b/scripts/setup @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +set -e + +cd "$(dirname "$0")/.." + +python3 -m pip install --requirement requirements.txt diff --git a/setup.cfg b/setup.cfg deleted file mode 100644 index 26a5fc0..0000000 --- a/setup.cfg +++ /dev/null @@ -1,39 +0,0 @@ -[flake8] -exclude = .venv,.git,.tox,docs,venv,bin,lib,deps,build -doctests = True -# To work with Black -max-line-length = 88 -# E501: line too long -# W503: Line break occurred before a binary operator -# E203: Whitespace before ':' -# D202 No blank lines allowed after function docstring -# W504 line break after binary operator -ignore = - E501, - W503, - E203, - D202, - W504 - -[isort] -# https://github.com/timothycrosley/isort -# https://github.com/timothycrosley/isort/wiki/isort-Settings -# splits long import on multiple lines indented by 4 spaces -multi_line_output = 3 -include_trailing_comma=True -force_grid_wrap=0 -use_parentheses=True -line_length=88 -indent = " " -force_sort_within_sections = true -sections = FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,LOCALFOLDER -default_section = THIRDPARTY -known_first_party = custom_components.victorsmartkill -combine_as_imports = true - - -[mypy] -follow_imports = silent - -[mypy-homeassistant.*, voluptuous] -ignore_missing_imports = True