From 803ead17526d469ba175e54d9be70cc8eb742525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=85ke=20S=20-=20Piper?= Date: Thu, 14 Sep 2023 13:41:05 +0000 Subject: [PATCH 1/4] New devcontainer --- .devcontainer.json | 55 +++++++++ .pre-commit.config.yaml | 34 ++++++ .ruff.toml | 110 ++++++++++++++++++ .../.gitignore | 0 .../configuration.yaml | 0 .../devcontainer.json | 0 requirements.txt | 8 ++ scripts/develop | 35 ++++++ scripts/setup | 8 ++ 9 files changed, 250 insertions(+) create mode 100644 .devcontainer.json create mode 100644 .pre-commit.config.yaml create mode 100644 .ruff.toml rename {.devcontainer => old_devcontainer}/.gitignore (100%) rename {.devcontainer => old_devcontainer}/configuration.yaml (100%) rename {.devcontainer => old_devcontainer}/devcontainer.json (100%) create mode 100644 requirements.txt create mode 100755 scripts/develop create mode 100755 scripts/setup diff --git a/.devcontainer.json b/.devcontainer.json new file mode 100644 index 00000000..cd3c0125 --- /dev/null +++ b/.devcontainer.json @@ -0,0 +1,55 @@ +{ + "name": "dev-easee", + "image": "mcr.microsoft.com/vscode/devcontainers/python:0-3.11-bullseye", + "postCreateCommand": "scripts/setup", + "appPort": ["9125:8123"], + "portsAttributes": { + "8123": { + "label": "Home Assistant internal", + "onAutoForward": "notify" + }, + "9124": { + "label": "Home Assistant remote", + "onAutoForward": "notify" + } + }, + "customizations": { + "vscode": { + "extensions": [ + "ms-python.python", + "github.vscode-pull-request-github", + "ryanluker.vscode-coverage-gutters", + "ms-python.vscode-pylance", + "ms-python.pylint", + "ms-python.black-formatter", + "thibault-vanderseypen.i18n-json-editor" + ], + "settings": { + "files.eol": "\n", + "editor.tabSize": 4, + "python.pythonPath": "/usr/bin/python3", + "python.analysis.autoSearchPaths": false, + "python.linting.pylintEnabled": true, + "python.linting.enabled": true, + "[python]": { + "editor.defaultFormatter": "ms-python.black-formatter", + "editor.formatOnSave": true + }, + "python.formatting.provider": "none", + "editor.formatOnPaste": false, + "editor.formatOnSave": true, + "editor.formatOnType": true, + "files.trimTrailingWhitespace": true, + "[markdown]": { + "files.trimTrailingWhitespace": false + }, + "i18nJsonEditor.forceKeyUPPERCASE": false, + "i18nJsonEditor.supportedFolders": ["translations", "i18n"] + } + } + }, + "remoteUser": "vscode", + "features": { + "ghcr.io/devcontainers/features/rust:1": {} + } +} diff --git a/.pre-commit.config.yaml b/.pre-commit.config.yaml new file mode 100644 index 00000000..1835e159 --- /dev/null +++ b/.pre-commit.config.yaml @@ -0,0 +1,34 @@ +repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.0.278 + hooks: + - id: ruff + args: + - --fix + - repo: https://github.com/psf/black + rev: 23.1.0 + hooks: + - id: black + args: + - --quiet + files: ^((custom_components|homeassistant|pylint|script|tests)/.+)?[^/]+\.py$ + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.4.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-json + exclude: (.vscode|.devcontainer) + - id: pretty-format-json + args: ['--autofix', '--no-ensure-ascii', '--top-keys=domain,name'] + files: manifest.json + - id: pretty-format-json + args: ['--autofix', '--no-ensure-ascii', '--top-keys=name'] + files: hacs.json + - id: pretty-format-json + args: ['--autofix', '--no-ensure-ascii', '--no-sort-keys'] + files: (/strings\.json$|translations/.+\.json$) + - id: check-yaml + args: ['--unsafe'] + - id: check-added-large-files + - id: check-shebang-scripts-are-executable diff --git a/.ruff.toml b/.ruff.toml new file mode 100644 index 00000000..f24d6a77 --- /dev/null +++ b/.ruff.toml @@ -0,0 +1,110 @@ +# The contents of this file is based on https://github.com/home-assistant/core/blob/dev/pyproject.toml + +target-version = "py310" + +select = [ + "B002", # Python does not support the unary prefix increment + "B007", # Loop control variable {name} not used within loop body + "B014", # Exception handler with duplicate exception + "B023", # Function definition does not bind loop variable {name} + "B026", # Star-arg unpacking after a keyword argument is strongly discouraged + "C", # complexity + "COM818", # Trailing comma on bare tuple prohibited + "D", # docstrings + "E", # pycodestyle + "F", # pyflakes/autoflake + "G", # flake8-logging-format + "I", # isort + "ICN001", # import concentions; {name} should be imported as {asname} + "ISC001", # Implicitly concatenated string literals on one line + "N804", # First argument of a class method should be named cls + "N805", # First argument of a method should be named self + "N815", # Variable {name} in class scope should not be mixedCase + "PGH001", # No builtin eval() allowed + "PGH004", # Use specific rule codes when using noqa + "PLC0414", # Useless import alias. Import alias does not rename original package. + "PLC", # pylint + "PLE", # pylint + "PLR", # pylint + "PLW", # pylint + "Q000", # Double quotes found but single quotes preferred + "RUF006", # Store a reference to the return value of asyncio.create_task + "S102", # Use of exec detected + "S103", # bad-file-permissions + "S108", # hardcoded-temp-file + "S306", # suspicious-mktemp-usage + "S307", # suspicious-eval-usage + "S313", # suspicious-xmlc-element-tree-usage + "S314", # suspicious-xml-element-tree-usage + "S315", # suspicious-xml-expat-reader-usage + "S316", # suspicious-xml-expat-builder-usage + "S317", # suspicious-xml-sax-usage + "S318", # suspicious-xml-mini-dom-usage + "S319", # suspicious-xml-pull-dom-usage + "S320", # suspicious-xmle-tree-usage + "S601", # paramiko-call + "S602", # subprocess-popen-with-shell-equals-true + "S604", # call-with-shell-equals-true + "S608", # hardcoded-sql-expression + "S609", # unix-command-wildcard-injection + "SIM105", # Use contextlib.suppress({exception}) instead of try-except-pass + "SIM117", # Merge with-statements that use the same scope + "SIM118", # Use {key} in {dict} instead of {key} in {dict}.keys() + "SIM201", # Use {left} != {right} instead of not {left} == {right} + "SIM208", # Use {expr} instead of not (not {expr}) + "SIM212", # Use {a} if {a} else {b} instead of {b} if not {a} else {a} + "SIM300", # Yoda conditions. Use 'age == 42' instead of '42 == age'. + "SIM401", # Use get from dict with default instead of an if block + "T100", # Trace found: {name} used + "T20", # flake8-print + "TID251", # Banned imports + "TRY004", # Prefer TypeError exception for invalid type + "TRY200", # Use raise from to specify exception cause + "TRY302", # Remove exception handler; error is immediately re-raised + "UP", # pyupgrade + "W", # pycodestyle +] + +ignore = [ + "D202", # No blank lines allowed after function docstring + "D203", # 1 blank line required before class docstring + "D213", # Multi-line docstring summary should start at the second line + "D406", # Section name should end with a newline + "D407", # Section name underlining + "E501", # line too long + "E731", # do not assign a lambda expression, use a def + "PLC1901", # Lots of false positives + # False positives https://github.com/astral-sh/ruff/issues/5386 + "PLC0208", # Use a sequence type instead of a `set` when iterating over values + "PLR0911", # Too many return statements ({returns} > {max_returns}) + "PLR0912", # Too many branches ({branches} > {max_branches}) + "PLR0913", # Too many arguments to function call ({c_args} > {max_args}) + "PLR0915", # Too many statements ({statements} > {max_statements}) + "PLR2004", # Magic value used in comparison, consider replacing {value} with a constant variable + "PLW2901", # Outer {outer_kind} variable {name} overwritten by inner {inner_kind} target + "UP006", # keep type annotation style as is + "UP007", # keep type annotation style as is + # Ignored due to performance: https://github.com/charliermarsh/ruff/issues/2923 + "UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)` + +] + +[flake8-import-conventions.extend-aliases] +voluptuous = "vol" +"homeassistant.helpers.area_registry" = "ar" +"homeassistant.helpers.config_validation" = "cv" +"homeassistant.helpers.device_registry" = "dr" +"homeassistant.helpers.entity_registry" = "er" +"homeassistant.helpers.issue_registry" = "ir" +"homeassistant.util.dt" = "dt_util" + +[isort] +force-sort-within-sections = true +known-first-party = ["homeassistant"] +combine-as-imports = true + +[flake8-pytest-style] +fixture-parentheses = false + +[mccabe] +max-complexity = 25 diff --git a/.devcontainer/.gitignore b/old_devcontainer/.gitignore similarity index 100% rename from .devcontainer/.gitignore rename to old_devcontainer/.gitignore diff --git a/.devcontainer/configuration.yaml b/old_devcontainer/configuration.yaml similarity index 100% rename from .devcontainer/configuration.yaml rename to old_devcontainer/configuration.yaml diff --git a/.devcontainer/devcontainer.json b/old_devcontainer/devcontainer.json similarity index 100% rename from .devcontainer/devcontainer.json rename to old_devcontainer/devcontainer.json diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..2b351c61 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,8 @@ +colorlog==6.7.0 +homeassistant +pip>=21.0,<23.3 +ruff==0.0.288 +pre-commit==3.4.0 +bump2version==1.0.1 + +pyeasee diff --git a/scripts/develop b/scripts/develop new file mode 100755 index 00000000..ea3c228b --- /dev/null +++ b/scripts/develop @@ -0,0 +1,35 @@ +#!/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 +if ! grep -R "^logger:" config/configuration.yaml >> /dev/null;then +echo -n " +logger: + default: info + logs: + homeassistant.components.viva: debug +" >> config/configuration.yaml +fi +if ! grep -R "debugpy:" config/configuration.yaml >> /dev/null;then +echo " +# Uncomment the line below if you want to use debugger +# debugpy: +" >> config/configuration.yaml +fi + + +# Set the path to custom_components +## This let's us have the structure we want /custom_components/ake_dev +## 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/setup b/scripts/setup new file mode 100755 index 00000000..68a65c01 --- /dev/null +++ b/scripts/setup @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -e + +cd "$(dirname "$0")/.." + +python3 -m pip install --requirement requirements.txt +pre-commit install From b4a2f255ebacc95a7cd95ec1e94ac978bb8cd7a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=85ke=20S=20-=20Piper?= Date: Thu, 14 Sep 2023 14:07:18 +0000 Subject: [PATCH 2/4] Tune new devcontainer --- .gitignore | 3 + ...mit.config.yaml => .pre-commit-config.yaml | 0 .vscode/settings.json | 12 ---- .vscode/tasks.json | 56 ++++++++++--------- old_devcontainer/.gitignore | 1 - old_devcontainer/configuration.yaml | 10 ---- old_devcontainer/devcontainer.json | 56 ------------------- scripts/dev-branch | 7 +++ scripts/develop | 3 +- scripts/lint | 7 +++ scripts/specific-version | 8 +++ scripts/upgrade | 7 +++ 12 files changed, 65 insertions(+), 105 deletions(-) rename .pre-commit.config.yaml => .pre-commit-config.yaml (100%) delete mode 100644 old_devcontainer/.gitignore delete mode 100644 old_devcontainer/configuration.yaml delete mode 100644 old_devcontainer/devcontainer.json create mode 100755 scripts/dev-branch create mode 100755 scripts/lint create mode 100755 scripts/specific-version create mode 100755 scripts/upgrade diff --git a/.gitignore b/.gitignore index 7eedc14a..d91d1f35 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Home Assistant config +config/ + #Links for local development /__init__.py /manifest.json diff --git a/.pre-commit.config.yaml b/.pre-commit-config.yaml similarity index 100% rename from .pre-commit.config.yaml rename to .pre-commit-config.yaml diff --git a/.vscode/settings.json b/.vscode/settings.json index f4bf9b84..e69de29b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,12 +0,0 @@ -{ - "python.linting.pylintEnabled": true, - "python.linting.enabled": true, - "python.pythonPath": "/usr/local/bin/python", - "files.associations": { - "*.yaml": "home-assistant" - }, - "[python]": { - "editor.defaultFormatter": "ms-python.black-formatter" - }, - "python.formatting.provider": "none" -} diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 50f7fb73..bf5a3dfa 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -1,29 +1,35 @@ { "version": "2.0.0", "tasks": [ - { - "label": "Run Home Assistant on port 9125", - "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": [] - } + { + "label": "Run Home Assistant on port 9125", + "type": "shell", + "command": "scripts/develop", + "problemMatcher": [] + }, + { + "label": "Upgrade Home Assistant to latest (beta)", + "type": "shell", + "command": "scripts/upgrade", + "problemMatcher": [] + }, + { + "label": "Load Home Assistant from github - dev branch", + "type": "shell", + "command": "scripts/dev-branch", + "problemMatcher": [] + }, + { + "label": "Load specific version of Home Assistant", + "type": "shell", + "command": "scripts/specific-version", + "problemMatcher": [] + }, + { + "label": "Lint with ruff", + "type": "shell", + "command": "scripts/lint", + "problemMatcher": [] + } ] -} \ No newline at end of file + } diff --git a/old_devcontainer/.gitignore b/old_devcontainer/.gitignore deleted file mode 100644 index 33a92995..00000000 --- a/old_devcontainer/.gitignore +++ /dev/null @@ -1 +0,0 @@ -secrets.yaml \ No newline at end of file diff --git a/old_devcontainer/configuration.yaml b/old_devcontainer/configuration.yaml deleted file mode 100644 index 6d010fb9..00000000 --- a/old_devcontainer/configuration.yaml +++ /dev/null @@ -1,10 +0,0 @@ -default_config: - -logger: - default: info - logs: - custom_components.easee: debug - pyeasee: debug - -# If you need to debug uncomment the line below (doc: https://www.home-assistant.io/integrations/debugpy/) -debugpy: \ No newline at end of file diff --git a/old_devcontainer/devcontainer.json b/old_devcontainer/devcontainer.json deleted file mode 100644 index b93707a3..00000000 --- a/old_devcontainer/devcontainer.json +++ /dev/null @@ -1,56 +0,0 @@ -// See https://aka.ms/vscode-remote/devcontainer.json for format details. -{ - "image": "ghcr.io/ludeeus/devcontainer/integration:stable", - "name": "Easee", - "appPort": [ - "9125:8123" - ], - "postCreateCommand": "container install && pip install -Ur requirements-devcontainer.txt", - "customizations": { - "vscode": { - "extensions": [ - "ms-python.python", - "github.vscode-pull-request-github", - "ryanluker.vscode-coverage-gutters", - "ms-python.vscode-pylance", - "ms-python.pylint", - "thibault-vanderseypen.i18n-json-editor" - ], - "settings": { - "terminal.integrated.profiles.linux": { - "bash": { - "path": "bash", - "icon": "terminal-bash" - }, - "zsh": { - "path": "zsh" - } - }, - "terminal.integrated.defaultProfile.linux": "zsh", - "python.pythonPath": "/usr/bin/python3", - "python.analysis.autoSearchPaths": false, - "python.linting.pylintEnabled": true, - "python.linting.enabled": true, - "python.formatting.provider": "black", - "editor.formatOnPaste": false, - "editor.formatOnSave": true, - "editor.formatOnType": true, - "editor.renderFinalNewline": "dimmed", - "editor.tabSize": 4, - "files.eol": "\n", - "files.insertFinalNewline": true, - "files.trimTrailingWhitespace": true, - "i18nJsonEditor.forceKeyUPPERCASE": false, - "i18nJsonEditor.supportedFolders": [ - "translations", - "i18n" - ] - } - } - }, - "mounts": [ - // Uncomment the line below to enable a persistent configuration directory - // Create ~/easee_hass_config directory and rebuild container - "source=${localEnv:HOME}/easee_hass_config,target=/config,type=bind" - ] -} diff --git a/scripts/dev-branch b/scripts/dev-branch new file mode 100755 index 00000000..4022526b --- /dev/null +++ b/scripts/dev-branch @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +set -e + +cd "$(dirname "$0")/.." + +python3 -m pip --disable-pip-version-check install --upgrade git+https://github.com/home-assistant/home-assistant.git@dev diff --git a/scripts/develop b/scripts/develop index ea3c228b..ea740eff 100755 --- a/scripts/develop +++ b/scripts/develop @@ -14,7 +14,8 @@ echo -n " logger: default: info logs: - homeassistant.components.viva: debug + custom_components.easee: debug + pyeasee: debug " >> config/configuration.yaml fi if ! grep -R "debugpy:" config/configuration.yaml >> /dev/null;then diff --git a/scripts/lint b/scripts/lint new file mode 100755 index 00000000..9b5b1df0 --- /dev/null +++ b/scripts/lint @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +set -e + +cd "$(dirname "$0")/.." + +ruff check . --fix diff --git a/scripts/specific-version b/scripts/specific-version new file mode 100755 index 00000000..fbfdd6fb --- /dev/null +++ b/scripts/specific-version @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -e + +cd "$(dirname "$0")/.." + +read -p 'Set Home Assistant version: ' -r version +python3 -m pip --disable-pip-version-check install --upgrade homeassistant=="$version" diff --git a/scripts/upgrade b/scripts/upgrade new file mode 100755 index 00000000..7ce8f789 --- /dev/null +++ b/scripts/upgrade @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +set -e + +cd "$(dirname "$0")/.." + +python3 -m pip install --upgrade --pre homeassistant From 622bf3dd4642690e51a8920ab74ec899259e4eac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=85ke=20S=20-=20Piper?= Date: Thu, 14 Sep 2023 14:15:20 +0000 Subject: [PATCH 3/4] Update README --- README.md | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index bdd2d57b..26db0711 100644 --- a/README.md +++ b/README.md @@ -82,19 +82,9 @@ logger: ## Development Container One option for development is to use the VS Code Dev Container. You need to have Docker installed. -1. For best performance, make sure to clone the repo in WSL2 if running on Windows. - - ```console - $ git clone https://github.com/{your_user}/easee_hass - $ mkdir easee_hass_config - ``` -1. Open the repository in VS Code. - - ```console - $ code easee_hass - ``` -1. VS Code will ask to reopen the folder in a container - - If not, press `Ctrl`+`Shift`+`P` and select `Dev Containers: Reopen in Dev Container`. +1. Open a new blank vscode +1. Select Command palette->Dev containers: Clone repository in named container volume +1. Follow the prompts 1. Wait for the container to be built. 1. Press `Ctrl`+`Shift`+`P` and select `Tasks: Run Task` > `Run Home Assistant on port 9125`. 1. Wait for Home Assistant to start and go to http://localhost:9125/. From 3c3931468074bdaaba2e1506cfd316b4211f6626 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=85ke=20S=20-=20Piper?= Date: Thu, 14 Sep 2023 15:25:45 +0000 Subject: [PATCH 4/4] change to ruff in makefile --- Makefile | 4 +--- requirements-devcontainer.txt | 6 ++---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 439b7842..e8e81fa2 100644 --- a/Makefile +++ b/Makefile @@ -4,9 +4,7 @@ bump: bump2version --allow-dirty --current-version $(__VERSION__) patch Makefile custom_components/easee/const.py custom_components/easee/manifest.json lint: - isort custom_components - black custom_components - flake8 custom_components + ruff check custom_components --fix install_dev: pip install -r requirements-dev.txt diff --git a/requirements-devcontainer.txt b/requirements-devcontainer.txt index 52cbedf5..88356904 100644 --- a/requirements-devcontainer.txt +++ b/requirements-devcontainer.txt @@ -1,7 +1,5 @@ -colorlog -black -isort -flake8 +colorlog==6.7.0 +ruff==0.0.288 bump2version wheel