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-06-10 14:47:00 +00:00
|
|
|
from typing import Any
|
2024-06-29 08:24:30 +00:00
|
|
|
from warnings import warn
|
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.
|
|
|
|
"""
|
|
|
|
|
2024-06-28 18:25:39 +00:00
|
|
|
_parent: SmartDevice
|
|
|
|
|
2024-01-29 16:11:29 +00:00
|
|
|
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
|
|
|
|
2024-06-29 08:24:30 +00:00
|
|
|
async def update(
|
|
|
|
self,
|
|
|
|
update_children_or_parent: bool = True,
|
|
|
|
*,
|
|
|
|
update_children: bool | None = None,
|
|
|
|
):
|
2024-06-28 18:25:39 +00:00
|
|
|
"""Update the device.
|
|
|
|
|
|
|
|
Calling update directly on a child device will update the parent
|
|
|
|
and only this child.
|
|
|
|
"""
|
2024-06-29 08:24:30 +00:00
|
|
|
if update_children is not None:
|
|
|
|
warn(
|
|
|
|
"update_children is deprecated, use update_children_or_parent",
|
|
|
|
DeprecationWarning,
|
|
|
|
stacklevel=1,
|
|
|
|
)
|
|
|
|
update_children_or_parent = False
|
|
|
|
|
2024-06-29 07:28:18 +00:00
|
|
|
if update_children_or_parent:
|
|
|
|
await self._parent._update(called_from_child=self)
|
2024-06-28 18:25:39 +00:00
|
|
|
else:
|
|
|
|
await self._update()
|
|
|
|
|
|
|
|
async def _update(self):
|
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-06-28 18:25:39 +00:00
|
|
|
# Hubs attached devices only update via the parent hub
|
|
|
|
if self._parent.device_type == DeviceType.Hub:
|
|
|
|
return
|
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-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}>"
|