|
| 1 | +"""Support for SwitchBot binary sensors.""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from homeassistant.components.binary_sensor import ( |
| 5 | + BinarySensorEntity, |
| 6 | + BinarySensorEntityDescription, |
| 7 | +) |
| 8 | +from homeassistant.config_entries import ConfigEntry |
| 9 | +from homeassistant.const import CONF_MAC, CONF_NAME |
| 10 | +from homeassistant.core import HomeAssistant |
| 11 | +from homeassistant.helpers.entity_platform import AddEntitiesCallback |
| 12 | + |
| 13 | +from .const import DATA_COORDINATOR, DOMAIN |
| 14 | +from .coordinator import SwitchbotDataUpdateCoordinator |
| 15 | +from .entity import SwitchbotEntity |
| 16 | + |
| 17 | +PARALLEL_UPDATES = 1 |
| 18 | + |
| 19 | +BINARY_SENSOR_TYPES: dict[str, BinarySensorEntityDescription] = { |
| 20 | + "calibration": BinarySensorEntityDescription( |
| 21 | + key="calibration", |
| 22 | + ), |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +async def async_setup_entry( |
| 27 | + hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback |
| 28 | +) -> None: |
| 29 | + """Set up Switchbot curtain based on a config entry.""" |
| 30 | + coordinator: SwitchbotDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][ |
| 31 | + DATA_COORDINATOR |
| 32 | + ] |
| 33 | + |
| 34 | + if not coordinator.data[entry.unique_id].get("data"): |
| 35 | + return |
| 36 | + |
| 37 | + async_add_entities( |
| 38 | + [ |
| 39 | + SwitchBotBinarySensor( |
| 40 | + coordinator, |
| 41 | + entry.unique_id, |
| 42 | + binary_sensor, |
| 43 | + entry.data[CONF_MAC], |
| 44 | + entry.data[CONF_NAME], |
| 45 | + ) |
| 46 | + for binary_sensor in coordinator.data[entry.unique_id]["data"] |
| 47 | + if binary_sensor in BINARY_SENSOR_TYPES |
| 48 | + ] |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +class SwitchBotBinarySensor(SwitchbotEntity, BinarySensorEntity): |
| 53 | + """Representation of a Switchbot binary sensor.""" |
| 54 | + |
| 55 | + coordinator: SwitchbotDataUpdateCoordinator |
| 56 | + |
| 57 | + def __init__( |
| 58 | + self, |
| 59 | + coordinator: SwitchbotDataUpdateCoordinator, |
| 60 | + idx: str | None, |
| 61 | + binary_sensor: str, |
| 62 | + mac: str, |
| 63 | + switchbot_name: str, |
| 64 | + ) -> None: |
| 65 | + """Initialize the Switchbot sensor.""" |
| 66 | + super().__init__(coordinator, idx, mac, name=switchbot_name) |
| 67 | + self._sensor = binary_sensor |
| 68 | + self._attr_unique_id = f"{idx}-{binary_sensor}" |
| 69 | + self._attr_name = f"{switchbot_name} {binary_sensor.title()}" |
| 70 | + self.entity_description = BINARY_SENSOR_TYPES[binary_sensor] |
| 71 | + |
| 72 | + @property |
| 73 | + def is_on(self) -> bool: |
| 74 | + """Return the state of the sensor.""" |
| 75 | + return self.data["data"][self._sensor] |
0 commit comments