mirror of
https://github.com/python-kasa/python-kasa.git
synced 2025-08-09 20:24:02 +00:00
Add support for contact sensor (T110) (#877)
Initial support for T110 contact sensor & T110 fixture by courtesy of @ngaertner.
This commit is contained in:
@@ -8,6 +8,7 @@ from .childdevicemodule import ChildDeviceModule
|
||||
from .cloudmodule import CloudModule
|
||||
from .colormodule import ColorModule
|
||||
from .colortemp import ColorTemperatureModule
|
||||
from .contact import ContactSensor
|
||||
from .devicemodule import DeviceModule
|
||||
from .energymodule import EnergyModule
|
||||
from .fanmodule import FanModule
|
||||
@@ -45,5 +46,6 @@ __all__ = [
|
||||
"ColorTemperatureModule",
|
||||
"ColorModule",
|
||||
"WaterleakSensor",
|
||||
"ContactSensor",
|
||||
"FrostProtectionModule",
|
||||
]
|
||||
|
42
kasa/smart/modules/contact.py
Normal file
42
kasa/smart/modules/contact.py
Normal file
@@ -0,0 +1,42 @@
|
||||
"""Implementation of contact sensor module."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from ...feature import Feature
|
||||
from ..smartmodule import SmartModule
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..smartdevice import SmartDevice
|
||||
|
||||
|
||||
class ContactSensor(SmartModule):
|
||||
"""Implementation of contact sensor module."""
|
||||
|
||||
REQUIRED_COMPONENT = None # we depend on availability of key
|
||||
REQUIRED_KEY_ON_PARENT = "open"
|
||||
|
||||
def __init__(self, device: SmartDevice, module: str):
|
||||
super().__init__(device, module)
|
||||
self._add_feature(
|
||||
Feature(
|
||||
device,
|
||||
id="is_open",
|
||||
name="Open",
|
||||
container=self,
|
||||
attribute_getter="is_open",
|
||||
icon="mdi:door",
|
||||
category=Feature.Category.Primary,
|
||||
type=Feature.Type.BinarySensor,
|
||||
)
|
||||
)
|
||||
|
||||
def query(self) -> dict:
|
||||
"""Query to execute during the update cycle."""
|
||||
return {}
|
||||
|
||||
@property
|
||||
def is_open(self):
|
||||
"""Return True if the contact sensor is open."""
|
||||
return self._device.sys_info["open"]
|
@@ -49,6 +49,7 @@ class SmartChildDevice(SmartDevice):
|
||||
"""Return child device type."""
|
||||
child_device_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,
|
||||
"kasa.switch.outlet.sub-fan": DeviceType.Fan,
|
||||
|
@@ -210,7 +210,10 @@ class SmartDevice(Bulb, Fan, Device):
|
||||
skip_parent_only_modules and mod in WALL_SWITCH_PARENT_ONLY_MODULES
|
||||
) or mod.__name__ in child_modules_to_skip:
|
||||
continue
|
||||
if mod.REQUIRED_COMPONENT in self._components:
|
||||
if (
|
||||
mod.REQUIRED_COMPONENT in self._components
|
||||
or self.sys_info.get(mod.REQUIRED_KEY_ON_PARENT) is not None
|
||||
):
|
||||
_LOGGER.debug(
|
||||
"Found required %s, adding %s to modules.",
|
||||
mod.REQUIRED_COMPONENT,
|
||||
|
@@ -18,8 +18,13 @@ class SmartModule(Module):
|
||||
"""Base class for SMART modules."""
|
||||
|
||||
NAME: str
|
||||
REQUIRED_COMPONENT: str
|
||||
#: Module is initialized, if the given component is available
|
||||
REQUIRED_COMPONENT: str | None = None
|
||||
#: Module is initialized, if the given key available in the main sysinfo
|
||||
REQUIRED_KEY_ON_PARENT: str | None = None
|
||||
#: Query to execute during the main update cycle
|
||||
QUERY_GETTER_NAME: str
|
||||
|
||||
REGISTERED_MODULES: dict[str, type[SmartModule]] = {}
|
||||
|
||||
def __init__(self, device: SmartDevice, module: str):
|
||||
@@ -27,8 +32,6 @@ class SmartModule(Module):
|
||||
super().__init__(device, module)
|
||||
|
||||
def __init_subclass__(cls, **kwargs):
|
||||
assert cls.REQUIRED_COMPONENT is not None # noqa: S101
|
||||
|
||||
name = getattr(cls, "NAME", cls.__name__)
|
||||
_LOGGER.debug("Registering %s" % cls)
|
||||
cls.REGISTERED_MODULES[name] = cls
|
||||
@@ -91,8 +94,13 @@ class SmartModule(Module):
|
||||
|
||||
@property
|
||||
def supported_version(self) -> int:
|
||||
"""Return version supported by the device."""
|
||||
return self._device._components[self.REQUIRED_COMPONENT]
|
||||
"""Return version supported by the device.
|
||||
|
||||
If the module has no required component, this will return -1.
|
||||
"""
|
||||
if self.REQUIRED_COMPONENT is not None:
|
||||
return self._device._components[self.REQUIRED_COMPONENT]
|
||||
return -1
|
||||
|
||||
async def _check_supported(self) -> bool:
|
||||
"""Additional check to see if the module is supported by the device.
|
||||
|
Reference in New Issue
Block a user