diff --git a/homeassistant/components/withings/binary_sensor.py b/homeassistant/components/withings/binary_sensor.py index 69af68e988b..1317befcf3f 100644 --- a/homeassistant/components/withings/binary_sensor.py +++ b/homeassistant/components/withings/binary_sensor.py @@ -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): diff --git a/tests/components/withings/test_binary_sensor.py b/tests/components/withings/test_binary_sensor.py index 5054bf46daa..c56b14ae893 100644 --- a/tests/components/withings/test_binary_sensor.py +++ b/tests/components/withings/test_binary_sensor.py @@ -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(