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:
Joost Lekkerkerker 2023-10-23 09:01:58 +02:00 committed by GitHub
parent 268425b5e3
commit 54ba376b4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 4 deletions

View file

@ -1,13 +1,17 @@
"""Sensors flow for Withings."""
from __future__ import annotations
from collections.abc import Callable
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
import homeassistant.helpers.entity_registry as er
from .const import DOMAIN
from .coordinator import WithingsBedPresenceDataUpdateCoordinator
@ -22,9 +26,22 @@ async def async_setup_entry(
"""Set up the sensor config entry."""
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):

View file

@ -22,6 +22,7 @@ async def test_binary_sensor(
webhook_config_entry: MockConfigEntry,
hass_client_no_auth: ClientSessionGenerator,
freezer: FrozenDateTimeFactory,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test binary sensor."""
await setup_integration(hass, webhook_config_entry)
@ -31,7 +32,7 @@ async def test_binary_sensor(
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(
hass,
@ -53,6 +54,15 @@ async def test_binary_sensor(
await hass.async_block_till_done()
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(
hass: HomeAssistant,
@ -67,7 +77,7 @@ async def test_polling_binary_sensor(
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):
await call_webhook(