From 2342815482e379435fe2f12fdc9b34bb8fdfac05 Mon Sep 17 00:00:00 2001 From: cyrilp Date: Mon, 18 Mar 2024 20:45:08 +0100 Subject: [PATCH] implement device polling and refresh --- custom_components/deltadore_tydom/__init__.py | 8 ++++- custom_components/deltadore_tydom/hub.py | 14 +++++--- .../deltadore_tydom/tydom/MessageHandler.py | 6 ++-- .../deltadore_tydom/tydom/tydom_client.py | 35 ++++++++++--------- .../deltadore_tydom/tydom/tydom_devices.py | 5 +++ 5 files changed, 44 insertions(+), 24 deletions(-) diff --git a/custom_components/deltadore_tydom/__init__.py b/custom_components/deltadore_tydom/__init__.py index 24e90ac..ac727fc 100644 --- a/custom_components/deltadore_tydom/__init__.py +++ b/custom_components/deltadore_tydom/__init__.py @@ -63,7 +63,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: target=tydom_hub.ping(), hass=hass, name="Tydom ping" ) entry.async_create_background_task( - target=tydom_hub.refresh_all(), hass=hass, name="Tydom refresh metadata and data" + target=tydom_hub.refresh_all(), hass=hass, name="Tydom refresh all metadata and data" + ) + entry.async_create_background_task( + target=tydom_hub.refresh_data_1s(), hass=hass, name="Tydom refresh data 1s" + ) + entry.async_create_background_task( + target=tydom_hub.refresh_data_5m(), hass=hass, name="Tydom refresh data 5m" ) except Exception as err: diff --git a/custom_components/deltadore_tydom/hub.py b/custom_components/deltadore_tydom/hub.py index be5220c..99f7118 100644 --- a/custom_components/deltadore_tydom/hub.py +++ b/custom_components/deltadore_tydom/hub.py @@ -299,10 +299,16 @@ async def refresh_all(self) -> None: await self._tydom_client.get_devices_cmeta() await self._tydom_client.get_devices_data() await self._tydom_client.get_scenarii() - await asyncio.sleep(300) + await asyncio.sleep(600) + + async def refresh_data_1s(self) -> None: + """Refresh data for devices in list.""" + while True: + await self._tydom_client.poll_devices_data_1s() + await asyncio.sleep(1) - async def refresh_data(self) -> None: + async def refresh_data_5m(self) -> None: """Periodically refresh data for devices which don't do push.""" while True: - await self._tydom_client.ping() - await asyncio.sleep(5) + await self._tydom_client.poll_devices_data_5m() + await asyncio.sleep(300) diff --git a/custom_components/deltadore_tydom/tydom/MessageHandler.py b/custom_components/deltadore_tydom/tydom/MessageHandler.py index 1f94f4f..d1f0ccb 100644 --- a/custom_components/deltadore_tydom/tydom/MessageHandler.py +++ b/custom_components/deltadore_tydom/tydom/MessageHandler.py @@ -379,7 +379,7 @@ async def parse_cmeta_data(self, parsed): + dest + "&reset=false" ) - self.tydom_client.add_poll_device_url(url) + self.tydom_client.add_poll_device_url_5m(url) LOGGER.debug("Add poll device : %s", url) elif elem["name"] == "energyInstant": device_name[unique_id] = "Tywatt" @@ -398,7 +398,7 @@ async def parse_cmeta_data(self, parsed): + unit + "&reset=false" ) - self.tydom_client.add_poll_device_url(url) + self.tydom_client.add_poll_device_url_5m(url) LOGGER.debug("Add poll device : " + url) elif elem["name"] == "energyDistrib": device_name[unique_id] = "Tywatt" @@ -416,7 +416,7 @@ async def parse_cmeta_data(self, parsed): + "&period=YEAR&periodOffset=0&src=" + src ) - self.tydom_client.add_poll_device_url(url) + self.tydom_client.add_poll_device_url_5m(url) LOGGER.debug("Add poll device : " + url) LOGGER.debug("Metadata configuration updated") diff --git a/custom_components/deltadore_tydom/tydom/tydom_client.py b/custom_components/deltadore_tydom/tydom/tydom_client.py index e560200..44265ac 100644 --- a/custom_components/deltadore_tydom/tydom/tydom_client.py +++ b/custom_components/deltadore_tydom/tydom/tydom_client.py @@ -77,7 +77,8 @@ def __init__( self._connection = None self.event_callback = event_callback # Some devices (like Tywatt) need polling - self.poll_device_urls = [] + self.poll_device_urls_1s = [] + self.poll_device_urls_5m = [] self.current_poll_index = 0 if self._remote_mode: @@ -442,16 +443,6 @@ async def post_refresh(self): msg_type = "/refresh/all" req = "POST" await self.send_message(method=req, msg=msg_type) - # Get poll device data - nb_poll_devices = len(self.poll_device_urls) - if self.current_poll_index < nb_poll_devices - 1: - self.current_poll_index = self.current_poll_index + 1 - else: - self.current_poll_index = 0 - if nb_poll_devices > 0: - await self.get_poll_device_data( - self.poll_device_urls[self.current_poll_index] - ) async def ping(self): """Send a ping (pong should be returned).""" @@ -471,8 +462,16 @@ async def get_devices_data(self): msg_type = "/devices/data" req = "GET" await self.send_message(method=req, msg=msg_type) - # Get poll devices data - for url in self.poll_device_urls: + + async def poll_devices_data_1s(self): + """Poll devices data.""" + if self.poll_device_urls_1s: + url = self.poll_device_urls_1s.pop() + await self.get_poll_device_data(url) + + async def poll_devices_data_5m(self): + """Poll devices data.""" + for url in self.poll_device_urls_5m: await self.get_poll_device_data(url) async def get_configs_file(self): @@ -519,15 +518,19 @@ async def get_device_data(self, id): await self.send_bytes(a_bytes) async def get_poll_device_data(self, url): - """Poll a device (probably unused).""" + """Poll a device.""" LOGGER.error("poll device data %s", url) msg_type = url req = "GET" await self.send_message(method=req, msg=msg_type) - def add_poll_device_url(self, url): + def add_poll_device_url_1s(self, url): + """Add a device for polling (probably unused).""" + self.poll_device_urls_1s.append(url) + + def add_poll_device_url_5m(self, url): """Add a device for polling (probably unused).""" - self.poll_device_urls.append(url) + self.poll_device_urls_5m.append(url) async def get_moments(self): """Get the moments (programs).""" diff --git a/custom_components/deltadore_tydom/tydom/tydom_devices.py b/custom_components/deltadore_tydom/tydom/tydom_devices.py index 63c7b78..cebdc8c 100644 --- a/custom_components/deltadore_tydom/tydom/tydom_devices.py +++ b/custom_components/deltadore_tydom/tydom/tydom_devices.py @@ -280,6 +280,7 @@ async def turn_on(self, brightness) -> None: await self._tydom_client.put_devices_data( self._id, self._endpoint, "level", str(brightness) ) + self._tydom_client.add_poll_device_url_1s(f"/devices/{self._id}/endpoints/{self._endpoint}/cdata") async def turn_off(self) -> None: """Tell light to turn off.""" @@ -291,6 +292,7 @@ async def turn_off(self) -> None: await self._tydom_client.put_devices_data( self._id, self._endpoint, "levelCmd", command ) + self._tydom_client.add_poll_device_url_1s(f"/devices/{self._id}/endpoints/{self._endpoint}/cdata") class TydomAlarm(TydomDevice): """represents an alarm.""" @@ -304,11 +306,14 @@ def is_legacy_alarm(self) -> bool: async def alarm_disarm(self, code) -> None: """Disarm alarm.""" await self._tydom_client.put_alarm_cdata(self._id, self._endpoint, code, "OFF", None, self.is_legacy_alarm()) + # self._tydom_client.add_poll_device_url_1s(f"/devices/{self._id}/endpoints/{self._endpoint}/cdata") async def alarm_arm_away(self, code=None) -> None: """Arm away alarm.""" await self._tydom_client.put_alarm_cdata(self._id, self._endpoint, code, "ON", self._tydom_client._zone_away, self.is_legacy_alarm()) + # self._tydom_client.add_poll_device_url_1s(f"/devices/{self._id}/endpoints/{self._endpoint}/cdata") async def alarm_arm_home(self, code=None) -> None: """Arm home alarm.""" await self._tydom_client.put_alarm_cdata(self._id, self._endpoint, code, "ON", self._tydom_client._zone_home, self.is_legacy_alarm()) + # self._tydom_client.add_poll_device_url_1s(f"/devices/{self._id}/endpoints/{self._endpoint}/cdata")