Add Reolink smart ai binary sensors (#140408)

* Add Crossline smart AI binary sensor

* Add intrusion, lingering, forgotten item, item taken detection

* Use unique_index instead of location for unique_id

* Add test

* Apply suggestions from code review

Co-authored-by: Abílio Costa <abmantis@users.noreply.github.com>

* Name changes

* Update homeassistant/components/reolink/binary_sensor.py

Co-authored-by: Abílio Costa <abmantis@users.noreply.github.com>

* Use smart_type instead of key

* Use occupancy translation instead of gas (point to the same thing).

* Revert "Use occupancy translation instead of gas (point to the same thing)."

This reverts commit 9caf796585e1cffdea6e66f16824fe8e34d03276.

* fix styling

---------

Co-authored-by: Abílio Costa <abmantis@users.noreply.github.com>
This commit is contained in:
starkillerOG 2025-03-17 15:49:13 +01:00 committed by GitHub
parent 76aef5be9f
commit 18bd8b561a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 378 additions and 5 deletions

View File

@ -25,7 +25,11 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .entity import ReolinkChannelCoordinatorEntity, ReolinkChannelEntityDescription
from .entity import (
ReolinkChannelCoordinatorEntity,
ReolinkChannelEntityDescription,
ReolinkEntityDescription,
)
from .util import ReolinkConfigEntry, ReolinkData
PARALLEL_UPDATES = 0
@ -41,6 +45,18 @@ class ReolinkBinarySensorEntityDescription(
value: Callable[[Host, int], bool]
@dataclass(frozen=True, kw_only=True)
class ReolinkSmartAIBinarySensorEntityDescription(
BinarySensorEntityDescription,
ReolinkEntityDescription,
):
"""A class that describes Smart AI binary sensor entities."""
smart_type: str
value: Callable[[Host, int, int], bool]
supported: Callable[[Host, int, int], bool] = lambda api, ch, loc: True
BINARY_PUSH_SENSORS = (
ReolinkBinarySensorEntityDescription(
key="motion",
@ -121,6 +137,142 @@ BINARY_SENSORS = (
),
)
BINARY_SMART_AI_SENSORS = (
ReolinkSmartAIBinarySensorEntityDescription(
key="crossline_person",
smart_type="crossline",
cmd_id=33,
translation_key="crossline_person",
value=lambda api, ch, loc: (
api.baichuan.smart_ai_state(ch, "crossline", loc, "people")
),
supported=lambda api, ch, loc: (
api.supported(ch, "ai_crossline")
and "people" in api.baichuan.smart_ai_type_list(ch, "crossline", loc)
),
),
ReolinkSmartAIBinarySensorEntityDescription(
key="crossline_vehicle",
smart_type="crossline",
cmd_id=33,
translation_key="crossline_vehicle",
value=lambda api, ch, loc: (
api.baichuan.smart_ai_state(ch, "crossline", loc, "vehicle")
),
supported=lambda api, ch, loc: (
api.supported(ch, "ai_crossline")
and "vehicle" in api.baichuan.smart_ai_type_list(ch, "crossline", loc)
),
),
ReolinkSmartAIBinarySensorEntityDescription(
key="crossline_dog_cat",
smart_type="crossline",
cmd_id=33,
translation_key="crossline_dog_cat",
value=lambda api, ch, loc: (
api.baichuan.smart_ai_state(ch, "crossline", loc, "dog_cat")
),
supported=lambda api, ch, loc: (
api.supported(ch, "ai_crossline")
and "dog_cat" in api.baichuan.smart_ai_type_list(ch, "crossline", loc)
),
),
ReolinkSmartAIBinarySensorEntityDescription(
key="intrusion_person",
smart_type="intrusion",
cmd_id=33,
translation_key="intrusion_person",
value=lambda api, ch, loc: (
api.baichuan.smart_ai_state(ch, "intrusion", loc, "people")
),
supported=lambda api, ch, loc: (
api.supported(ch, "ai_intrusion")
and "people" in api.baichuan.smart_ai_type_list(ch, "intrusion", loc)
),
),
ReolinkSmartAIBinarySensorEntityDescription(
key="intrusion_vehicle",
smart_type="intrusion",
cmd_id=33,
translation_key="intrusion_vehicle",
value=lambda api, ch, loc: (
api.baichuan.smart_ai_state(ch, "intrusion", loc, "vehicle")
),
supported=lambda api, ch, loc: (
api.supported(ch, "ai_intrusion")
and "vehicle" in api.baichuan.smart_ai_type_list(ch, "intrusion", loc)
),
),
ReolinkSmartAIBinarySensorEntityDescription(
key="intrusion_dog_cat",
smart_type="intrusion",
cmd_id=33,
translation_key="intrusion_dog_cat",
value=lambda api, ch, loc: (
api.baichuan.smart_ai_state(ch, "intrusion", loc, "dog_cat")
),
supported=lambda api, ch, loc: (
api.supported(ch, "ai_intrusion")
and "dog_cat" in api.baichuan.smart_ai_type_list(ch, "intrusion", loc)
),
),
ReolinkSmartAIBinarySensorEntityDescription(
key="linger_person",
smart_type="loitering",
cmd_id=33,
translation_key="linger_person",
value=lambda api, ch, loc: (
api.baichuan.smart_ai_state(ch, "loitering", loc, "people")
),
supported=lambda api, ch, loc: (
api.supported(ch, "ai_linger")
and "people" in api.baichuan.smart_ai_type_list(ch, "loitering", loc)
),
),
ReolinkSmartAIBinarySensorEntityDescription(
key="linger_vehicle",
smart_type="loitering",
cmd_id=33,
translation_key="linger_vehicle",
value=lambda api, ch, loc: (
api.baichuan.smart_ai_state(ch, "loitering", loc, "vehicle")
),
supported=lambda api, ch, loc: (
api.supported(ch, "ai_linger")
and "vehicle" in api.baichuan.smart_ai_type_list(ch, "loitering", loc)
),
),
ReolinkSmartAIBinarySensorEntityDescription(
key="linger_dog_cat",
smart_type="loitering",
cmd_id=33,
translation_key="linger_dog_cat",
value=lambda api, ch, loc: (
api.baichuan.smart_ai_state(ch, "loitering", loc, "dog_cat")
),
supported=lambda api, ch, loc: (
api.supported(ch, "ai_linger")
and "dog_cat" in api.baichuan.smart_ai_type_list(ch, "loitering", loc)
),
),
ReolinkSmartAIBinarySensorEntityDescription(
key="forgotten_item",
smart_type="legacy",
cmd_id=33,
translation_key="forgotten_item",
value=lambda api, ch, loc: (api.baichuan.smart_ai_state(ch, "legacy", loc)),
supported=lambda api, ch, loc: api.supported(ch, "ai_forgotten_item"),
),
ReolinkSmartAIBinarySensorEntityDescription(
key="taken_item",
smart_type="loss",
cmd_id=33,
translation_key="taken_item",
value=lambda api, ch, loc: (api.baichuan.smart_ai_state(ch, "loss", loc)),
supported=lambda api, ch, loc: api.supported(ch, "ai_taken_item"),
),
)
async def async_setup_entry(
hass: HomeAssistant,
@ -129,18 +281,29 @@ async def async_setup_entry(
) -> None:
"""Set up a Reolink IP Camera."""
reolink_data: ReolinkData = config_entry.runtime_data
api = reolink_data.host.api
entities: list[ReolinkBinarySensorEntity] = []
for channel in reolink_data.host.api.channels:
entities: list[ReolinkBinarySensorEntity | ReolinkSmartAIBinarySensorEntity] = []
for channel in api.channels:
entities.extend(
ReolinkPushBinarySensorEntity(reolink_data, channel, entity_description)
for entity_description in BINARY_PUSH_SENSORS
if entity_description.supported(reolink_data.host.api, channel)
if entity_description.supported(api, channel)
)
entities.extend(
ReolinkBinarySensorEntity(reolink_data, channel, entity_description)
for entity_description in BINARY_SENSORS
if entity_description.supported(reolink_data.host.api, channel)
if entity_description.supported(api, channel)
)
entities.extend(
ReolinkSmartAIBinarySensorEntity(
reolink_data, channel, location, entity_description
)
for entity_description in BINARY_SMART_AI_SENSORS
for location in api.baichuan.smart_location_list(
channel, entity_description.key
)
if entity_description.supported(api, channel, location)
)
async_add_entities(entities)
@ -198,3 +361,40 @@ class ReolinkPushBinarySensorEntity(ReolinkBinarySensorEntity):
async def _async_handle_event(self, event: str) -> None:
"""Handle incoming event for motion detection."""
self.async_write_ha_state()
class ReolinkSmartAIBinarySensorEntity(
ReolinkChannelCoordinatorEntity, BinarySensorEntity
):
"""Binary-sensor class for Reolink IP camera Smart AI sensors."""
entity_description: ReolinkSmartAIBinarySensorEntityDescription
def __init__(
self,
reolink_data: ReolinkData,
channel: int,
location: int,
entity_description: ReolinkSmartAIBinarySensorEntityDescription,
) -> None:
"""Initialize Reolink binary sensor."""
self.entity_description = entity_description
super().__init__(reolink_data, channel)
unique_index = self._host.api.baichuan.smart_ai_index(
channel, entity_description.smart_type, location
)
self._attr_unique_id = f"{self._attr_unique_id}_{unique_index}"
self._location = location
self._attr_translation_placeholders = {
"zone_name": self._host.api.baichuan.smart_ai_name(
channel, entity_description.smart_type, location
)
}
@property
def is_on(self) -> bool:
"""State of the sensor."""
return self.entity_description.value(
self._host.api, self._channel, self._location
)

View File

@ -54,6 +54,72 @@
"state": {
"on": "mdi:sleep"
}
},
"crossline_person": {
"default": "mdi:fence",
"state": {
"on": "mdi:fence-electric"
}
},
"crossline_vehicle": {
"default": "mdi:fence",
"state": {
"on": "mdi:fence-electric"
}
},
"crossline_dog_cat": {
"default": "mdi:fence",
"state": {
"on": "mdi:fence-electric"
}
},
"intrusion_person": {
"default": "mdi:location-enter",
"state": {
"on": "mdi:alert-circle-outline"
}
},
"intrusion_vehicle": {
"default": "mdi:location-enter",
"state": {
"on": "mdi:alert-circle-outline"
}
},
"intrusion_dog_cat": {
"default": "mdi:location-enter",
"state": {
"on": "mdi:alert-circle-outline"
}
},
"linger_person": {
"default": "mdi:account-switch",
"state": {
"on": "mdi:account-alert"
}
},
"linger_vehicle": {
"default": "mdi:account-switch",
"state": {
"on": "mdi:account-alert"
}
},
"linger_dog_cat": {
"default": "mdi:account-switch",
"state": {
"on": "mdi:account-alert"
}
},
"forgotten_item": {
"default": "mdi:package-variant-closed-plus",
"state": {
"on": "mdi:package-variant-closed-check"
}
},
"taken_item": {
"default": "mdi:package-variant-closed-minus",
"state": {
"on": "mdi:package-variant-closed-check"
}
}
},
"button": {

View File

@ -337,6 +337,83 @@
"off": "Awake",
"on": "Sleeping"
}
},
"crossline_person": {
"name": "Crossline {zone_name} person",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "[%key:component::binary_sensor::entity_component::gas::state::on%]"
}
},
"crossline_vehicle": {
"name": "Crossline {zone_name} vehicle",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "[%key:component::binary_sensor::entity_component::gas::state::on%]"
}
},
"crossline_dog_cat": {
"name": "Crossline {zone_name} animal",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "[%key:component::binary_sensor::entity_component::gas::state::on%]"
}
},
"intrusion_person": {
"name": "Intrusion {zone_name} person",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "[%key:component::binary_sensor::entity_component::gas::state::on%]"
}
},
"intrusion_vehicle": {
"name": "Intrusion {zone_name} vehicle",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "[%key:component::binary_sensor::entity_component::gas::state::on%]"
}
},
"intrusion_dog_cat": {
"name": "Intrusion {zone_name} animal",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "[%key:component::binary_sensor::entity_component::gas::state::on%]"
}
},
"linger_person": {
"name": "Linger {zone_name} person",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "[%key:component::binary_sensor::entity_component::gas::state::on%]"
}
},
"linger_vehicle": {
"name": "Linger {zone_name} vehicle",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "[%key:component::binary_sensor::entity_component::gas::state::on%]"
}
},
"linger_dog_cat": {
"name": "Linger {zone_name} animal",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "[%key:component::binary_sensor::entity_component::gas::state::on%]"
}
},
"forgotten_item": {
"name": "Item forgotten {zone_name}",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "[%key:component::binary_sensor::entity_component::gas::state::on%]"
}
},
"taken_item": {
"name": "Item taken {zone_name}",
"state": {
"off": "[%key:component::binary_sensor::entity_component::gas::state::off%]",
"on": "[%key:component::binary_sensor::entity_component::gas::state::on%]"
}
}
},
"button": {

View File

@ -146,6 +146,10 @@ def reolink_connect_class() -> Generator[MagicMock]:
0: {"chnID": 0, "aitype": 34615},
"Host": {"pushAlarm": 7},
}
host_mock.baichuan.smart_location_list.return_value = [0]
host_mock.baichuan.smart_ai_type_list.return_value = ["people"]
host_mock.baichuan.smart_ai_index.return_value = 1
host_mock.baichuan.smart_ai_name.return_value = "zone1"
yield host_mock_class

View File

@ -51,6 +51,32 @@ async def test_motion_sensor(
assert hass.states.get(entity_id).state == STATE_ON
async def test_smart_ai_sensor(
hass: HomeAssistant,
hass_client_no_auth: ClientSessionGenerator,
freezer: FrozenDateTimeFactory,
config_entry: MockConfigEntry,
reolink_connect: MagicMock,
) -> None:
"""Test smart ai binary sensor entity."""
reolink_connect.model = TEST_HOST_MODEL
reolink_connect.baichuan.smart_ai_state.return_value = True
with patch("homeassistant.components.reolink.PLATFORMS", [Platform.BINARY_SENSOR]):
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert config_entry.state is ConfigEntryState.LOADED
entity_id = f"{Platform.BINARY_SENSOR}.{TEST_NVR_NAME}_crossline_zone1_person"
assert hass.states.get(entity_id).state == STATE_ON
reolink_connect.baichuan.smart_ai_state.return_value = False
freezer.tick(DEVICE_UPDATE_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_OFF
async def test_tcp_callback(
hass: HomeAssistant,
config_entry: MockConfigEntry,