fixed resolver

This commit is contained in:
2026-05-09 10:18:57 -04:00
parent a1acf479f0
commit 8c7b5fb711
3 changed files with 70 additions and 11 deletions

View File

@@ -88,6 +88,11 @@ class MQTTConfig:
username: str
password: str
discovery_prefix: str = "homeassistant"
# Periodically re-publish (retain=True) every discovery config we've ever
# sent, so HA recovers automatically if it loses the entity registration —
# broker restart that purged retained messages, HA missed the initial
# publish, integration glitch. Set to 0 to disable.
discovery_republish_interval_s: float = 1800.0
@dataclasses.dataclass
@@ -751,6 +756,7 @@ class MQTTPublisher:
self._dry_run = dry_run
self._client: mqtt.Client | None = None
self._discovered: set[tuple[str, str]] = set()
self._last_discovery_republish_at: float = time.monotonic()
if not dry_run:
c = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, client_id="eg4-battery")
c.username_pw_set(cfg.username, cfg.password)
@@ -776,6 +782,22 @@ class MQTTPublisher:
else:
self._client.publish(state_topic, payload, qos=0, retain=False)
def maybe_republish_discovery(self) -> None:
"""Heartbeat: re-emit every previously sent discovery config if the
configured interval has elapsed. Idempotent (retain=True), so HA
re-picks up any registrations it has lost without operator action."""
interval = self._cfg.discovery_republish_interval_s
if interval <= 0 or not self._discovered:
return
now = time.monotonic()
if now - self._last_discovery_republish_at < interval:
return
for pack_name, key in self._discovered:
state_topic = f"{self._cfg.discovery_prefix}/sensor/{pack_name}_{key}/state"
self._publish_discovery(pack_name, key, state_topic)
self._last_discovery_republish_at = now
log.info("re-published %d discovery configs (heartbeat)", len(self._discovered))
def _publish_discovery(self, pack_name: str, key: str, state_topic: str) -> None:
unit, device_class, state_class, icon = field_meta(key)
cfg = {
@@ -866,6 +888,7 @@ def run_active(transport: ActiveTransport, publisher: MQTTPublisher, cfg: AppCon
st.ok = False
st.last_error_category = category
st.consecutive_errors += 1
publisher.maybe_republish_discovery()
if one_cycle:
return
elapsed = time.monotonic() - cycle_start
@@ -881,6 +904,7 @@ def run_passive(listener: PassiveListener, publisher: MQTTPublisher, cfg: AppCon
n = 0
for frame in listener.frames():
n += 1
publisher.maybe_republish_discovery()
if trace:
log.debug("%r raw=%s", frame, frame.raw.hex(" "))
if frame.kind != "response" or frame.function != 0x03:
@@ -969,6 +993,7 @@ def run_modbus_per_pack(cfg: AppConfig, publisher: MQTTPublisher,
st.ok = False
st.last_error_category = category
st.consecutive_errors += 1
publisher.maybe_republish_discovery()
if one_cycle:
return
elapsed = time.monotonic() - cycle_start

View File

@@ -33,6 +33,10 @@ mqtt:
username: <MQTT_USER>
password: <MQTT_PASSWORD>
discovery_prefix: homeassistant
# Re-publish every retained discovery config every N seconds so HA recovers
# automatically if it ever loses entity registrations (broker restart, missed
# initial publish, integration glitch). 0 disables. Default: 1800 (30 min).
# discovery_republish_interval_s: 1800
# One entry per pack. `name` is the HA entity prefix and device identifier.
# `address` is the EG4 7E protocol address in active mode (master = 1, slaves