Files
core/homeassistant/components/switchbot/binary_sensor.py
T

103 lines
3.4 KiB
Python
Raw Normal View History

2021-09-24 00:10:34 +02:00
"""Support for SwitchBot binary sensors."""
from __future__ import annotations
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
2021-09-24 00:10:34 +02:00
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
2021-12-20 18:03:08 +01:00
from homeassistant.helpers.entity import EntityCategory
2021-09-24 00:10:34 +02:00
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2022-07-24 11:38:45 -05:00
from .const import DOMAIN
2021-09-24 00:10:34 +02:00
from .coordinator import SwitchbotDataUpdateCoordinator
from .entity import SwitchbotEntity
PARALLEL_UPDATES = 0
2021-09-24 00:10:34 +02:00
BINARY_SENSOR_TYPES: dict[str, BinarySensorEntityDescription] = {
"calibration": BinarySensorEntityDescription(
key="calibration",
name="Calibration",
2021-12-20 18:03:08 +01:00
entity_category=EntityCategory.DIAGNOSTIC,
2021-09-24 00:10:34 +02:00
),
"motion_detected": BinarySensorEntityDescription(
key="pir_state",
name="Motion detected",
device_class=BinarySensorDeviceClass.MOTION,
),
"contact_open": BinarySensorEntityDescription(
key="contact_open",
name="Door open",
device_class=BinarySensorDeviceClass.DOOR,
),
"contact_timeout": BinarySensorEntityDescription(
key="contact_timeout",
name="Door timeout",
device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=EntityCategory.DIAGNOSTIC,
),
"is_light": BinarySensorEntityDescription(
key="is_light",
name="Light",
device_class=BinarySensorDeviceClass.LIGHT,
),
2022-12-28 13:16:00 +09:00
"door_open": BinarySensorEntityDescription(
key="door_status",
name="Door status",
device_class=BinarySensorDeviceClass.DOOR,
),
"unclosed_alarm": BinarySensorEntityDescription(
key="unclosed_alarm",
name="Door unclosed alarm",
entity_category=EntityCategory.DIAGNOSTIC,
device_class=BinarySensorDeviceClass.PROBLEM,
),
"unlocked_alarm": BinarySensorEntityDescription(
key="unlocked_alarm",
name="Door unlocked alarm",
entity_category=EntityCategory.DIAGNOSTIC,
device_class=BinarySensorDeviceClass.PROBLEM,
),
"auto_lock_paused": BinarySensorEntityDescription(
key="auto_lock_paused",
name="Door auto-lock paused",
entity_category=EntityCategory.DIAGNOSTIC,
),
2021-09-24 00:10:34 +02: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 11:38:45 -05:00
coordinator: SwitchbotDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
2021-09-24 00:10:34 +02:00
async_add_entities(
2022-08-10 09:02:08 -10:00
SwitchBotBinarySensor(coordinator, binary_sensor)
for binary_sensor in coordinator.device.parsed_data
2022-08-10 09:02:08 -10:00
if binary_sensor in BINARY_SENSOR_TYPES
2021-09-24 00:10:34 +02:00
)
class SwitchBotBinarySensor(SwitchbotEntity, BinarySensorEntity):
"""Representation of a Switchbot binary sensor."""
def __init__(
self,
coordinator: SwitchbotDataUpdateCoordinator,
binary_sensor: str,
) -> None:
"""Initialize the Switchbot sensor."""
2022-08-10 09:02:08 -10:00
super().__init__(coordinator)
2021-09-24 00:10:34 +02:00
self._sensor = binary_sensor
2022-08-10 09:02:08 -10:00
self._attr_unique_id = f"{coordinator.base_unique_id}-{binary_sensor}"
2021-09-24 00:10:34 +02:00
self.entity_description = BINARY_SENSOR_TYPES[binary_sensor]
self._attr_name = self.entity_description.name
2021-09-24 00:10:34 +02:00
@property
def is_on(self) -> bool:
"""Return the state of the sensor."""
return self.parsed_data[self._sensor]