From 3734055f28bb99b379dba5620a40cf5c2fa651ca Mon Sep 17 00:00:00 2001 From: Erik Seglem Date: Mon, 13 Nov 2023 14:57:26 +0000 Subject: [PATCH] Cleanup docs and code. --- .devcontainer.json | 5 +-- .github/ISSUE_TEMPLATE/config.yml | 2 +- .github/dependabot.yml | 8 ----- .github/workflows/lint.yml | 31 ---------------- .github/workflows/release.yml | 6 ++-- .github/workflows/validate.yml | 26 +++++++------- .pre-commit-config.yaml | 42 ++++++++++++++++++++++ CONTRIBUTING.md | 12 ++++--- README.md | 47 +++++++++++++------------ custom_components/wattbox/const.py | 8 +++-- custom_components/wattbox/manifest.json | 4 ++- requirements.txt | 9 +++-- scripts/lint | 2 +- 13 files changed, 108 insertions(+), 94 deletions(-) delete mode 100644 .github/workflows/lint.yml create mode 100644 .pre-commit-config.yaml diff --git a/.devcontainer.json b/.devcontainer.json index 3c41bf8..0b12430 100644 --- a/.devcontainer.json +++ b/.devcontainer.json @@ -17,15 +17,16 @@ "ms-python.python", "ms-python.vscode-pylance", "ms-python.black-formatter", + "ms-python.mypy-type-checker", "charliermarsh.ruff", "EditorConfig.EditorConfig", "redhat.vscode-yaml", - "tamasfe.even-better-toml" + "tamasfe.even-better-toml", + "github.vscode-github-actions" ], "settings": { "files.eol": "\n", "editor.tabSize": 4, - "python.pythonPath": "/usr/bin/python3", "python.analysis.autoSearchPaths": false, "editor.formatOnPaste": false, "editor.formatOnSave": true, diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml index ec4bb38..3ba13e0 100644 --- a/.github/ISSUE_TEMPLATE/config.yml +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -1 +1 @@ -blank_issues_enabled: false \ No newline at end of file +blank_issues_enabled: false diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 39b7e94..7623913 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -5,11 +5,3 @@ updates: directory: "/" schedule: interval: "monthly" - - - package-ecosystem: "pip" - directory: "/" - schedule: - interval: "monthly" - ignore: - # Dependabot should not update Home Assistant as that should match the homeassistant key in hacs.json - - dependency-name: "homeassistant" \ No newline at end of file diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml deleted file mode 100644 index 954bea5..0000000 --- a/.github/workflows/lint.yml +++ /dev/null @@ -1,31 +0,0 @@ -name: "Lint" - -on: - push: - branches: - - "master" - - "develop" - pull_request: - branches: - - "master" - - "develop" - -jobs: - ruff: - name: "Ruff" - runs-on: "ubuntu-latest" - steps: - - name: "Checkout the repository" - uses: "actions/checkout@v3.5.2" - - - name: "Set up Python" - uses: actions/setup-python@v4.6.0 - with: - python-version: "3.10" - cache: "pip" - - - name: "Install requirements" - run: python3 -m pip install ruff - - - name: "Run" - run: python3 -m ruff check . diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8ab9942..ab530f7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -21,15 +21,15 @@ jobs: shell: "bash" run: | yq -i -o json '.version="${{ github.event.release.tag_name }}"' \ - "${{ github.workspace }}/custom_components/hass-wattbox/manifest.json" + "${{ github.workspace }}/custom_components/wattbox/manifest.json" - name: "ZIP the integration directory" shell: "bash" run: | - cd "${{ github.workspace }}/custom_components/hass-wattbox" + cd "${{ github.workspace }}/custom_components/wattbox" zip hass-wattbox.zip -r ./ - name: "Upload the ZIP file to the release" uses: softprops/action-gh-release@v0.1.15 with: - files: ${{ github.workspace }}/custom_components/hass-wattbox/hass-wattbox.zip + files: ${{ github.workspace }}/custom_components/wattbox/hass-wattbox.zip diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index 85459f6..37673d9 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -3,14 +3,14 @@ name: "Validate" on: workflow_dispatch: schedule: - - cron: "0 0 * * *" + - cron: "0 0 * * *" push: branches: - "master" - "develop" pull_request: branches: - - "main" + - "master" - "develop" jobs: @@ -18,22 +18,20 @@ jobs: name: "Hassfest Validation" runs-on: "ubuntu-latest" steps: - - name: "Checkout the repository" - uses: "actions/checkout@v3.5.2" + - name: "Checkout the repository" + uses: "actions/checkout@v3.5.2" - - name: "Run hassfest validation" - uses: "home-assistant/actions/hassfest@master" + - name: "Run hassfest validation" + uses: "home-assistant/actions/hassfest@master" hacs: # https://github.com/hacs/action name: "HACS Validation" runs-on: "ubuntu-latest" steps: - - name: "Checkout the repository" - uses: "actions/checkout@v3.5.2" + - name: "Checkout the repository" + uses: "actions/checkout@v3.5.2" - - name: "Run HACS validation" - uses: "hacs/action@main" - with: - category: "integration" - # Remove this 'ignore' key when you have added brand images for your integration to https://github.com/home-assistant/brands - ignore: "brands" + - name: "Run HACS validation" + uses: "hacs/action@main" + with: + category: "integration" diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..4bf7c94 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,42 @@ +ci: + autoupdate_schedule: monthly + +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: check-added-large-files + - id: check-toml + - id: check-yaml + args: ["--unsafe"] + - id: debug-statements + - id: end-of-file-fixer + - id: mixed-line-ending + args: ["--fix=lf"] + - id: trailing-whitespace + - repo: https://github.com/asottile/pyupgrade + rev: v3.15.0 + hooks: + - id: pyupgrade + args: ["--py38-plus", "--keep-runtime-typing"] + - repo: https://github.com/charliermarsh/ruff-pre-commit + rev: v0.1.5 + hooks: + - id: ruff + args: ["--fix"] + - repo: https://github.com/psf/black + rev: 23.11.0 + hooks: + - id: black + language_version: python + - repo: https://github.com/pre-commit/mirrors-mypy + rev: v1.7.0 + hooks: + - id: mypy + language_version: python + args: [--config-file=pyproject.toml, custom_components/] + pass_filenames: false + additional_dependencies: + - homeassistant-stubs + - pywattbox [http, ip] @ git+https://github.com/eseglem/pywattbox@develop#pywattbox==0.6.0 + - types-beautifulsoup4 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2c3048a..f6f1582 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -14,10 +14,12 @@ Github is used to host code, to track issues and feature requests, as well as ac Pull requests are the best way to propose changes to the codebase. 1. Fork the repo and create your branch from `main`. -2. If you've changed something, update the documentation. -3. Make sure your code lints (using `scripts/lint`). -4. Test you contribution. -5. Issue that pull request! +1. If you've changed something, update the documentation. +1. Use `pre-commit`. + 1. `pre-commit install` will run it automatically on each commit. + 1. `pre-commit run --all` manually runs all checks. +1. Test you contribution. +1. Issue that pull request! ## Any contributions you make will be under the MIT Software License @@ -48,7 +50,7 @@ Use [black](https://github.com/ambv/black) to make sure the code follows the sty ## Test your code modification -This custom component is based on [integration_blueprint template](https://github.com/ludeeus/integration_blueprint). +This custom component is based on [ludeeus/integration_blueprint](https://github.com/ludeeus/integration_blueprint). It comes with development environment in a container, easy to launch if you use Visual Studio Code. With this container you will have a stand alone diff --git a/README.md b/README.md index 2922115..f8deca6 100644 --- a/README.md +++ b/README.md @@ -10,27 +10,29 @@ # hass-wattbox -_Home Assistant Component to integrate with [WattBox][wattbox]._ +[Home Assistant](home-assistant) Custom Component to integrate with [WattBox][wattbox]. Easiest way to install this component is through [HACS][hacs]. -Configuration through `configuration.yaml`, not available in UI yet. +Configuration through `configuration.yaml`. UI based configuration not available yet. Example Config: ```yaml wattbox: -- host: 192.168.1.100 - name: wattbox1 - username: username1 - password: password1 +- name: WattBox-HTTP + host: !secret wattbox1_ip + port: 80 + username: !secret wattbox1_username + password: !secret wattbox1_password name_regexp: "^Fixed Prefix (.*)$" skip_regexp: "SKIP" scan_interval: 00:00:10 -- host: 192.168.1.101 - name: wattbox2 - username: username2 - password: password2 +- name: WattBox-SSH + host: !secret wattbox2_ip + port: 22 + username: !secret wattbox2_username + password: !secret wattbox2_password scan_interval: 00:00:20 resources: - auto_reboot @@ -43,15 +45,15 @@ wattbox: Configuration Options: -- _host_: Host IP of the WattBox (Required) -- _port_: Port of the HTTP interface (Default 80) -- _username_: Username for authentication (Default wattbox) -- _password_: Password for authentication (Default wattbox) -- _name_: Name for the WattBox (Default wattbox) -- _resources_: A list of resources to enable (Default all of them) -- _scan_interval_: A time interval run updates at (Default 30s, format HH:MM:SS) -- _name_regexp_: A regexp to extract the name to use for the outlet instead of just the index. If there is a match group, it is used, else the whole match is used. -- _skip_regexp_: A regexp to use that, if the outlet name matches, the outlet is not added as a switch entity. +- **`host`**: Host IP of the WattBox (Required) +- **`name`**: Name for the WattBox (Default WattBox) +- **`port`**: Port of the HTTP interface (Default 80) +- **`username`**: Username for authentication (Default wattbox) +- **`password`**: Password for authentication (Default wattbox) +- **`scan_interval`**: A time interval run updates at (Default 30s, format HH:MM:SS) +- **`resources`**: A list of resources to enable (Default all of them) +- **`name_regexp`**: A regexp to extract the name to use for the outlet instead of just the index. If there is a match group, it is used, else the whole match is used. +- **`skip_regexp`**: A regexp to use that, if the outlet name matches, the outlet is not added as a switch entity. Resources: @@ -71,12 +73,12 @@ Resources: - power_value - voltage_value -Be careful, if the WattBox controls the power to its own networking equipment you can turn it off and not have remote access until you fix it. You may even have to plug it in elsewhere to get back online and turn that outlet back on in HA. You can use the _skip_regexp_ option for those outlets. +Be careful, if the WattBox controls the power to its own networking equipment you can turn it off and not have remote access until you fix it. You may even have to plug it in elsewhere to get back online and turn that outlet back on in HA. You can use the `skip_regexp` option for those outlets. -Master switch will turn on / off all the switches that the physical switch on the box does. You can config that through the UI on the wattbox directly. If ALL of the switches controlled by Master are on, then Master will be on. Otherwise it will be off. If any outlets on a wattbox are skipped via _skip_regexp_ then +Master switch will turn on / off all the switches that the physical switch on the box does. You can config that through the UI on the wattbox directly. If ALL of the switches controlled by Master are on, then Master will be on. Otherwise it will be off. If any outlets on a wattbox are skipped via `skip_regexp` then the master switch for that wattbox will also not be added as an entity. -Based on [integration_blueprint template][blueprint] +Based on: [ludeeus/integration_blueprint][blueprint] @@ -97,3 +99,4 @@ Based on [integration_blueprint template][blueprint] [maintenance-shield]: https://img.shields.io/badge/maintainer-Erik%20Seglem%20%40Bedon292-blue [hacs]: https://github.com/custom-components/hacs [hacsbadge]: https://img.shields.io/badge/HACS-Custom-orange +[home-assistant]: https://github.com/home-assistant/core diff --git a/custom_components/wattbox/const.py b/custom_components/wattbox/const.py index cd82a7a..3771fc1 100644 --- a/custom_components/wattbox/const.py +++ b/custom_components/wattbox/const.py @@ -18,7 +18,9 @@ PLATFORMS: Final[List[str]] = ["binary_sensor", "sensor", "switch"] ISSUE_URL: Final[str] = "https://github.com/eseglem/hass-wattbox/issues" -STARTUP: Final[str] = f""" +STARTUP: Final[ + str +] = f""" ------------------------------------------------------------------- {DOMAIN} Version: {VERSION} @@ -42,8 +44,8 @@ TOPIC_UPDATE: Final[str] = "{}_data_update_{}" # config options -CONF_NAME_REGEXP: Final[str] = 'name_regexp' -CONF_SKIP_REGEXP: Final[str] = 'skip_regexp' +CONF_NAME_REGEXP: Final[str] = "name_regexp" +CONF_SKIP_REGEXP: Final[str] = "skip_regexp" class _BinarySensorDict(TypedDict): diff --git a/custom_components/wattbox/manifest.json b/custom_components/wattbox/manifest.json index b628951..993b897 100644 --- a/custom_components/wattbox/manifest.json +++ b/custom_components/wattbox/manifest.json @@ -5,7 +5,9 @@ "integration_type": "device", "documentation": "https://github.com/eseglem/hass-wattbox", "issue_tracker": "https://github.com/eseglem/hass-wattbox/issues", - "dependencies": [], + "after_dependencies": [ + "component" + ], "codeowners": [ "@eseglem" ], diff --git a/requirements.txt b/requirements.txt index 1e531e1..2de5175 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,8 @@ -homeassistant==2023.8.0 -homeassistant-stubs==2023.8.0 -pip>=21.0,<23.2 +homeassistant==2023.11.0 +homeassistant-stubs==2023.11.0 pywattbox [http, ip] @ git+https://github.com/eseglem/pywattbox@develop#pywattbox==0.6.0 ruff +pre-commit +# Issue with default_config, python3.11, and botocore +# See: https://github.com/home-assistant/core/issues/95192 +botocore @ git+https://github.com/boto/botocore diff --git a/scripts/lint b/scripts/lint index 9b5b1df..90c3a82 100755 --- a/scripts/lint +++ b/scripts/lint @@ -4,4 +4,4 @@ set -e cd "$(dirname "$0")/.." -ruff check . --fix +pre-commit run --all