2020-06-16 18:16:18 +00:00
|
|
|
"""Sensors flow for Withings."""
|
2021-03-18 14:08:35 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-12-16 19:04:07 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
2022-12-16 14:35:03 +00:00
|
|
|
from withings_api.common import NotifyAppli
|
|
|
|
|
2020-06-16 18:16:18 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
2021-12-16 08:09:28 +00:00
|
|
|
BinarySensorDeviceClass,
|
2020-07-07 17:17:04 +00:00
|
|
|
BinarySensorEntity,
|
2022-12-16 19:04:07 +00:00
|
|
|
BinarySensorEntityDescription,
|
2020-06-16 18:16:18 +00:00
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-30 18:38:59 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-06-16 18:16:18 +00:00
|
|
|
|
2022-12-16 14:35:03 +00:00
|
|
|
from .common import (
|
|
|
|
BaseWithingsSensor,
|
|
|
|
UpdateType,
|
2022-12-16 19:04:07 +00:00
|
|
|
WithingsEntityDescription,
|
2022-12-16 14:35:03 +00:00
|
|
|
async_get_data_manager,
|
|
|
|
)
|
|
|
|
from .const import Measurement
|
|
|
|
|
2022-12-16 19:04:07 +00:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class WithingsBinarySensorEntityDescription(
|
|
|
|
BinarySensorEntityDescription, WithingsEntityDescription
|
|
|
|
):
|
|
|
|
"""Immutable class for describing withings binary sensor data."""
|
|
|
|
|
|
|
|
|
2022-12-16 14:35:03 +00:00
|
|
|
BINARY_SENSORS = [
|
|
|
|
# Webhook measurements.
|
2022-12-16 19:04:07 +00:00
|
|
|
WithingsBinarySensorEntityDescription(
|
|
|
|
key=Measurement.IN_BED.value,
|
|
|
|
measurement=Measurement.IN_BED,
|
|
|
|
measure_type=NotifyAppli.BED_IN,
|
|
|
|
name="In bed",
|
|
|
|
icon="mdi:bed",
|
|
|
|
update_type=UpdateType.WEBHOOK,
|
|
|
|
device_class=BinarySensorDeviceClass.OCCUPANCY,
|
2022-12-16 14:35:03 +00:00
|
|
|
),
|
|
|
|
]
|
2020-06-16 18:16:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
2021-04-30 18:38:59 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2020-06-16 18:16:18 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up the sensor config entry."""
|
2022-12-16 14:35:03 +00:00
|
|
|
data_manager = await async_get_data_manager(hass, entry)
|
|
|
|
|
|
|
|
entities = [
|
|
|
|
WithingsHealthBinarySensor(data_manager, attribute)
|
|
|
|
for attribute in BINARY_SENSORS
|
|
|
|
]
|
2020-06-16 18:16:18 +00:00
|
|
|
|
|
|
|
async_add_entities(entities, True)
|
|
|
|
|
|
|
|
|
2020-07-07 17:17:04 +00:00
|
|
|
class WithingsHealthBinarySensor(BaseWithingsSensor, BinarySensorEntity):
|
2020-06-16 18:16:18 +00:00
|
|
|
"""Implementation of a Withings sensor."""
|
|
|
|
|
2022-12-16 19:04:07 +00:00
|
|
|
entity_description: WithingsBinarySensorEntityDescription
|
2021-06-01 19:43:55 +00:00
|
|
|
|
2020-06-16 18:16:18 +00:00
|
|
|
@property
|
2022-07-11 17:14:23 +00:00
|
|
|
def is_on(self) -> bool | None:
|
2020-06-16 18:16:18 +00:00
|
|
|
"""Return true if the binary sensor is on."""
|
|
|
|
return self._state_data
|