Skip to content

Commit

Permalink
v0.10.0-pre: QuotasStatusSensor for DELTA_2, RIVER_2_MAX (more attrib…
Browse files Browse the repository at this point in the history
…utes)
  • Loading branch information
tolwi committed Jul 1, 2023
1 parent 8bdf73a commit 33ddb61
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
6 changes: 4 additions & 2 deletions custom_components/ecoflow_cloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
Platform.SWITCH,
}

ATTR_STATUS_SN = "sn"
ATTR_STATUS_UPDATES = "updates"
ATTR_STATUS_SN = "SN"
ATTR_STATUS_QUOTA_UPDATES = "quota_update_count"
ATTR_STATUS_QUOTA_LAST_UPDATE = "quota_last_update"
ATTR_STATUS_DATA_LAST_UPDATE = "data_last_update"


async def async_migrate_entry(hass, config_entry: ConfigEntry):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ecoflow_cloud/entities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def enabled_default(self):

async def async_added_to_hass(self):
await super().async_added_to_hass()
d = self._client.data.observable().subscribe(self.__updated)
d = self._client.data.params_observable().subscribe(self.__updated)
self.async_on_remove(d.dispose)

def __updated(self, data: dict[str, Any]):
Expand Down
3 changes: 2 additions & 1 deletion custom_components/ecoflow_cloud/mqtt/ecoflow_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def __init__(self, update_period_sec: int, collect_raw: bool = False):
self.__set_reply_observable = Subject[list[dict[str, Any]]]()
self.__get_reply_observable = Subject[list[dict[str, Any]]]()

def observable(self) -> Observable[dict[str, Any]]:
def params_observable(self) -> Observable[dict[str, Any]]:
return self.__params_observable

def get_reply_observable(self) -> Observable[list[dict[str, Any]]]:
Expand Down Expand Up @@ -142,6 +142,7 @@ def update_to_target_state(self, target_state: dict[str, Any]):

def update_data(self, raw: dict[str, Any]):
self.__add_raw_data(raw)
self.params['timestamp'] = raw['timestamp']
self.params.update(raw['params'])

if (utcnow() - self.__params_broadcast_time).total_seconds() > self.__update_period_sec:
Expand Down
5 changes: 3 additions & 2 deletions custom_components/ecoflow_cloud/recorder.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from homeassistant.core import callback, HomeAssistant

from custom_components.ecoflow_cloud import ATTR_STATUS_UPDATES
from custom_components.ecoflow_cloud import ATTR_STATUS_QUOTA_UPDATES, ATTR_STATUS_DATA_LAST_UPDATE, \
ATTR_STATUS_QUOTA_LAST_UPDATE


@callback
def exclude_attributes(hass: HomeAssistant) -> set[str]:
return {ATTR_STATUS_UPDATES}
return {ATTR_STATUS_QUOTA_UPDATES, ATTR_STATUS_DATA_LAST_UPDATE, ATTR_STATUS_QUOTA_LAST_UPDATE}
33 changes: 24 additions & 9 deletions custom_components/ecoflow_cloud/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_time_interval
from homeassistant.util import utcnow

from . import DOMAIN, OPTS_REFRESH_PERIOD_SEC, ATTR_STATUS_SN, ATTR_STATUS_UPDATES
from . import DOMAIN, OPTS_REFRESH_PERIOD_SEC, ATTR_STATUS_SN, ATTR_STATUS_DATA_LAST_UPDATE, ATTR_STATUS_QUOTA_UPDATES, \
ATTR_STATUS_QUOTA_LAST_UPDATE
from .entities import BaseSensorEntity, EcoFlowAbstractEntity
from .mqtt.ecoflow_mqtt import EcoflowMQTTClient

Expand Down Expand Up @@ -113,13 +115,18 @@ def __init__(self, client: EcoflowMQTTClient):
super().__init__(client, "Status", "status")
self._data_refresh_sec = int(client.config_entry.options[OPTS_REFRESH_PERIOD_SEC])
self.__online = -1
self.__last_update = utcnow()
self.__attrs = dict[str, Any]()
self.__attrs[ATTR_STATUS_UPDATES] = 0
self.__attrs[ATTR_STATUS_QUOTA_UPDATES] = 0

async def async_added_to_hass(self):
await super().async_added_to_hass()
d = self._client.data.get_reply_observable().subscribe(self.__updated)
self.async_on_remove(d.dispose)

get_reply_d = self._client.data.get_reply_observable().subscribe(self.__get_reply_update)
self.async_on_remove(get_reply_d.dispose)

params_d = self._client.data.params_observable().subscribe(self.__params_update)
self.async_on_remove(params_d.dispose)

self.__get_latest_quotas()

Expand All @@ -128,20 +135,28 @@ async def async_added_to_hass(self):

def __check_latest_quotas(self, now: datetime):
update_delta_sec = (now - self._client.data.last_params_broadcast_time()).total_seconds()
data_after_quota = (self._client.data.last_params_broadcast_time() - self.__last_update).total_seconds()
is_data_outdated = update_delta_sec > self._data_refresh_sec * 3
is_data_without_quota = data_after_quota > self._data_refresh_sec * 2

if (self.__online == 1 and update_delta_sec > self._data_refresh_sec * 3) or (
self.__online != 1 and update_delta_sec < self._data_refresh_sec):
# online and outdated (try to force data updates) OR offline but with incoming updates (refresh status)
if (self.__online == 1 and is_data_outdated) or (self.__online != 1 and is_data_without_quota):
self.__get_latest_quotas()

def __get_latest_quotas(self):
self.send_get_message({"version": "1.1", "moduleType": 0, "operateType": "latestQuotas", "params": {}})

def __updated(self, data: list[dict[str, Any]]):
def __params_update(self, data: dict[str, Any]):
self.__attrs[ATTR_STATUS_DATA_LAST_UPDATE] = datetime.fromtimestamp(data['timestamp'])
self.async_write_ha_state()

def __get_reply_update(self, data: list[dict[str, Any]]):
d = data[0]
if d["operateType"] == "latestQuotas":
self.__online = d["data"]["online"]

self.__attrs[ATTR_STATUS_UPDATES] = self.__attrs[ATTR_STATUS_UPDATES] + 1
self.__last_update = utcnow()
self.__attrs[ATTR_STATUS_QUOTA_LAST_UPDATE] = self.__last_update
self.__attrs[ATTR_STATUS_QUOTA_UPDATES] = self.__attrs[ATTR_STATUS_QUOTA_UPDATES] + 1

if self.__online == 1:
self.__attrs[ATTR_STATUS_SN] = d["data"]["sn"]
Expand Down

0 comments on commit 33ddb61

Please sign in to comment.