Skip to content

Commit b276826

Browse files
committed
fix(river2): mark telemetry unavailable offline
1 parent 7749a36 commit b276826

3 files changed

Lines changed: 26 additions & 7 deletions

File tree

custom_components/ecoflow_cloud/devices/data_holder.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ def on_data_received(self) -> None: ...
3636

3737

3838
class _NoOpStatusCallback:
39-
def on_explicit_status(self, online: bool) -> None: pass
40-
def on_data_received(self) -> None: pass
39+
def on_explicit_status(self, online: bool) -> None:
40+
pass
41+
42+
def on_data_received(self) -> None:
43+
pass
4144

4245

4346
class EcoflowDataHolder:
@@ -66,10 +69,12 @@ def __init__(
6669

6770
self.set_status = BoundFifoList[dict[str, Any]]()
6871
self.set_status_time = dt.utcnow().replace(year=2000, month=1, day=1, hour=0, minute=0, second=0)
72+
self.status_change_time = dt.utcnow().replace(year=2000, month=1, day=1, hour=0, minute=0, second=0)
6973

7074
def last_received_time(self):
7175
return max(
7276
self.set_status_time,
77+
self.status_change_time,
7378
self.set_params_time,
7479
# 1. get_reply can receive '"message": "The device is not online"'
7580
# 2. if device is online - get_reply message will update params, so param_time will be updated as well
@@ -120,6 +125,7 @@ def __update_params(self, params: dict[str, Any]):
120125
def __accept_prepared_data(self, data: PreparedData, raw_data_acceptor: Callable[[dict[str, Any]], None]):
121126
if data.online is not None:
122127
self._status_callback.on_explicit_status(data.online)
128+
self.status_change_time = dt.utcnow()
123129

124130
if data.params is not None:
125131
self.__update_params(data.params)

custom_components/ecoflow_cloud/entities/__init__.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ def _adopt_json_key(self, key: str):
123123
else:
124124
return key
125125

126+
@property
127+
def available(self) -> bool:
128+
return bool(self._attr_available) and not self._device.status_tracker.is_offline
129+
126130
@property
127131
def mqtt_key(self):
128132
return self.__mqtt_key
@@ -141,13 +145,19 @@ async def async_added_to_hass(self):
141145
# self.async_on_remove(d.dispose)
142146

143147
def _handle_coordinator_update(self) -> None:
148+
if self._device.status_tracker.is_offline:
149+
self._mark_unavailable()
150+
return
151+
144152
if self.coordinator.data.changed:
145153
self._updated(self.coordinator.data.data_holder.params)
146-
elif self._device.status_tracker.is_offline: # Device is offline
147-
# Reset sensors that should reset to default values
148-
if isinstance(self, BaseSensorEntity) and self._attr_default_value is not None:
149-
self._mqtt_key_expr.update(self.coordinator.data.data_holder.params, self._attr_default_value)
150-
self._updated(self.coordinator.data.data_holder.params)
154+
155+
def _mark_unavailable(self) -> None:
156+
if not self._attr_available:
157+
return
158+
159+
self._attr_available = False
160+
self.schedule_update_ha_state()
151161

152162
def _updated(self, data: dict[str, Any]):
153163
# update attributes

custom_components/ecoflow_cloud/sensor.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
ECOFLOW_DOMAIN,
4646
)
4747
from .api import EcoflowApiClient
48+
from .devices.data_coordinator import EcoflowBroadcastDataHolder
4849
from .devices import BaseDevice, const
4950
from .entities import (
5051
BaseSensorEntity,
@@ -688,6 +689,8 @@ def _handle_coordinator_update(self) -> None:
688689
self._attr_native_value = status.label
689690
self._actualize_attributes()
690691
self.schedule_update_ha_state()
692+
if status.online is not True:
693+
self.coordinator.async_set_updated_data(EcoflowBroadcastDataHolder(self._device.data, False))
691694

692695
def _format_age(self, timestamp: datetime | None) -> str | None:
693696
if timestamp is None:

0 commit comments

Comments
 (0)