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

Dynamically load sensor definitions. Pin ruff to 0.9.3 #417

Merged
merged 3 commits into from
Jan 24, 2025
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 6 additions & 5 deletions .github/workflows/deploy-multi-mp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }}
Expand All @@ -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' &&
Expand Down
190 changes: 0 additions & 190 deletions .github/workflows/deploy-multi.yml

This file was deleted.

6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)/"
Expand Down
9 changes: 5 additions & 4 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 2 additions & 14 deletions src/ha_addon_sunsynk_multi/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 4 additions & 19 deletions src/ha_addon_sunsynk_multi/sensor_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -115,22 +113,9 @@ def init_sensors(self) -> None:

def import_definitions() -> None:
"""Load definitions according to options."""
DEFS.all.clear()
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
defs = import_defs(OPT.sensor_definitions)
DEFS.all = defs.all
DEFS.deprecated = defs.deprecated

# Add custom sensors to DEFS
try:
Expand Down
16 changes: 16 additions & 0 deletions src/sunsynk/definitions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""Sensor definitions."""

import logging

from sunsynk.sensors import (
EnumSensor,
ProtocolVersionSensor,
SensorDefinitions,
SerialSensor,
)
from sunsynk.utils import import_module

COMMON = SensorDefinitions()

Expand Down Expand Up @@ -38,3 +41,16 @@
ProtocolVersionSensor(2, "Protocol"),
SerialSensor((3, 4, 5, 6, 7), "Serial"),
)


def import_defs(name: str) -> SensorDefinitions:
"""Import defs."""
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")
2 changes: 1 addition & 1 deletion src/sunsynk/definitions/three_phase_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)
############
Expand Down
Loading
Loading