Add motion binary sensor to Fully Kiosk Browser (#195203)

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Jake Frietsch
2026-12-01 20:03:01 +00:00
committed by GitHub
co-authored by Claude Fable 5.2
parent dfddbf8df8
commit 659d63e54a
2 changed files with 181 additions and 5 deletions
@@ -1,6 +1,7 @@
"""Fully Kiosk Browser sensor."""
from typing import override
from datetime import datetime
from typing import Any, override
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
@@ -8,8 +9,9 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntityDescription,
)
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.event import async_call_later
from .coordinator import FullyKioskConfigEntry, FullyKioskDataUpdateCoordinator
from .entity import FullyKioskEntity
@@ -33,6 +35,15 @@ SENSORS: tuple[BinarySensorEntityDescription, ...] = (
),
)
MOTION_SENSOR = BinarySensorEntityDescription(
key="motion",
device_class=BinarySensorDeviceClass.MOTION,
)
MOTION_EVENT = "onMotion"
# Fully Kiosk Browser only reports when motion is detected, not when it stops.
MOTION_OFF_DELAY = 10
async def async_setup_entry(
hass: HomeAssistant,
@@ -42,11 +53,16 @@ async def async_setup_entry(
"""Set up the Fully Kiosk Browser sensor."""
coordinator = config_entry.runtime_data
async_add_entities(
entities: list[BinarySensorEntity] = [
FullyBinarySensor(coordinator, description)
for description in SENSORS
if description.key in coordinator.data
)
]
# Motion is only reported through MQTT events
if coordinator.data["settings"].get("mqttEnabled"):
entities.append(FullyMotionBinarySensor(coordinator))
async_add_entities(entities)
class FullyBinarySensor(FullyKioskEntity, BinarySensorEntity):
@@ -69,3 +85,62 @@ class FullyBinarySensor(FullyKioskEntity, BinarySensorEntity):
if (value := self.coordinator.data.get(self.entity_description.key)) is None:
return None
return bool(value)
class FullyMotionBinarySensor(FullyKioskEntity, BinarySensorEntity):
"""Representation of the Fully Kiosk Browser motion binary sensor."""
entity_description = MOTION_SENSOR
_attr_is_on = False
def __init__(self, coordinator: FullyKioskDataUpdateCoordinator) -> None:
"""Initialize the motion binary sensor."""
super().__init__(coordinator)
self._attr_unique_id = (
f"{coordinator.data['deviceID']}-{self.entity_description.key}"
)
self._motion_subscription: CALLBACK_TYPE | None = None
self._cancel_motion_off: CALLBACK_TYPE | None = None
@override
async def async_added_to_hass(self) -> None:
"""When entity is added to hass."""
await super().async_added_to_hass()
self._motion_subscription = await self.mqtt_subscribe(
MOTION_EVENT, self._motion_detected
)
@property
@override
def available(self) -> bool:
"""Return if motion events can be received."""
return super().available and self._motion_subscription is not None
@override
async def async_will_remove_from_hass(self) -> None:
"""Close MQTT subscription and cancel the off timer when removed."""
await super().async_will_remove_from_hass()
if self._motion_subscription is not None:
self._motion_subscription()
self._motion_subscription = None
if self._cancel_motion_off is not None:
self._cancel_motion_off()
self._cancel_motion_off = None
@callback
def _motion_detected(self, **kwargs: Any) -> None:
"""Turn on and restart the timer that turns the sensor off again."""
if self._cancel_motion_off is not None:
self._cancel_motion_off()
self._cancel_motion_off = async_call_later(
self.hass, MOTION_OFF_DELAY, self._motion_cleared
)
self._attr_is_on = True
self.async_write_ha_state()
@callback
def _motion_cleared(self, now: datetime) -> None:
"""Turn off when no motion has been reported for a while."""
self._cancel_motion_off = None
self._attr_is_on = False
self.async_write_ha_state()
@@ -4,13 +4,20 @@ from unittest.mock import MagicMock
from freezegun.api import FrozenDateTimeFactory
from fullykiosk import FullyKioskError
from pytest_homeassistant.common import MockConfigEntry, async_fire_time_changed
from pytest_homeassistant.common import (
MockConfigEntry,
async_fire_mqtt_message,
async_fire_time_changed,
)
from pytest_homeassistant.typing import MqttMockHAClient
from homeassistant.components.binary_sensor import BinarySensorDeviceClass
from homeassistant.components.fully_kiosk.binary_sensor import MOTION_OFF_DELAY
from homeassistant.components.fully_kiosk.const import DOMAIN, UPDATE_INTERVAL
from homeassistant.const import (
ATTR_DEVICE_CLASS,
ATTR_FRIENDLY_NAME,
STATE_OFF,
STATE_ON,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
@@ -93,3 +100,97 @@ async def test_binary_sensors(
state = hass.states.get("binary_sensor.amazon_fire_plugged_in")
assert state
assert state.state == STATE_UNAVAILABLE
MOTION_ENTITY_ID = "binary_sensor.amazon_fire_motion"
MOTION_TOPIC = "fully/event/onMotion/abcdef-123456"
MOTION_PAYLOAD = '{"deviceId": "abcdef-123456","event": "onMotion"}'
async def test_motion_binary_sensor(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
freezer: FrozenDateTimeFactory,
mock_fully_kiosk: MagicMock,
mqtt_mock: MqttMockHAClient,
init_integration: MockConfigEntry,
) -> None:
"""Test the motion binary sensor is updated by MQTT events."""
assert any(
call.args[0] == MOTION_TOPIC
for call in mqtt_mock.async_subscribe.call_args_list
)
state = hass.states.get(MOTION_ENTITY_ID)
assert state
assert state.state == STATE_OFF
assert state.attributes.get(ATTR_DEVICE_CLASS) == BinarySensorDeviceClass.MOTION
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "Amazon Fire Motion"
entry = entity_registry.async_get(MOTION_ENTITY_ID)
assert entry
assert entry.unique_id == "abcdef-123456-motion"
assert entry.entity_category is None
async_fire_mqtt_message(hass, MOTION_TOPIC, MOTION_PAYLOAD)
state = hass.states.get(MOTION_ENTITY_ID)
assert state
assert state.state == STATE_ON
# More motion before the off delay expired restarts the off delay
freezer.tick(MOTION_OFF_DELAY - 1)
async_fire_time_changed(hass)
await hass.async_block_till_done()
async_fire_mqtt_message(hass, MOTION_TOPIC, MOTION_PAYLOAD)
freezer.tick(MOTION_OFF_DELAY - 1)
async_fire_time_changed(hass)
await hass.async_block_till_done()
state = hass.states.get(MOTION_ENTITY_ID)
assert state
assert state.state == STATE_ON
freezer.tick(1)
async_fire_time_changed(hass)
await hass.async_block_till_done()
state = hass.states.get(MOTION_ENTITY_ID)
assert state
assert state.state == STATE_OFF
# Unloading while motion is detected cancels the off delay
async_fire_mqtt_message(hass, MOTION_TOPIC, MOTION_PAYLOAD)
assert await hass.config_entries.async_unload(init_integration.entry_id)
await hass.async_block_till_done()
state = hass.states.get(MOTION_ENTITY_ID)
assert state
assert state.state == STATE_UNAVAILABLE
async def test_motion_binary_sensor_mqtt_disabled(
hass: HomeAssistant,
mock_fully_kiosk: MagicMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test no motion binary sensor is added when MQTT is disabled in Fully."""
mock_fully_kiosk.getSettings.return_value["mqttEnabled"] = False
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert hass.states.get("binary_sensor.amazon_fire_plugged_in")
assert hass.states.get(MOTION_ENTITY_ID) is None
async def test_motion_binary_sensor_mqtt_not_set_up(
hass: HomeAssistant,
mock_fully_kiosk: MagicMock,
init_integration: MockConfigEntry,
) -> None:
"""Test the motion binary sensor is unavailable without MQTT in Home Assistant."""
state = hass.states.get(MOTION_ENTITY_ID)
assert state
assert state.state == STATE_UNAVAILABLE
state = hass.states.get("binary_sensor.amazon_fire_plugged_in")
assert state
assert state.state == STATE_ON