Skip to content

Commit

Permalink
Add periodic passive device polling
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Jan 18, 2025
1 parent 9432d39 commit b7cd6bd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 12 additions & 4 deletions custom_components/yandex_station/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from datetime import timedelta

import voluptuous as vol
from homeassistant.components.binary_sensor import HomeAssistant # important for tests
Expand Down Expand Up @@ -29,6 +30,7 @@
device_registry as dr,
discovery,
)
from homeassistant.helpers.event import async_track_time_interval

from .core import utils
from .core.const import (
Expand Down Expand Up @@ -187,10 +189,16 @@ async def update_cookie_and_token(**kwargs):

quasar.start()

platforms = (
PLATFORMS if hass_utils.incluce_devices(hass, entry) else SPEAKER_PLATFORMS
)
setattr(quasar, "platforms", platforms)
if hass_utils.incluce_devices(hass, entry):
quasar.platforms = platforms = PLATFORMS
entry.async_on_unload(
async_track_time_interval(
hass, quasar.devices_passive_update, timedelta(minutes=5)
)
)
else:
quasar.platforms = platforms = SPEAKER_PLATFORMS

await hass.config_entries.async_forward_entry_setups(entry, platforms)
entry.async_on_unload(entry.add_update_listener(async_reload_entry))

Expand Down
16 changes: 16 additions & 0 deletions custom_components/yandex_station/core/yandex_quasar.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,22 @@ async def connect(self):
if '"source":"create_scenario_launch"' in resp["message"]:
_ = asyncio.create_task(self.get_voice_trigger(1))

async def devices_passive_update(self, *args):
try:
r = await self.session.get(
f"https://iot.quasar.yandex.ru/m/v3/user/devices", timeout=15
)
resp = await r.json()
assert resp["status"] == "ok", resp

for house in resp["households"]:
if "sharing_info" in house:
continue
for device in house["all"]:
self.dispatch_update(device["id"], device)
except Exception as e:
_LOGGER.debug(f"Devices forceupdate problem: {repr(e)}")

async def get_voice_trigger(self, retries: int = 0):
try:
# 1. Get all scenarios history
Expand Down

0 comments on commit b7cd6bd

Please sign in to comment.