From 43a000a12cf0ad2e5471c59ad129206743845bdc Mon Sep 17 00:00:00 2001 From: petretiandrea Date: Thu, 2 Nov 2023 23:31:05 +0100 Subject: [PATCH] feat: added water leak sensor (#595) --- custom_components/tapo/__init__.py | 7 +++++- custom_components/tapo/hub/binary_sensor.py | 22 +++++++++++++++++++ custom_components/tapo/hub/sensor.py | 5 ++++- .../tapo/hub/tapo_hub_child_coordinator.py | 7 ++++++ custom_components/tapo/manifest.json | 2 +- requirements_dev.txt | 2 +- 6 files changed, 41 insertions(+), 4 deletions(-) diff --git a/custom_components/tapo/__init__.py b/custom_components/tapo/__init__.py index bcf31de6..171e90f1 100755 --- a/custom_components/tapo/__init__.py +++ b/custom_components/tapo/__init__.py @@ -12,6 +12,7 @@ from custom_components.tapo.setup_helpers import setup_tapo_api from custom_components.tapo.tapo_device import TapoDevice from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_SCAN_INTERVAL from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady from plugp100.api.hub.hub_device import HubDevice @@ -40,7 +41,11 @@ 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: - hub = TapoHub(entry, HubDevice(api)) + scan_interval = entry.data.get(CONF_SCAN_INTERVAL) + hub = TapoHub( + entry, + HubDevice(api, subscription_polling_interval_millis=scan_interval), + ) return await hub.initialize_hub(hass) else: device = TapoDevice(entry, api) diff --git a/custom_components/tapo/hub/binary_sensor.py b/custom_components/tapo/hub/binary_sensor.py index 3b895a48..bc0f1a80 100644 --- a/custom_components/tapo/hub/binary_sensor.py +++ b/custom_components/tapo/hub/binary_sensor.py @@ -17,6 +17,7 @@ from plugp100.api.hub.t100_device import T100MotionSensor from plugp100.api.hub.t110_device import T110SmartDoor from plugp100.api.hub.t31x_device import T31Device +from plugp100.api.hub.water_leak_device import WaterLeakSensor as WaterLeakDevice async def async_setup_entry( @@ -47,6 +48,26 @@ def is_on(self) -> bool: ) +class WaterLeakSensor(BaseTapoHubChildEntity, BinarySensorEntity): + _attr_has_entity_name = True + + def __init__(self, coordinator: TapoCoordinator): + super().__init__(coordinator) + + @property + def device_class(self) -> Optional[str]: + return BinarySensorDeviceClass.MOISTURE + + @property + def is_on(self) -> bool: + return ( + cast(TapoCoordinator, self.coordinator) + .get_state_of(HubChildCommonState) + .water_leak_status + != "normal" + ) + + class MotionSensor(BaseTapoHubChildEntity, BinarySensorEntity): def __init__(self, coordinator: TapoCoordinator): super().__init__(coordinator) @@ -93,4 +114,5 @@ def is_on(self) -> bool: S200ButtonDevice: [LowBatterySensor], T100MotionSensor: [MotionSensor, LowBatterySensor], SwitchChildDevice: [LowBatterySensor], + WaterLeakDevice: [WaterLeakSensor, LowBatterySensor], } diff --git a/custom_components/tapo/hub/sensor.py b/custom_components/tapo/hub/sensor.py index 52588050..ec387101 100644 --- a/custom_components/tapo/hub/sensor.py +++ b/custom_components/tapo/hub/sensor.py @@ -1,4 +1,5 @@ -from datetime import date, datetime +from datetime import date +from datetime import datetime from typing import cast from typing import Optional from typing import Union @@ -23,6 +24,7 @@ from plugp100.api.hub.t100_device import T100MotionSensor from plugp100.api.hub.t110_device import T110SmartDoor from plugp100.api.hub.t31x_device import T31Device +from plugp100.api.hub.water_leak_device import WaterLeakSensor as WaterLeakDevice from plugp100.responses.hub_childs.t31x_device_state import TemperatureUnit @@ -147,4 +149,5 @@ def native_value(self) -> Union[StateType, date, datetime]: T110SmartDoor: [ReportIntervalDiagnostic], S200ButtonDevice: [ReportIntervalDiagnostic], T100MotionSensor: [ReportIntervalDiagnostic], + WaterLeakDevice: [ReportIntervalDiagnostic], } diff --git a/custom_components/tapo/hub/tapo_hub_child_coordinator.py b/custom_components/tapo/hub/tapo_hub_child_coordinator.py index 9dfb4d6d..66bae1b2 100644 --- a/custom_components/tapo/hub/tapo_hub_child_coordinator.py +++ b/custom_components/tapo/hub/tapo_hub_child_coordinator.py @@ -18,6 +18,8 @@ from plugp100.api.hub.t31x_device import T31Device from plugp100.api.hub.t31x_device import T31DeviceState from plugp100.api.hub.t31x_device import TemperatureHumidityRecordsRaw +from plugp100.api.hub.water_leak_device import LeakDeviceState +from plugp100.api.hub.water_leak_device import WaterLeakSensor HubChildDevice = ( T31Device @@ -26,6 +28,7 @@ | S200ButtonDevice | T100MotionSensor | SwitchChildDevice + | WaterLeakSensor ) HubChildCommonState = ( T31DeviceState @@ -33,6 +36,7 @@ | S200BDeviceState | T100MotionSensorState | SwitchChildDeviceState + | LeakDeviceState ) @@ -65,6 +69,9 @@ async def _update_state(self): elif isinstance(self.device, SwitchChildDevice): base_state = (await self.device.get_device_info()).get_or_raise() self.update_state_of(HubChildCommonState, base_state) + elif isinstance(self.device, WaterLeakSensor): + base_state = (await self.device.get_device_state()).get_or_raise() + self.update_state_of(HubChildCommonState, base_state) C = TypeVar("C", bound=TapoCoordinator) diff --git a/custom_components/tapo/manifest.json b/custom_components/tapo/manifest.json index 15e91bf0..a0215b37 100755 --- a/custom_components/tapo/manifest.json +++ b/custom_components/tapo/manifest.json @@ -6,7 +6,7 @@ "iot_class": "local_polling", "documentation": "https://github.com/petretiandrea/home-assistant-tapo-p100", "issue_tracker": "https://github.com/petretiandrea/home-assistant-tapo-p100/issues", - "requirements": ["plugp100==3.12.0"], + "requirements": ["plugp100==3.13.1"], "dependencies": [], "integration_type": "device", "codeowners": ["@petretiandrea"] diff --git a/requirements_dev.txt b/requirements_dev.txt index 5a748f81..eb27a66e 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -1,5 +1,5 @@ homeassistant==2023.8.3 -plugp100==3.12.0 +plugp100==3.13.1 pre-commit==3.3.3 reorder-python-imports==3.10.0 flake8==6.1.0