Skip to content

Commit

Permalink
feat: added water leak sensor (#595)
Browse files Browse the repository at this point in the history
  • Loading branch information
petretiandrea authored Nov 2, 2023
1 parent 32e6fa9 commit 43a000a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 4 deletions.
7 changes: 6 additions & 1 deletion custom_components/tapo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions custom_components/tapo/hub/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -93,4 +114,5 @@ def is_on(self) -> bool:
S200ButtonDevice: [LowBatterySensor],
T100MotionSensor: [MotionSensor, LowBatterySensor],
SwitchChildDevice: [LowBatterySensor],
WaterLeakDevice: [WaterLeakSensor, LowBatterySensor],
}
5 changes: 4 additions & 1 deletion custom_components/tapo/hub/sensor.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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


Expand Down Expand Up @@ -147,4 +149,5 @@ def native_value(self) -> Union[StateType, date, datetime]:
T110SmartDoor: [ReportIntervalDiagnostic],
S200ButtonDevice: [ReportIntervalDiagnostic],
T100MotionSensor: [ReportIntervalDiagnostic],
WaterLeakDevice: [ReportIntervalDiagnostic],
}
7 changes: 7 additions & 0 deletions custom_components/tapo/hub/tapo_hub_child_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,13 +28,15 @@
| S200ButtonDevice
| T100MotionSensor
| SwitchChildDevice
| WaterLeakSensor
)
HubChildCommonState = (
T31DeviceState
| T110SmartDoorState
| S200BDeviceState
| T100MotionSensorState
| SwitchChildDeviceState
| LeakDeviceState
)


Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/tapo/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 43a000a

Please sign in to comment.