Skip to content

Commit 4623aaf

Browse files
committed
fix(status): propagate offline poll updates
1 parent b276826 commit 4623aaf

4 files changed

Lines changed: 22 additions & 9 deletions

File tree

custom_components/ecoflow_cloud/devices/data_holder.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ def add_status(self, data: PreparedData):
108108
self.__accept_prepared_data(data, self.set_status.append)
109109
self.set_status_time = dt.utcnow()
110110

111+
def mark_status_changed(self) -> None:
112+
self.status_change_time = dt.utcnow()
113+
111114
def add_data(self, data: PreparedData):
112115
if data.params is not None and self.module_sn is not None:
113116
if "moduleSn" not in data.params:

custom_components/ecoflow_cloud/devices/status_coordinator.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
1717

1818
from ..api import EcoflowApiClient
19+
from .data_coordinator import EcoflowBroadcastDataHolder
1920

2021
_LOGGER = logging.getLogger(__name__)
2122

@@ -44,9 +45,7 @@ def unregister(self, entry_id: str) -> None:
4445

4546
def _actualize_interval(self) -> None:
4647
interval = min(
47-
(d.device_data.options.assume_offline_sec
48-
for c in self._clients.values()
49-
for d in c.devices.values()),
48+
(d.device_data.options.assume_offline_sec for c in self._clients.values() for d in c.devices.values()),
5049
default=DEFAULT_STATUS_POLL_INTERVAL_SEC,
5150
)
5251
self.update_interval = datetime.timedelta(seconds=interval)
@@ -80,9 +79,7 @@ async def _async_update_data(self) -> None:
8079

8180
# Only poll if at least one device needs status clarification
8281
needs_poll = any(
83-
device.status_tracker.wants_status_poll
84-
for devices in all_devices.values()
85-
for device in devices
82+
device.status_tracker.wants_status_poll for devices in all_devices.values() for device in devices
8683
)
8784
if not needs_poll:
8885
_LOGGER.debug("No devices need status poll — skipping")
@@ -98,4 +95,7 @@ async def _async_update_data(self) -> None:
9895

9996
for api_device in api_devices:
10097
for device in all_devices.get(api_device.sn, []):
101-
device.status_tracker.on_explicit_status(api_device.status == 1)
98+
online = api_device.status == 1
99+
device.status_tracker.on_explicit_status(online)
100+
device.data.mark_status_changed()
101+
device.coordinator.async_set_updated_data(EcoflowBroadcastDataHolder(device.data, online))

custom_components/ecoflow_cloud/entities/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ def _updated(self, data: dict[str, Any]):
175175
# update value
176176
values = self._mqtt_key_expr.find(data)
177177
if len(values) == 1 or (len(values) > 1 and self._multiple_value_sum):
178+
was_available = bool(self._attr_available)
178179
self._attr_available = True
179180
if self._auto_enable:
180181
self._attr_entity_registry_enabled_default = True
@@ -184,7 +185,7 @@ def _updated(self, data: dict[str, Any]):
184185
if len(values) > 1 and self._multiple_value_sum:
185186
for v in values[1:]:
186187
total += v.value
187-
if self._update_value(total):
188+
if self._update_value(total) or not was_available:
188189
self.schedule_update_ha_state()
189190

190191
@property

custom_components/ecoflow_cloud/sensor.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class DcModeStateSensorEntity(MiscSensorEntity):
135135
def __init__(self, *args, **kwargs):
136136
super().__init__(*args, **kwargs)
137137
self._raw_code: int | None = None
138+
self._configured_code: int | None = None
138139

139140
def _runtime_mode(self) -> tuple[int | None, str | None]:
140141
params = self._device.data.params
@@ -189,8 +190,16 @@ def _updated(self, data: dict[str, Any]):
189190
if runtime_code is None:
190191
return
191192

193+
configured_code = params.get("mppt.cfgChgType")
194+
if configured_code is not None:
195+
configured_code = int(configured_code)
196+
197+
was_available = bool(self._attr_available)
192198
self._attr_available = True
193-
if self._update_value(runtime_code):
199+
updated = self._update_value(runtime_code)
200+
configured_changed = configured_code != self._configured_code
201+
self._configured_code = configured_code
202+
if updated or configured_changed or not was_available:
194203
self.schedule_update_ha_state()
195204

196205

0 commit comments

Comments
 (0)