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):