Skip to content

Commit

Permalink
feat: add hub child device assoication tracking (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
petretiandrea authored Nov 2, 2023
1 parent 43a000a commit 64aaed0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
6 changes: 4 additions & 2 deletions custom_components/tapo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
(await api.get_device_info()).map(lambda x: DeviceInfo(**x)).get_or_raise()
)
if get_short_model(state.model) in SUPPORTED_HUB_DEVICE_MODEL:
scan_interval = entry.data.get(CONF_SCAN_INTERVAL)
scan_interval_millis = entry.data.get(CONF_SCAN_INTERVAL) * 1000
hub = TapoHub(
entry,
HubDevice(api, subscription_polling_interval_millis=scan_interval),
HubDevice(
api, subscription_polling_interval_millis=scan_interval_millis
),
)
return await hub.initialize_hub(hass)
else:
Expand Down
25 changes: 22 additions & 3 deletions custom_components/tapo/hub/tapo_hub.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from dataclasses import dataclass
from datetime import timedelta
from typing import List
Expand All @@ -18,6 +19,8 @@
from homeassistant.helpers.device_registry import DeviceEntry
from homeassistant.helpers.device_registry import DeviceRegistry
from plugp100.api.hub.hub_device import HubDevice
from plugp100.api.hub.hub_device_tracker import DeviceAdded
from plugp100.api.hub.hub_device_tracker import HubDeviceEvent
from plugp100.api.hub.s200b_device import S200ButtonDevice
from plugp100.api.hub.switch_child_device import SwitchChildDevice
from plugp100.api.hub.t100_device import T100MotionSensor
Expand All @@ -26,6 +29,8 @@
from plugp100.responses.device_state import DeviceInfo
from plugp100.responses.hub_childs.hub_child_base_info import HubChildBaseInfo

_LOGGER = logging.getLogger(__name__)


@dataclass
class TapoHub:
Expand All @@ -51,7 +56,6 @@ async def initialize_hub(self, hass: HomeAssistant):
hw_version=device_info.hardware_version,
)

# TODO: TIPS: attach to hub coordinator to handle device removal
device_list = (
(await self.hub.get_children()).get_or_else([]).get_children_base_info()
)
Expand All @@ -66,6 +70,21 @@ async def initialize_hub(self, hass: HomeAssistant):
),
child_coordinators=child_coordinators,
)

# TODO: refactory with add_device and remove_device methods
initial_device_ids = list(map(lambda x: x.device_id, device_list))

async def _handle_child_device_event(event: HubDeviceEvent):
_LOGGER.info("Detected child association change %s", str(event))
if event.device_id not in initial_device_ids:
await hass.config_entries.async_reload(self.entry.entry_id)
elif event is DeviceAdded:
initial_device_ids.remove(event.device_id)

self.entry.async_on_unload(
self.hub.subscribe_device_association(_handle_child_device_event)
)

await hass.config_entries.async_forward_entry_setups(self.entry, HUB_PLATFORMS)
return True

Expand All @@ -76,14 +95,14 @@ async def setup_child_devices(
device_list: list[HubChildBaseInfo],
):
knwon_children = [
self.add_child_device(registry, device_state)
self.add_child_device(registry, device_state).id
for device_state in device_list
]

# delete device which is no longer available to hub
for device in dr.async_entries_for_config_entry(registry, self.entry.entry_id):
# avoid delete hub device which has a connection
if device not in knwon_children and device.connections is []:
if device.id not in knwon_children and len(device.connections) == 0:
registry.async_remove_device(device.id)

async def setup_child_coordinators(
Expand Down

0 comments on commit 64aaed0

Please sign in to comment.