Skip to content

Commit 0fdfaed

Browse files
committed
MQTT entity upgrade
1 parent 0784386 commit 0fdfaed

7 files changed

Lines changed: 147 additions & 158 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ on:
2525
- ".github/workflows/ci.yml"
2626

2727
env:
28-
RUFF_VERSION: 0.15.4
29-
UV_VERSION: 0.10.2
28+
RUFF_VERSION: 0.16.5
29+
UV_VERSION: 0.12.1
3030

3131
jobs:
3232
ruff:

‎pyproject.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[build-system]
22
build-backend = "uv_build"
3-
requires = [ "uv-build>=0.11,<0.12" ]
3+
requires = [ "uv-build>=0.12,<0.14" ]
44

55
[project]
66
name = "sunsynk"
@@ -31,7 +31,7 @@ classifiers = [
3131
dependencies = [
3232
"cattrs>=25,<27",
3333
"modbus-connection[pymodbus,tmodbus]>=4.7",
34-
"mqtt-entity[options]>=1.1",
34+
"mqtt-entity[options]>=1.2.5",
3535
"prettytable>=3.17,<4",
3636
"whenever>=0.10,<11",
3737
]

‎scripts/copy2local.sh‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ print() {
1818

1919
# rsync patterns equivalent to scripts/copyexclude.txt (xcopy substring match)
2020
rsync_excl() {
21-
rsync -a \
21+
rsync -a --inplace \
2222
--exclude '__pycache__' \
2323
--exclude '*.egg-info' \
2424
--exclude '.*' \
@@ -57,7 +57,7 @@ copy_builder() {
5757
print "Copy builder files for '$1'"
5858
mkdir -p "$DEST/$1"
5959
cp -f hass-addon-sunsynk-edge/Dockerfile "$DEST/$1/"
60-
rsync -a hass-addon-sunsynk-edge/rootfs/ "$DEST/$1/rootfs/"
60+
rsync -a --inplace hass-addon-sunsynk-edge/rootfs/ "$DEST/$1/rootfs/"
6161
echo 0.0.0 > "$DEST/$1/VERSION"
6262
}
6363

‎src/ha_addon_sunsynk_multi/__main__.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from collections import defaultdict
77
from pathlib import Path
88

9-
from ha_addon_sunsynk_multi.driver import HASS_DISCOVERY_INFO_UPDATE_QUEUE
109
from sunsynk import VERSION
1110
from sunsynk.utils import pretty_table_sensors
1211

@@ -55,8 +54,6 @@ async def main_loop() -> int:
5554
for ist in STATE:
5655
ist.init_sensors()
5756

58-
HASS_DISCOVERY_INFO_UPDATE_QUEUE.clear()
59-
6057
asyncio.get_event_loop().set_debug(OPT.debug > 0)
6158

6259
# MQTT broker LWT + connect "online": SS/availability_<sorted HA_PREFIX list joined by _>
@@ -69,10 +66,10 @@ async def main_loop() -> int:
6966
AsyncCallback(name="discovery_info", every=5, callback=callback_discovery_info)
7067
)
7168

72-
for ist in STATE:
69+
for idx, ist in enumerate(STATE):
7370
try:
7471
await ist.connect()
75-
await ist.hass_discover_sensors()
72+
await ist.hass_discover_sensors(mqtt_connect=idx == 0)
7673
build_callback_schedule(ist)
7774
CALLBACKS.append(ist.cb)
7875

‎src/ha_addon_sunsynk_multi/a_inverter.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,12 @@ def hass_create_discovery_info(self) -> None:
353353
except Exception as err:
354354
_LOG.error("Could not create MQTT entity for %s: %s", s, err)
355355

356-
async def hass_discover_sensors(self) -> bool:
356+
async def hass_discover_sensors(self, *, mqtt_connect: bool = False) -> bool:
357357
"""Discover all sensors."""
358358
self.hass_create_discovery_info()
359-
await MQTT.connect(OPT)
360-
MQTT.monitor_homeassistant_status()
359+
if mqtt_connect:
360+
await MQTT.connect(OPT)
361+
await MQTT.monitor_homeassistant_status()
361362
if self.entity_near_realtime is not None:
362363
await self.entity_near_realtime.send_state(MQTT, "OFF", retain=True)
363364
return True

‎src/ha_addon_sunsynk_multi/driver.py‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,14 @@
1010
from sunsynk.sunsynk import HoldingUnit
1111

1212
from .a_inverter import STATE, AInverter
13-
from .a_sensor import MQTT
1413
from .options import InverterOptions, Options, is_solarman_port
1514
from .sensor_options import SOPT
1615

1716
_LOG = logging.getLogger(":")
1817

1918

20-
HASS_DISCOVERY_INFO_UPDATE_QUEUE: set[Sensor] = set()
21-
"""Update Sensor discovery info."""
22-
23-
2419
async def callback_discovery_info(now: int) -> None:
25-
"""Update HASS discovery & write RWSensors."""
26-
# Flush any pending discovery info updates
27-
if HASS_DISCOVERY_INFO_UPDATE_QUEUE:
28-
for ist in STATE:
29-
ist.hass_create_discovery_info()
30-
await MQTT.publish_discovery_info()
31-
HASS_DISCOVERY_INFO_UPDATE_QUEUE.clear()
32-
33-
# Publish statistics
20+
"""Publish inverter callback statistics."""
3421
if now % 120 == 0:
3522
for ist in STATE:
3623
await ist.publish_stats(120)
@@ -41,11 +28,15 @@ def sensor_on_update(sen: Sensor, _new: ValType, _old: ValType) -> None:
4128
if sen not in SOPT or not SOPT[sen].affects:
4229
return
4330
_LOG.debug(
44-
"%s changed: Enqueue discovery info updates for %s",
31+
"%s changed: Rebuild discovery for %s",
4532
sen.name,
4633
", ".join(s.id for s in SOPT[sen].affects),
4734
)
48-
HASS_DISCOVERY_INFO_UPDATE_QUEUE.update(SOPT[sen].affects)
35+
for ist in STATE:
36+
if ist.mqtt_dev.id == "":
37+
continue
38+
ist.hass_create_discovery_info()
39+
ist.mqtt_dev.flag_discovery()
4940

5041

5142
def _shared_modbus_connection(opt: Options, *, port: str) -> ModbusConnection:

0 commit comments

Comments
 (0)