From a4af4ed952f7843b4c9e9ddde96e9e11e6661fb2 Mon Sep 17 00:00:00 2001 From: Johann Kellerman Date: Fri, 24 Jan 2025 16:03:33 +0200 Subject: [PATCH 1/3] Dynamically load sensor definitions. Pin ruff to 0.9.3 --- .github/workflows/ci.yml | 4 +-- .pre-commit-config.yaml | 6 ++--- setup.cfg | 9 ++++--- src/ha_addon_sunsynk_multi/helpers.py | 16 ++---------- src/ha_addon_sunsynk_multi/sensor_options.py | 26 ++++++++----------- src/sunsynk/definitions/__init__.py | 7 +++++ src/sunsynk/definitions/three_phase_common.py | 2 +- src/sunsynk/definitions/three_phase_lv.py | 2 +- src/sunsynk/helpers.py | 26 +++++++++---------- src/sunsynk/rwsensors.py | 4 ++- src/sunsynk/sensors.py | 6 +++-- src/sunsynk/state.py | 2 +- src/sunsynk/utils.py | 18 +++++++++++++ .../ha_addon_sunsynk_multi/test_versions.py | 1 + 14 files changed, 72 insertions(+), 57 deletions(-) create mode 100644 src/sunsynk/utils.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1baea795..fc1e183b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,10 +29,10 @@ jobs: - uses: actions/checkout@v4 - uses: chartboost/ruff-action@v1 with: - version: 0.6.3 + version: 0.9.3 - uses: chartboost/ruff-action@v1 with: - version: 0.6.3 + version: 0.9.3 args: "format --check" lint: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e2848f5b..e9a29c57 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,13 +1,13 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.6.3 + rev: v0.9.3 hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] - id: ruff-format - repo: https://github.com/codespell-project/codespell - rev: v2.3.0 + rev: v2.4.0 hooks: - id: codespell files: "src/.*.py" @@ -25,7 +25,7 @@ repos: # - "package-lock.json,*.map,www/docs/.vitepress/cache/*" - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.11.2 + rev: v1.14.1 hooks: # - id: mypy # files: "^(sunsynk|hass-addon-multi)/" diff --git a/setup.cfg b/setup.cfg index cae6fff2..cd7b1f8c 100644 --- a/setup.cfg +++ b/setup.cfg @@ -47,15 +47,16 @@ pymodbus = solarman = pysolarmanv5==3.0.6 tests = + aiohttp # Used for ESP + mypy + pylint pytest pytest-asyncio pytest-cov pytest-github-actions-annotate-failures - types-PyYAML + ruff==0.9.3 types-jmespath - pylint - aiohttp # Used for ESP - mypy + types-PyYAML [pylint.FORMAT] max-line-length = 121 diff --git a/src/ha_addon_sunsynk_multi/helpers.py b/src/ha_addon_sunsynk_multi/helpers.py index 27357ae7..726b7dd4 100644 --- a/src/ha_addon_sunsynk_multi/helpers.py +++ b/src/ha_addon_sunsynk_multi/helpers.py @@ -2,25 +2,13 @@ import logging import os -import sys import traceback -from importlib import import_module as _import_module from pathlib import Path -from types import ModuleType from typing import Any -_LOGGER = logging.getLogger(__name__) - +from sunsynk.utils import import_module -def import_module(mod_name: str, folder: str) -> ModuleType: - """import_module.""" - here = Path(os.getcwd()) / folder - sys.path.insert(0, str(here)) - try: - mod_obj = _import_module(mod_name) - return mod_obj - finally: - sys.path.pop(0) +_LOGGER = logging.getLogger(__name__) def get_root(create: bool = False) -> Path: diff --git a/src/ha_addon_sunsynk_multi/sensor_options.py b/src/ha_addon_sunsynk_multi/sensor_options.py index 1b5a94ad..6bf05373 100644 --- a/src/ha_addon_sunsynk_multi/sensor_options.py +++ b/src/ha_addon_sunsynk_multi/sensor_options.py @@ -9,9 +9,7 @@ from ha_addon_sunsynk_multi.helpers import import_mysensors from ha_addon_sunsynk_multi.options import OPT from ha_addon_sunsynk_multi.timer_schedule import SCHEDULES, Schedule, get_schedule -from sunsynk.definitions.single_phase import SENSORS as SENSORS_1PH -from sunsynk.definitions.three_phase_hv import SENSORS as SENSORS_3PHV -from sunsynk.definitions.three_phase_lv import SENSORS as SENSORS_3PHLV +from sunsynk.definitions import import_defs from sunsynk.helpers import slug from sunsynk.rwsensors import RWSensor from sunsynk.sensors import Sensor, SensorDefinitions @@ -119,18 +117,16 @@ def import_definitions() -> None: DEFS.deprecated.clear() # Load DEFS - if OPT.sensor_definitions == "three-phase": - _LOGGER.info("Using three phase sensor definitions.") - DEFS.all = dict(SENSORS_3PHLV.all) - DEFS.deprecated = SENSORS_3PHLV.deprecated - elif OPT.sensor_definitions == "three-phase-hv": - _LOGGER.info("Using three phase HV sensor definitions.") - DEFS.all = dict(SENSORS_3PHV.all) - DEFS.deprecated = SENSORS_3PHV.deprecated - else: - _LOGGER.info("Using Single phase sensor definitions.") - DEFS.all = dict(SENSORS_1PH.all) - DEFS.deprecated = SENSORS_1PH.deprecated + name = { + "three-phase": "three_phase_lv", + }.get(OPT.sensor_definitions) or OPT.sensor_definitions.replace("-", "_") + _LOGGER.info( + "Using the sensor definitions from https://github.com/kellerza/sunsynk/tree/main/src/sunsynk/definitions/%s.py", + name, + ) + defs = import_defs(name) + DEFS.all = dict(defs.all) + DEFS.deprecated = defs.deprecated # Add custom sensors to DEFS try: diff --git a/src/sunsynk/definitions/__init__.py b/src/sunsynk/definitions/__init__.py index 5cc64003..e1ca5872 100644 --- a/src/sunsynk/definitions/__init__.py +++ b/src/sunsynk/definitions/__init__.py @@ -6,6 +6,7 @@ SensorDefinitions, SerialSensor, ) +from sunsynk.utils import import_module COMMON = SensorDefinitions() @@ -38,3 +39,9 @@ ProtocolVersionSensor(2, "Protocol"), SerialSensor((3, 4, 5, 6, 7), "Serial"), ) + + +def import_defs(name: str) -> SensorDefinitions: + """Import defs.""" + mod = import_module(f"sunsynk.definitions.{name}") + return getattr(mod, "SENSORS") diff --git a/src/sunsynk/definitions/three_phase_common.py b/src/sunsynk/definitions/three_phase_common.py index 94b0adc9..942ed923 100644 --- a/src/sunsynk/definitions/three_phase_common.py +++ b/src/sunsynk/definitions/three_phase_common.py @@ -540,7 +540,7 @@ }, bitmask=0b11 << 14, ), - SwitchRWSensor(235,"Track Grid Phase",on=1), + SwitchRWSensor(235, "Track Grid Phase", on=1), NumberRWSensor(209, "UPS delay time", "s"), ) ############ diff --git a/src/sunsynk/definitions/three_phase_lv.py b/src/sunsynk/definitions/three_phase_lv.py index be663414..1962eb93 100644 --- a/src/sunsynk/definitions/three_phase_lv.py +++ b/src/sunsynk/definitions/three_phase_lv.py @@ -2,11 +2,11 @@ from sunsynk import AMPS, CELSIUS, VOLT, WATT from sunsynk.definitions.three_phase_common import SENSORS +from sunsynk.rwsensors import SelectRWSensor from sunsynk.sensors import ( Sensor, TempSensor, ) -from sunsynk.rwsensors import SelectRWSensor SENSORS = SENSORS.copy() diff --git a/src/sunsynk/helpers.py b/src/sunsynk/helpers.py index 8ed556b6..e1c27b6d 100644 --- a/src/sunsynk/helpers.py +++ b/src/sunsynk/helpers.py @@ -2,8 +2,8 @@ import logging import math -from typing import Any import struct +from typing import Any _LOGGER = logging.getLogger(__name__) @@ -15,41 +15,41 @@ def pack_value(value: int, bits: int = 16, signed: bool = True) -> RegType: """Pack a value into register format. - + Args: value: The value to pack bits: Number of bits (16 or 32) signed: Whether the value should be treated as signed - + Returns: For 16-bit: single register value For 32-bit: tuple of (low, high) register values """ if bits == 16: - fmt = 'h' if signed else 'H' - return struct.unpack('H', struct.pack(fmt, value)) + fmt = "h" if signed else "H" + return struct.unpack("H", struct.pack(fmt, value)) if bits == 32: - fmt = 'i' if signed else 'I' - return struct.unpack('2H', struct.pack(fmt, value)) + fmt = "i" if signed else "I" + return struct.unpack("2H", struct.pack(fmt, value)) raise ValueError(f"Unsupported number of bits: {bits}") def unpack_value(regs: RegType, signed: bool = True) -> int: """Unpack register value(s) into an integer. - + Args: regs: Register values (1 or 2 registers) signed: Whether to treat as signed value - + Returns: Unpacked integer value """ if len(regs) == 1: - fmt = 'h' if signed else 'H' - return struct.unpack(fmt, struct.pack('H', regs[0]))[0] + fmt = "h" if signed else "H" + return struct.unpack(fmt, struct.pack("H", regs[0]))[0] if len(regs) == 2: - fmt = 'i' if signed else 'I' - return struct.unpack(fmt, struct.pack('2H', regs[0], regs[1]))[0] + fmt = "i" if signed else "I" + return struct.unpack(fmt, struct.pack("2H", regs[0], regs[1]))[0] raise ValueError(f"Unsupported number of registers: {len(regs)}") diff --git a/src/sunsynk/rwsensors.py b/src/sunsynk/rwsensors.py index e5f405d6..c7d5f83b 100644 --- a/src/sunsynk/rwsensors.py +++ b/src/sunsynk/rwsensors.py @@ -83,7 +83,9 @@ def value_to_reg(self, value: ValType, resolve: ResolveType) -> RegType: minv = resolve_num(resolve, self.min, 0) maxv = resolve_num(resolve, self.max, 100) val = int(max(minv, min(maxv, fval)) / abs(self.factor)) - return self.reg(*pack_value(val, bits=len(self.address)*16, signed=self.factor < 0)) + return self.reg( + *pack_value(val, bits=len(self.address) * 16, signed=self.factor < 0) + ) @attrs.define(slots=True, eq=False) diff --git a/src/sunsynk/sensors.py b/src/sunsynk/sensors.py index daff913a..5815f9d4 100644 --- a/src/sunsynk/sensors.py +++ b/src/sunsynk/sensors.py @@ -7,9 +7,9 @@ import attrs from sunsynk.helpers import ( + NumType, RegType, ValType, - NumType, ensure_tuple, int_round, slug, @@ -137,7 +137,9 @@ class MathSensor(Sensor): def reg_to_value(self, regs: RegType) -> ValType: """Calculate the math value.""" - val = int_round(sum(unpack_value((i,), signed=True) * s for i, s in zip(regs, self.factors))) + val = int_round( + sum(unpack_value((i,), signed=True) * s for i, s in zip(regs, self.factors)) + ) if self.absolute and val < 0: val = -val if self.no_negative and val < 0: diff --git a/src/sunsynk/state.py b/src/sunsynk/state.py index 9182a5d1..9b7ea7bb 100644 --- a/src/sunsynk/state.py +++ b/src/sunsynk/state.py @@ -6,9 +6,9 @@ import attr +from sunsynk.helpers import NumType from sunsynk.rwsensors import RWSensor from sunsynk.sensors import BinarySensor, Sensor, ValType -from sunsynk.helpers import NumType _LOGGER = logging.getLogger(__name__) diff --git a/src/sunsynk/utils.py b/src/sunsynk/utils.py new file mode 100644 index 00000000..d745e520 --- /dev/null +++ b/src/sunsynk/utils.py @@ -0,0 +1,18 @@ +"""Utilities.""" + +import sys +from importlib import import_module as _import_module +from pathlib import Path +from types import ModuleType + + +def import_module(mod_name: str, folder: str | Path | None = None) -> ModuleType: + """import_module.""" + if folder: + sys.path.insert(0, str(folder)) + try: + mod_obj = _import_module(mod_name) + return mod_obj + finally: + if folder and sys.path[0] == str(folder): + sys.path.pop(0) diff --git a/src/tests/ha_addon_sunsynk_multi/test_versions.py b/src/tests/ha_addon_sunsynk_multi/test_versions.py index 241705be..4a3def97 100644 --- a/src/tests/ha_addon_sunsynk_multi/test_versions.py +++ b/src/tests/ha_addon_sunsynk_multi/test_versions.py @@ -64,5 +64,6 @@ def test_deps() -> None: filename="setup.cfg", regex=regex, ) + v_setup = [v for v in v_setup if not v.startswith("ruff")] assert " ".join(sorted(v_setup)) == " ".join(sorted(v_docker)) From b9d606669587bd4b1d30ce4e8b18362116157911 Mon Sep 17 00:00:00 2001 From: Johann Kellerman Date: Fri, 24 Jan 2025 16:46:03 +0200 Subject: [PATCH 2/3] test_import_defs --- .../test_definitions3ph.py | 11 ----------- src/tests/sunsynk/definitions/__init__.py | 1 + src/tests/sunsynk/definitions/test_init.py | 16 ++++++++++++++++ 3 files changed, 17 insertions(+), 11 deletions(-) delete mode 100644 src/tests/ha_addon_sunsynk_multi/test_definitions3ph.py create mode 100644 src/tests/sunsynk/definitions/__init__.py create mode 100644 src/tests/sunsynk/definitions/test_init.py diff --git a/src/tests/ha_addon_sunsynk_multi/test_definitions3ph.py b/src/tests/ha_addon_sunsynk_multi/test_definitions3ph.py deleted file mode 100644 index 68a329ae..00000000 --- a/src/tests/ha_addon_sunsynk_multi/test_definitions3ph.py +++ /dev/null @@ -1,11 +0,0 @@ -"""Test 3ph defs.""" - -from sunsynk.definitions.single_phase import SENSORS as SENSORS1P -from sunsynk.definitions.three_phase_common import SENSORS as SENSORS3P - - -def test_ok() -> None: - """Test.""" - assert len(SENSORS1P.serial.address) == len(SENSORS3P.serial.address) - assert SENSORS1P.rated_power - assert SENSORS3P.rated_power diff --git a/src/tests/sunsynk/definitions/__init__.py b/src/tests/sunsynk/definitions/__init__.py new file mode 100644 index 00000000..e4390193 --- /dev/null +++ b/src/tests/sunsynk/definitions/__init__.py @@ -0,0 +1 @@ +"""Sensor definitions.""" diff --git a/src/tests/sunsynk/definitions/test_init.py b/src/tests/sunsynk/definitions/test_init.py new file mode 100644 index 00000000..6a347fc6 --- /dev/null +++ b/src/tests/sunsynk/definitions/test_init.py @@ -0,0 +1,16 @@ +"""Test sensor definitions.""" + +from sunsynk.definitions import import_defs + + +def test_import_defs() -> None: + """Test importing sensors.""" + libs = ( + "single_phase", + "three_phase_hv", + "three_phase_lv", + ) + for lib in libs: + defs = import_defs(lib) + assert len(defs.all) > 50 + assert defs.rated_power From 1b9aa4de0c36475d42b0386538ec69f60d7170a1 Mon Sep 17 00:00:00 2001 From: Johann Kellerman Date: Fri, 24 Jan 2025 16:58:51 +0200 Subject: [PATCH 3/3] CI restrict to main --- .github/workflows/deploy-multi-mp.yml | 11 +- .github/workflows/deploy-multi.yml | 190 ------------------- src/ha_addon_sunsynk_multi/sensor_options.py | 15 +- src/sunsynk/definitions/__init__.py | 11 +- 4 files changed, 18 insertions(+), 209 deletions(-) delete mode 100644 .github/workflows/deploy-multi.yml diff --git a/.github/workflows/deploy-multi-mp.yml b/.github/workflows/deploy-multi-mp.yml index 32f071d9..bd1f9023 100644 --- a/.github/workflows/deploy-multi-mp.yml +++ b/.github/workflows/deploy-multi-mp.yml @@ -295,22 +295,22 @@ jobs: -t ${{ env.REGISTRY_IMAGE }}:${{ needs.information.outputs.version }} \ -t ${{ env.REGISTRY_IMAGE }}:${{ needs.information.outputs.environment }} \ $(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' "${digests[@]}") - + echo "Waiting 30 seconds after base manifest creation before processing platform-specific tags..." sleep 30 # Wait 30 seconds before processing platform-specific tags # Process each platform separately platforms="${{ steps.platforms_raw.outputs.value }}" IFS=',' read -ra PLATFORM_ARRAY <<< "$platforms" - + for PLATFORM in "${PLATFORM_ARRAY[@]}"; do echo "Processing platform: $PLATFORM" - + retry_with_backoff docker buildx imagetools create \ -t ${{ env.REGISTRY_IMAGE }}/${PLATFORM}:${{ needs.information.outputs.version }} \ -t ${{ env.REGISTRY_IMAGE }}/${PLATFORM}:${{ needs.information.outputs.environment }} \ $(printf '${{ env.REGISTRY_IMAGE }}@sha256:%s ' "${digests[@]}") - + echo "Completed manifest for $PLATFORM" echo "Waiting 30 seconds before processing next platform..." sleep 30 # Wait 30 seconds between platforms @@ -352,7 +352,7 @@ jobs: # Inspect each platform platforms="${{ steps.platforms_raw.outputs.value }}" IFS=',' read -ra PLATFORM_ARRAY <<< "$platforms" - + for PLATFORM in "${PLATFORM_ARRAY[@]}"; do echo "Inspecting platform: $PLATFORM" retry_with_backoff docker buildx imagetools inspect ${{ env.REGISTRY_IMAGE }}/${PLATFORM}:${{ needs.information.outputs.version }} @@ -363,6 +363,7 @@ jobs: publish_addon: name: ๐Ÿ†• Update addon version to ${{ needs.information.outputs.version }} needs: [information, build, merge] + branches: [main] if: | always() && needs.information.result == 'success' && diff --git a/.github/workflows/deploy-multi.yml b/.github/workflows/deploy-multi.yml deleted file mode 100644 index 5e090768..00000000 --- a/.github/workflows/deploy-multi.yml +++ /dev/null @@ -1,190 +0,0 @@ ---- -name: Deploy hass-addon-sunsynk-multi to ghcr.io - -# yamllint disable-line rule:truthy -on: - # On publish this job will start immediately - # release: - # types: - # - published - # workflow_run: - # workflows: ["CI"] - # branches: [main] - # types: - # - completed - workflow_dispatch: {} - push: - paths: - - ".github/workflows/deploy-multi.yml" - - "src/**" - - "hass-addon-sunsynk-edge/**" - - "hass-addon-sunsynk-multi/**" - -jobs: - # https://github.com/hassio-addons/workflows/blob/main/.github/workflows/addon-deploy.yaml - # https://github.com/frenck/action-addon-information - information: - name: Gather add-on information - runs-on: ubuntu-latest - outputs: - architectures: ${{ steps.information.outputs.architectures }} - build: ${{ steps.information.outputs.build }} - description: ${{ steps.information.outputs.description }} - environment: ${{ steps.release.outputs.environment }} - name: ${{ steps.information.outputs.name }} - slug: ${{ steps.information.outputs.slug }} - target: ${{ steps.information.outputs.target }} - version: ${{ steps.release.outputs.version }} - steps: - - name: โคต๏ธ Check out code from GitHub - uses: actions/checkout@v4 - - name: ๐Ÿš€ Run add-on information - id: information - uses: frenck/action-addon-information@v1.4 - with: - path: ./hass-addon-sunsynk-multi - - name: โ„น๏ธ Gather version and environment - id: release - run: | - sha="${{ github.sha }}" - environment="edge" - version="${sha:0:7}" - if [[ "${{ github.event_name }}" = "release" ]]; then - version="${{ github.event.release.tag_name }}" - version="${version,,}" - version="${version#v}" - environment="stable" - if [[ "${{ github.event.release.prerelease }}" = "true" ]]; then - environment="beta" - fi - fi - - echo "environment=${environment}" >> "$GITHUB_OUTPUT" - echo "version=${version}" >> "$GITHUB_OUTPUT" - - cat "$GITHUB_OUTPUT" - cat "$GITHUB_OUTPUT" >> $GITHUB_STEP_SUMMARY - - deploy: - name: ๐Ÿ‘ท Build & Deploy ${{ matrix.architecture }} ${{ needs.information.outputs.environment }}/${{ needs.information.outputs.version }} - needs: information - runs-on: ubuntu-latest - strategy: - matrix: - architecture: ${{ fromJson(needs.information.outputs.architectures) }} - steps: - - name: โคต๏ธ Check out code from GitHub - uses: actions/checkout@v4 - - name: Local copy of sunsynk - run: | - ls - echo $GITHUB_WORKSPACE - mkdir -p hass-addon-sunsynk-multi/sunsynk - cp -r src hass-addon-sunsynk-multi/sunsynk/ - cp setup.* README.md hass-addon-sunsynk-multi/sunsynk/ - - name: ๐Ÿ— Set up build cache - id: cache - uses: actions/cache@v4 - with: - path: /tmp/.docker-cache - key: docker-${{ matrix.architecture }}-${{ github.sha }} - restore-keys: | - docker-${{ matrix.architecture }} - - name: ๐Ÿ— Set up QEMU - uses: docker/setup-qemu-action@v3.2.0 - - name: ๐Ÿ— Set up Docker Buildx - uses: docker/setup-buildx-action@v3.6.1 - # - name: ๐Ÿ— Set up Codenotary Community Attestation Service (CAS) - # uses: frenck/action-setup-cas@v0.1.1 - # with: - # version: v1.0.3 - - name: โ„น๏ธ Compose build flags - id: flags - run: | - echo "date=$(date +"%Y-%m-%dT%H:%M:%SZ")" >> "$GITHUB_OUTPUT" - from=$(yq --no-colors eval ".build_from.${{ matrix.architecture }}" "${{ needs.information.outputs.build }}") - echo "from=${from}" >> "$GITHUB_OUTPUT" - - if [[ "${{ matrix.architecture}}" = "amd64" ]]; then - echo "platform=linux/amd64" >> "$GITHUB_OUTPUT" - elif [[ "${{ matrix.architecture }}" = "i386" ]]; then - echo "platform=linux/386" >> "$GITHUB_OUTPUT" - elif [[ "${{ matrix.architecture }}" = "armhf" ]]; then - echo "platform=linux/arm/v6" >> "$GITHUB_OUTPUT" - elif [[ "${{ matrix.architecture }}" = "armv7" ]]; then - echo "platform=linux/arm/v7" >> "$GITHUB_OUTPUT" - elif [[ "${{ matrix.architecture }}" = "aarch64" ]]; then - echo "platform=linux/arm64/v8" >> "$GITHUB_OUTPUT" - else - echo "::error ::Could not determine platform for architecture ${{ matrix.architecture }}" - exit 1 - fi - - name: ๐Ÿ— Login to GitHub Container Registry - uses: docker/login-action@v3.3.0 - with: - registry: ghcr.io - username: ${{ github.repository_owner }} - password: ${{ secrets.GITHUB_TOKEN }} - - name: โคต๏ธ Download base image - if: steps.flags.outputs.from != 'null' - run: docker pull "${{ steps.flags.outputs.from }}" - - name: ๐Ÿš€ Build - uses: docker/build-push-action@v6.7.0 - with: - load: true - # yamllint disable rule:line-length - tags: | - ghcr.io/${{ github.repository_owner }}/${{ needs.information.outputs.slug }}/${{ matrix.architecture }}:${{ needs.information.outputs.environment }} - ghcr.io/${{ github.repository_owner }}/${{ needs.information.outputs.slug }}/${{ matrix.architecture }}:${{ needs.information.outputs.version }} - # yamllint enable rule:line-length - context: ${{ needs.information.outputs.target }} - file: ${{ needs.information.outputs.target }}/Dockerfile - cache-from: | - type=local,src=/tmp/.docker-cache - ghcr.io/${{ github.repository_owner }}/${{ needs.information.outputs.slug }}/${{ matrix.architecture }}:edge - cache-to: type=local,mode=max,dest=/tmp/.docker-cache-new - platforms: ${{ steps.flags.outputs.platform }} - build-args: | - BUILD_ARCH=${{ matrix.architecture }} - BUILD_DATE=${{ steps.flags.outputs.date }} - BUILD_DESCRIPTION=${{ needs.information.outputs.description }} - BUILD_FROM=${{ steps.flags.outputs.from }} - BUILD_NAME=${{ needs.information.outputs.name }} - BUILD_REF=${{ github.sha }} - BUILD_REPOSITORY=${{ github.repository }} - BUILD_VERSION=${{ needs.information.outputs.version }} - # This ugly bit is necessary, or our cache will grow forever... - # Well until we hit GitHub's limit of 5GB :) - # https://github.com/docker/build-push-action/issues/252 - # https://github.com/moby/buildkit/issues/1896 - - name: ๐Ÿšš Swap build cache - run: | - rm -rf /tmp/.docker-cache - mv /tmp/.docker-cache-new /tmp/.docker-cache - - name: ๐Ÿš€ Push - run: | - docker push \ - "ghcr.io/${{ github.repository_owner }}/${{ needs.information.outputs.slug }}/${{ matrix.architecture }}:${{ needs.information.outputs.environment }}" - docker push \ - "ghcr.io/${{ github.repository_owner }}/${{ needs.information.outputs.slug }}/${{ matrix.architecture }}:${{ needs.information.outputs.version }}" - - publish_addon: - name: ๐Ÿ†• Update addon version to ${{ needs.information.outputs.version }} - needs: [information, deploy] - permissions: - contents: write - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - name: Point edge to the new ref - run: | - sed -i 's/version:.*/version: "${{ needs.information.outputs.version }}"/g' hass-addon-sunsynk-edge/config.yaml - - name: Point multi to the new ref - if: "${{ needs.information.outputs.environment == 'stable'}}" - run: | - sed -i 's/version:.*/version: "${{ needs.information.outputs.version }}"/g' hass-addon-sunsynk-multi/config.yaml - - uses: stefanzweifel/git-auto-commit-action@v5.0.1 - with: - branch: main - commit_user_email: kellerza@gmail.com - commit_message: Update addon version to ${{ needs.information.outputs.version }} [no ci] diff --git a/src/ha_addon_sunsynk_multi/sensor_options.py b/src/ha_addon_sunsynk_multi/sensor_options.py index 6bf05373..4a902923 100644 --- a/src/ha_addon_sunsynk_multi/sensor_options.py +++ b/src/ha_addon_sunsynk_multi/sensor_options.py @@ -113,19 +113,8 @@ def init_sensors(self) -> None: def import_definitions() -> None: """Load definitions according to options.""" - DEFS.all.clear() - DEFS.deprecated.clear() - - # Load DEFS - name = { - "three-phase": "three_phase_lv", - }.get(OPT.sensor_definitions) or OPT.sensor_definitions.replace("-", "_") - _LOGGER.info( - "Using the sensor definitions from https://github.com/kellerza/sunsynk/tree/main/src/sunsynk/definitions/%s.py", - name, - ) - defs = import_defs(name) - DEFS.all = dict(defs.all) + defs = import_defs(OPT.sensor_definitions) + DEFS.all = defs.all DEFS.deprecated = defs.deprecated # Add custom sensors to DEFS diff --git a/src/sunsynk/definitions/__init__.py b/src/sunsynk/definitions/__init__.py index e1ca5872..eade97cf 100644 --- a/src/sunsynk/definitions/__init__.py +++ b/src/sunsynk/definitions/__init__.py @@ -1,5 +1,7 @@ """Sensor definitions.""" +import logging + from sunsynk.sensors import ( EnumSensor, ProtocolVersionSensor, @@ -43,5 +45,12 @@ def import_defs(name: str) -> SensorDefinitions: """Import defs.""" - mod = import_module(f"sunsynk.definitions.{name}") + libname = {"three-phase": "three_phase_lv"}.get(name) or name.replace("-", "_") + logging.getLogger(__name__).info( + "Importing sensor definitions %s (view the source online: " + "https://github.com/kellerza/sunsynk/tree/main/src/sunsynk/definitions/%s.py )", + name, + libname, + ) + mod = import_module(f"sunsynk.definitions.{libname}") return getattr(mod, "SENSORS")