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
|
2024-11-13 17:50:21 +00:00
|
|
|
from ..protocols.smartprotocol import SmartProtocol, _ChildProtocolWrapper
|
2024-02-04 15:20:08 +00:00
|
|
|
from .smartdevice import SmartDevice
|
2024-07-30 18:23:07 +00:00
|
|
|
from .smartmodule import SmartModule
|
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.
|
|
|
|
"""
|
|
|
|
|
2024-10-18 08:55:07 +00:00
|
|
|
CHILD_DEVICE_TYPE_MAP = {
|
|
|
|
"plug.powerstrip.sub-plug": DeviceType.Plug,
|
|
|
|
"subg.trigger.contact-sensor": DeviceType.Sensor,
|
|
|
|
"subg.trigger.temp-hmdt-sensor": DeviceType.Sensor,
|
|
|
|
"subg.trigger.water-leak-sensor": DeviceType.Sensor,
|
2024-10-18 10:31:52 +00:00
|
|
|
"subg.trigger.motion-sensor": DeviceType.Sensor,
|
2024-10-18 08:55:07 +00:00
|
|
|
"kasa.switch.outlet.sub-fan": DeviceType.Fan,
|
|
|
|
"kasa.switch.outlet.sub-dimmer": DeviceType.Dimmer,
|
|
|
|
"subg.trv": DeviceType.Thermostat,
|
|
|
|
"subg.trigger.button": DeviceType.Sensor,
|
|
|
|
}
|
|
|
|
|
2024-01-29 16:11:29 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2024-02-04 15:20:08 +00:00
|
|
|
parent: SmartDevice,
|
2024-10-24 16:22:45 +00:00
|
|
|
info: dict,
|
|
|
|
component_info: dict,
|
|
|
|
*,
|
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:
|
2024-10-31 15:21:54 +00:00
|
|
|
self._id = info["device_id"]
|
|
|
|
_protocol = protocol or _ChildProtocolWrapper(self._id, parent.protocol)
|
|
|
|
super().__init__(parent.host, config=parent.config, protocol=_protocol)
|
2024-01-29 16:11:29 +00:00
|
|
|
self._parent = parent
|
2024-02-22 19:46:19 +00:00
|
|
|
self._update_internal_state(info)
|
|
|
|
self._components = component_info
|
2024-01-29 16:11:29 +00:00
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
async def update(self, update_children: bool = True) -> None:
|
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)
|
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
async def _update(self, update_children: bool = True) -> None:
|
2024-07-02 13:11:19 +00:00
|
|
|
"""Update child module info.
|
|
|
|
|
|
|
|
Internal implementation to allow patching of public update in the cli
|
|
|
|
or test framework.
|
|
|
|
"""
|
2024-07-30 18:23:07 +00:00
|
|
|
now = time.monotonic()
|
|
|
|
module_queries: list[SmartModule] = []
|
2024-06-10 14:47:00 +00:00
|
|
|
req: dict[str, Any] = {}
|
|
|
|
for module in self.modules.values():
|
2024-07-30 18:23:07 +00:00
|
|
|
if module.disabled is False and (mod_query := module.query()):
|
|
|
|
module_queries.append(module)
|
2024-06-10 14:47:00 +00:00
|
|
|
req.update(mod_query)
|
|
|
|
if req:
|
|
|
|
self._last_update = await self.protocol.query(req)
|
2024-07-30 18:23:07 +00:00
|
|
|
|
|
|
|
for module in self.modules.values():
|
2024-10-08 07:16:51 +00:00
|
|
|
await self._handle_module_post_update(
|
2024-07-30 18:23:07 +00:00
|
|
|
module, now, had_query=module in module_queries
|
|
|
|
)
|
|
|
|
self._last_update_time = now
|
2024-01-29 16:11:29 +00:00
|
|
|
|
2024-02-22 19:46:19 +00:00
|
|
|
@classmethod
|
2024-10-24 16:22:45 +00:00
|
|
|
async def create(
|
|
|
|
cls,
|
|
|
|
parent: SmartDevice,
|
|
|
|
child_info: dict,
|
|
|
|
child_components: dict,
|
|
|
|
protocol: SmartProtocol | None = None,
|
|
|
|
*,
|
|
|
|
last_update: dict | None = None,
|
|
|
|
) -> SmartDevice:
|
|
|
|
"""Create a child device based on device info and component listing.
|
|
|
|
|
|
|
|
If creating a smart child from a different protocol, i.e. a camera hub,
|
|
|
|
protocol: SmartProtocol and last_update should be provided as per the
|
|
|
|
FIRST_UPDATE_MODULES expected by the update cycle as these cannot be
|
|
|
|
derived from the parent.
|
|
|
|
"""
|
|
|
|
child: SmartChildDevice = cls(
|
|
|
|
parent, child_info, child_components, protocol=protocol
|
|
|
|
)
|
|
|
|
if last_update:
|
|
|
|
child._last_update = last_update
|
2024-02-22 19:46:19 +00:00
|
|
|
await child._initialize_modules()
|
|
|
|
return child
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_type(self) -> DeviceType:
|
|
|
|
"""Return child device type."""
|
2024-10-18 10:31:52 +00:00
|
|
|
category = self.sys_info["category"]
|
|
|
|
dev_type = self.CHILD_DEVICE_TYPE_MAP.get(category)
|
2024-02-22 19:46:19 +00:00
|
|
|
if dev_type is None:
|
2024-10-18 10:31:52 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"Unknown child device type %s for model %s, please open issue",
|
|
|
|
category,
|
|
|
|
self.model,
|
|
|
|
)
|
2024-02-22 19:46:19 +00:00
|
|
|
dev_type = DeviceType.Unknown
|
|
|
|
return dev_type
|
2024-01-29 16:11:29 +00:00
|
|
|
|
2024-11-10 18:55:13 +00:00
|
|
|
def __repr__(self) -> str:
|
2024-03-05 12:35:19 +00:00
|
|
|
return f"<{self.device_type} {self.alias} ({self.model}) of {self._parent}>"
|