2024-01-29 16:11:29 +00:00
|
|
|
"""Child device implementation."""
|
2024-04-16 18:21:20 +00:00
|
|
|
|
2024-04-17 13:39:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-02-22 19:46:19 +00:00
|
|
|
import logging
|
2024-07-11 15:21:59 +00:00
|
|
|
import time
|
2024-06-10 14:47:00 +00:00
|
|
|
from typing import Any
|
2024-01-29 16:11:29 +00:00
|
|
|
|
2024-02-02 16:29:14 +00:00
|
|
|
from ..device_type import DeviceType
|
2024-01-29 16:11:29 +00:00
|
|
|
from ..deviceconfig import DeviceConfig
|
|
|
|
from ..smartprotocol import SmartProtocol, _ChildProtocolWrapper
|
2024-02-04 15:20:08 +00:00
|
|
|
from .smartdevice import SmartDevice
|
2024-01-29 16:11:29 +00:00
|
|
|
|
2024-02-22 19:46:19 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2024-01-29 16:11:29 +00:00
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
class SmartChildDevice(SmartDevice):
|
2024-01-29 16:11:29 +00:00
|
|
|
"""Presentation of a child device.
|
|
|
|
|
|
|
|
This wraps the protocol communications and sets internal data for the child.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2024-02-04 15:20:08 +00:00
|
|
|
parent: SmartDevice,
|
2024-02-22 19:46:19 +00:00
|
|
|
info,
|
|
|
|
component_info,
|
2024-04-17 13:39:24 +00:00
|
|
|
config: DeviceConfig | None = None,
|
|
|
|
protocol: SmartProtocol | None = None,
|
2024-01-29 16:11:29 +00:00
|
|
|
) -> None:
|
|
|
|
super().__init__(parent.host, config=parent.config, protocol=parent.protocol)
|
|
|
|
self._parent = parent
|
2024-02-22 19:46:19 +00:00
|
|
|
self._update_internal_state(info)
|
|
|
|
self._components = component_info
|
|
|
|
self._id = info["device_id"]
|
|
|
|
self.protocol = _ChildProtocolWrapper(self._id, parent.protocol)
|
2024-01-29 16:11:29 +00:00
|
|
|
|
|
|
|
async def update(self, update_children: bool = True):
|
2024-06-10 14:47:00 +00:00
|
|
|
"""Update child module info.
|
|
|
|
|
|
|
|
The parent updates our internal info so just update modules with
|
|
|
|
their own queries.
|
|
|
|
"""
|
2024-07-02 13:11:19 +00:00
|
|
|
await self._update(update_children)
|
|
|
|
|
|
|
|
async def _update(self, update_children: bool = True):
|
|
|
|
"""Update child module info.
|
|
|
|
|
|
|
|
Internal implementation to allow patching of public update in the cli
|
|
|
|
or test framework.
|
|
|
|
"""
|
2024-06-10 14:47:00 +00:00
|
|
|
req: dict[str, Any] = {}
|
|
|
|
for module in self.modules.values():
|
|
|
|
if mod_query := module.query():
|
|
|
|
req.update(mod_query)
|
|
|
|
if req:
|
|
|
|
self._last_update = await self.protocol.query(req)
|
2024-07-11 15:21:59 +00:00
|
|
|
self._last_update_time = time.time()
|
2024-01-29 16:11:29 +00:00
|
|
|
|
2024-02-22 19:46:19 +00:00
|
|
|
@classmethod
|
|
|
|
async def create(cls, parent: SmartDevice, child_info, child_components):
|
|
|
|
"""Create a child device based on device info and component listing."""
|
2024-04-17 13:39:24 +00:00
|
|
|
child: SmartChildDevice = cls(parent, child_info, child_components)
|
2024-02-22 19:46:19 +00:00
|
|
|
await child._initialize_modules()
|
|
|
|
return child
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_type(self) -> DeviceType:
|
|
|
|
"""Return child device type."""
|
|
|
|
child_device_map = {
|
|
|
|
"plug.powerstrip.sub-plug": DeviceType.Plug,
|
2024-05-07 18:58:18 +00:00
|
|
|
"subg.trigger.contact-sensor": DeviceType.Sensor,
|
2024-02-22 19:46:19 +00:00
|
|
|
"subg.trigger.temp-hmdt-sensor": DeviceType.Sensor,
|
2024-04-30 15:31:47 +00:00
|
|
|
"subg.trigger.water-leak-sensor": DeviceType.Sensor,
|
2024-04-17 10:07:16 +00:00
|
|
|
"kasa.switch.outlet.sub-fan": DeviceType.Fan,
|
|
|
|
"kasa.switch.outlet.sub-dimmer": DeviceType.Dimmer,
|
2024-04-22 11:39:07 +00:00
|
|
|
"subg.trv": DeviceType.Thermostat,
|
2024-02-22 19:46:19 +00:00
|
|
|
}
|
|
|
|
dev_type = child_device_map.get(self.sys_info["category"])
|
|
|
|
if dev_type is None:
|
|
|
|
_LOGGER.warning("Unknown child device type, please open issue ")
|
|
|
|
dev_type = DeviceType.Unknown
|
|
|
|
return dev_type
|
2024-01-29 16:11:29 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
2024-03-05 12:35:19 +00:00
|
|
|
return f"<{self.device_type} {self.alias} ({self.model}) of {self._parent}>"
|