Add Switchbot Water Leak Detector (BLE) ()

Co-authored-by: J. Nick Koston <nick@koston.org>
pull/133839/head
Lucas Gasenzer 2024-12-22 22:37:57 +01:00 committed by GitHub
parent 2d2b979c7d
commit c2358d5158
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 75 additions and 0 deletions
homeassistant/components/switchbot
tests/components/switchbot

View File

@ -64,6 +64,7 @@ PLATFORMS_BY_TYPE = {
SupportedModels.HUB2.value: [Platform.SENSOR],
SupportedModels.RELAY_SWITCH_1PM.value: [Platform.SWITCH, Platform.SENSOR],
SupportedModels.RELAY_SWITCH_1.value: [Platform.SWITCH],
SupportedModels.LEAK.value: [Platform.BINARY_SENSOR, Platform.SENSOR],
}
CLASS_BY_DEVICE = {
SupportedModels.CEILING_LIGHT.value: switchbot.SwitchbotCeilingLight,

View File

@ -64,6 +64,11 @@ BINARY_SENSOR_TYPES: dict[str, BinarySensorEntityDescription] = {
translation_key="door_auto_lock_paused",
entity_category=EntityCategory.DIAGNOSTIC,
),
"leak": BinarySensorEntityDescription(
key="leak",
name=None,
device_class=BinarySensorDeviceClass.MOISTURE,
),
}

View File

@ -33,6 +33,7 @@ class SupportedModels(StrEnum):
HUB2 = "hub2"
RELAY_SWITCH_1PM = "relay_switch_1pm"
RELAY_SWITCH_1 = "relay_switch_1"
LEAK = "leak"
CONNECTABLE_SUPPORTED_MODEL_TYPES = {
@ -58,6 +59,7 @@ NON_CONNECTABLE_SUPPORTED_MODEL_TYPES = {
SwitchbotModel.METER_PRO_C: SupportedModels.HYGROMETER_CO2,
SwitchbotModel.CONTACT_SENSOR: SupportedModels.CONTACT,
SwitchbotModel.MOTION_SENSOR: SupportedModels.MOTION,
SwitchbotModel.LEAK: SupportedModels.LEAK,
}
SUPPORTED_MODEL_TYPES = (

View File

@ -250,3 +250,27 @@ WORELAY_SWITCH_1PM_SERVICE_INFO = BluetoothServiceInfoBleak(
connectable=True,
tx_power=-127,
)
LEAK_SERVICE_INFO = BluetoothServiceInfoBleak(
name="Any",
manufacturer_data={
2409: b"\xd6407D1\x02V\x90\x00\x00\x00\x00\x1e\x05\x00\x00\x00\x00"
},
service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"&\\x00V"},
service_uuids=["cba20d00-224d-11e6-9fb8-0002a5d5c51b"],
address="AA:BB:CC:DD:EE:FF",
rssi=-60,
source="local",
advertisement=generate_advertisement_data(
local_name="Any",
manufacturer_data={
2409: b"\xd6407D1\x02V\x90\x00\x00\x00\x00\x1e\x05\x00\x00\x00\x00"
},
service_data={"0000fd3d-0000-1000-8000-00805f9b34fb": b"&\\x00V"},
service_uuids=["cba20d00-224d-11e6-9fb8-0002a5d5c51b"],
),
device=generate_ble_device("AA:BB:CC:DD:EE:FF", "Any"),
time=0,
connectable=False,
tx_power=-127,
)

View File

@ -22,6 +22,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from . import (
LEAK_SERVICE_INFO,
WOHAND_SERVICE_INFO,
WOMETERTHPC_SERVICE_INFO,
WORELAY_SWITCH_1PM_SERVICE_INFO,
@ -151,3 +152,45 @@ async def test_relay_switch_1pm_power_sensor(hass: HomeAssistant) -> None:
assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_leak_sensor(hass: HomeAssistant) -> None:
"""Test setting up the leak detector."""
await async_setup_component(hass, DOMAIN, {})
inject_bluetooth_service_info(hass, LEAK_SERVICE_INFO)
entry = MockConfigEntry(
domain=DOMAIN,
data={
CONF_ADDRESS: "aa:bb:cc:dd:ee:ff",
CONF_NAME: "test-name",
CONF_SENSOR_TYPE: "leak",
},
unique_id="aabbccddeeaa",
)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
battery_sensor = hass.states.get("sensor.test_name_battery")
battery_sensor_attrs = battery_sensor.attributes
assert battery_sensor.state == "86"
assert battery_sensor_attrs[ATTR_FRIENDLY_NAME] == "test-name Battery"
assert battery_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "%"
assert battery_sensor_attrs[ATTR_STATE_CLASS] == "measurement"
rssi_sensor = hass.states.get("sensor.test_name_bluetooth_signal")
rssi_sensor_attrs = rssi_sensor.attributes
assert rssi_sensor.state == "-60"
assert rssi_sensor_attrs[ATTR_FRIENDLY_NAME] == "test-name Bluetooth signal"
assert rssi_sensor_attrs[ATTR_UNIT_OF_MEASUREMENT] == "dBm"
leak_sensor = hass.states.get("binary_sensor.test_name")
leak_sensor_attrs = leak_sensor.attributes
assert leak_sensor.state == "off"
assert leak_sensor_attrs[ATTR_FRIENDLY_NAME] == "test-name"
assert await hass.config_entries.async_unload(entry.entry_id)
await hass.async_block_till_done()