Skip to content

Commit

Permalink
implement device polling and refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrilp committed Mar 18, 2024
1 parent 00f4699 commit 2342815
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
8 changes: 7 additions & 1 deletion custom_components/deltadore_tydom/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 10 additions & 4 deletions custom_components/deltadore_tydom/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
6 changes: 3 additions & 3 deletions custom_components/deltadore_tydom/tydom/MessageHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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")
Expand Down
35 changes: 19 additions & 16 deletions custom_components/deltadore_tydom/tydom/tydom_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)."""
Expand All @@ -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):
Expand Down Expand Up @@ -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)."""
Expand Down
5 changes: 5 additions & 0 deletions custom_components/deltadore_tydom/tydom/tydom_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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."""
Expand All @@ -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")

0 comments on commit 2342815

Please sign in to comment.