2021-09-23 22:10:34 +00:00
|
|
|
"""Support for SwitchBot binary sensors."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
BinarySensorEntity,
|
|
|
|
BinarySensorEntityDescription,
|
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-07-24 16:38:45 +00:00
|
|
|
from homeassistant.const import CONF_ADDRESS, CONF_NAME
|
2021-09-23 22:10:34 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-12-20 17:03:08 +00:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
2021-09-23 22:10:34 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
2022-07-24 16:38:45 +00:00
|
|
|
from .const import DOMAIN
|
2021-09-23 22:10:34 +00:00
|
|
|
from .coordinator import SwitchbotDataUpdateCoordinator
|
|
|
|
from .entity import SwitchbotEntity
|
|
|
|
|
|
|
|
PARALLEL_UPDATES = 1
|
|
|
|
|
|
|
|
BINARY_SENSOR_TYPES: dict[str, BinarySensorEntityDescription] = {
|
|
|
|
"calibration": BinarySensorEntityDescription(
|
|
|
|
key="calibration",
|
2021-12-20 17:03:08 +00:00
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2021-09-23 22:10:34 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
|
|
|
"""Set up Switchbot curtain based on a config entry."""
|
2022-07-24 16:38:45 +00:00
|
|
|
coordinator: SwitchbotDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
unique_id = entry.unique_id
|
|
|
|
assert unique_id is not None
|
2021-09-23 22:10:34 +00:00
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
SwitchBotBinarySensor(
|
|
|
|
coordinator,
|
2022-07-24 16:38:45 +00:00
|
|
|
unique_id,
|
2021-09-23 22:10:34 +00:00
|
|
|
binary_sensor,
|
2022-07-24 16:38:45 +00:00
|
|
|
entry.data[CONF_ADDRESS],
|
2021-09-23 22:10:34 +00:00
|
|
|
entry.data[CONF_NAME],
|
|
|
|
)
|
2022-07-24 16:38:45 +00:00
|
|
|
for binary_sensor in coordinator.data["data"]
|
2021-09-23 22:10:34 +00:00
|
|
|
if binary_sensor in BINARY_SENSOR_TYPES
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class SwitchBotBinarySensor(SwitchbotEntity, BinarySensorEntity):
|
|
|
|
"""Representation of a Switchbot binary sensor."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: SwitchbotDataUpdateCoordinator,
|
2022-07-24 16:38:45 +00:00
|
|
|
unique_id: str,
|
2021-09-23 22:10:34 +00:00
|
|
|
binary_sensor: str,
|
|
|
|
mac: str,
|
|
|
|
switchbot_name: str,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the Switchbot sensor."""
|
2022-07-24 16:38:45 +00:00
|
|
|
super().__init__(coordinator, unique_id, mac, name=switchbot_name)
|
2021-09-23 22:10:34 +00:00
|
|
|
self._sensor = binary_sensor
|
2022-07-24 16:38:45 +00:00
|
|
|
self._attr_unique_id = f"{unique_id}-{binary_sensor}"
|
2021-09-23 22:10:34 +00:00
|
|
|
self._attr_name = f"{switchbot_name} {binary_sensor.title()}"
|
|
|
|
self.entity_description = BINARY_SENSOR_TYPES[binary_sensor]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self.data["data"][self._sensor]
|