Make Withings bed presence sensor dynamic (#102058)
* Make Withings bed presence sensor dynamic * Make Withings bed presence sensor dynamic * Update homeassistant/components/withings/binary_sensor.py Co-authored-by: J. Nick Koston <nick@koston.org> --------- Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
268425b5e3
commit
54ba376b4b
2 changed files with 31 additions and 4 deletions
|
@ -1,13 +1,17 @@
|
||||||
"""Sensors flow for Withings."""
|
"""Sensors flow for Withings."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
BinarySensorDeviceClass,
|
BinarySensorDeviceClass,
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
import homeassistant.helpers.entity_registry as er
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import WithingsBedPresenceDataUpdateCoordinator
|
from .coordinator import WithingsBedPresenceDataUpdateCoordinator
|
||||||
|
@ -22,9 +26,22 @@ async def async_setup_entry(
|
||||||
"""Set up the sensor config entry."""
|
"""Set up the sensor config entry."""
|
||||||
coordinator = hass.data[DOMAIN][entry.entry_id].bed_presence_coordinator
|
coordinator = hass.data[DOMAIN][entry.entry_id].bed_presence_coordinator
|
||||||
|
|
||||||
entities = [WithingsBinarySensor(coordinator)]
|
ent_reg = er.async_get(hass)
|
||||||
|
|
||||||
async_add_entities(entities)
|
callback: Callable[[], None] | None = None
|
||||||
|
|
||||||
|
def _async_add_bed_presence_entity() -> None:
|
||||||
|
"""Add bed presence entity."""
|
||||||
|
async_add_entities([WithingsBinarySensor(coordinator)])
|
||||||
|
if callback:
|
||||||
|
callback()
|
||||||
|
|
||||||
|
if ent_reg.async_get_entity_id(
|
||||||
|
Platform.BINARY_SENSOR, DOMAIN, f"withings_{entry.unique_id}_in_bed"
|
||||||
|
):
|
||||||
|
_async_add_bed_presence_entity()
|
||||||
|
else:
|
||||||
|
callback = coordinator.async_add_listener(_async_add_bed_presence_entity)
|
||||||
|
|
||||||
|
|
||||||
class WithingsBinarySensor(WithingsEntity, BinarySensorEntity):
|
class WithingsBinarySensor(WithingsEntity, BinarySensorEntity):
|
||||||
|
|
|
@ -22,6 +22,7 @@ async def test_binary_sensor(
|
||||||
webhook_config_entry: MockConfigEntry,
|
webhook_config_entry: MockConfigEntry,
|
||||||
hass_client_no_auth: ClientSessionGenerator,
|
hass_client_no_auth: ClientSessionGenerator,
|
||||||
freezer: FrozenDateTimeFactory,
|
freezer: FrozenDateTimeFactory,
|
||||||
|
caplog: pytest.LogCaptureFixture,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test binary sensor."""
|
"""Test binary sensor."""
|
||||||
await setup_integration(hass, webhook_config_entry)
|
await setup_integration(hass, webhook_config_entry)
|
||||||
|
@ -31,7 +32,7 @@ async def test_binary_sensor(
|
||||||
|
|
||||||
entity_id = "binary_sensor.henk_in_bed"
|
entity_id = "binary_sensor.henk_in_bed"
|
||||||
|
|
||||||
assert hass.states.get(entity_id).state == STATE_UNKNOWN
|
assert hass.states.get(entity_id) is None
|
||||||
|
|
||||||
resp = await call_webhook(
|
resp = await call_webhook(
|
||||||
hass,
|
hass,
|
||||||
|
@ -53,6 +54,15 @@ async def test_binary_sensor(
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert hass.states.get(entity_id).state == STATE_OFF
|
assert hass.states.get(entity_id).state == STATE_OFF
|
||||||
|
|
||||||
|
await hass.config_entries.async_reload(webhook_config_entry.entry_id)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert hass.states.get(entity_id).state == STATE_UNKNOWN
|
||||||
|
assert (
|
||||||
|
"Platform withings does not generate unique IDs. ID withings_12345_in_bed already exists - ignoring binary_sensor.henk_in_bed"
|
||||||
|
not in caplog.text
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_polling_binary_sensor(
|
async def test_polling_binary_sensor(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
@ -67,7 +77,7 @@ async def test_polling_binary_sensor(
|
||||||
|
|
||||||
entity_id = "binary_sensor.henk_in_bed"
|
entity_id = "binary_sensor.henk_in_bed"
|
||||||
|
|
||||||
assert hass.states.get(entity_id).state == STATE_UNKNOWN
|
assert hass.states.get(entity_id) is None
|
||||||
|
|
||||||
with pytest.raises(ClientResponseError):
|
with pytest.raises(ClientResponseError):
|
||||||
await call_webhook(
|
await call_webhook(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue