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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

127 lines
4.2 KiB
Python
Raw Normal View History

2021-09-24 00:10:34 +02:00
"""Support for SwitchBot binary sensors."""
2021-09-24 00:10:34 +02:00
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from switchbot import SwitchbotModel
2021-09-24 00:10:34 +02:00
from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
2021-09-24 00:10:34 +02:00
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.const import EntityCategory
2021-09-24 00:10:34 +02:00
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
2021-09-24 00:10:34 +02:00
from .coordinator import SwitchbotConfigEntry, SwitchbotDataUpdateCoordinator
2021-09-24 00:10:34 +02:00
from .entity import SwitchbotEntity
PARALLEL_UPDATES = 0
2021-09-24 00:10:34 +02:00
@dataclass(frozen=True, kw_only=True)
class SwitchbotBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Describes Switchbot binary sensor entity."""
device_class_fn: Callable[[SwitchbotModel], BinarySensorDeviceClass] | None = None
BINARY_SENSOR_TYPES: dict[str, SwitchbotBinarySensorEntityDescription] = {
"calibration": SwitchbotBinarySensorEntityDescription(
2021-09-24 00:10:34 +02:00
key="calibration",
translation_key="calibration",
2021-12-20 18:03:08 +01:00
entity_category=EntityCategory.DIAGNOSTIC,
2021-09-24 00:10:34 +02:00
),
"motion_detected": SwitchbotBinarySensorEntityDescription(
key="pir_state",
device_class_fn=lambda model: {
SwitchbotModel.PRESENCE_SENSOR: BinarySensorDeviceClass.OCCUPANCY,
}.get(model, BinarySensorDeviceClass.MOTION),
),
"contact_open": SwitchbotBinarySensorEntityDescription(
key="contact_open",
name=None,
device_class=BinarySensorDeviceClass.DOOR,
),
"contact_timeout": SwitchbotBinarySensorEntityDescription(
key="contact_timeout",
translation_key="door_timeout",
device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=EntityCategory.DIAGNOSTIC,
),
"is_light": SwitchbotBinarySensorEntityDescription(
key="is_light",
device_class=BinarySensorDeviceClass.LIGHT,
),
"door_open": SwitchbotBinarySensorEntityDescription(
2022-12-28 13:16:00 +09:00
key="door_status",
name=None,
2022-12-28 13:16:00 +09:00
device_class=BinarySensorDeviceClass.DOOR,
),
"unclosed_alarm": SwitchbotBinarySensorEntityDescription(
2022-12-28 13:16:00 +09:00
key="unclosed_alarm",
translation_key="door_unclosed_alarm",
2022-12-28 13:16:00 +09:00
entity_category=EntityCategory.DIAGNOSTIC,
device_class=BinarySensorDeviceClass.PROBLEM,
),
"unlocked_alarm": SwitchbotBinarySensorEntityDescription(
2022-12-28 13:16:00 +09:00
key="unlocked_alarm",
translation_key="door_unlocked_alarm",
2022-12-28 13:16:00 +09:00
entity_category=EntityCategory.DIAGNOSTIC,
device_class=BinarySensorDeviceClass.PROBLEM,
),
"auto_lock_paused": SwitchbotBinarySensorEntityDescription(
2022-12-28 13:16:00 +09:00
key="auto_lock_paused",
translation_key="door_auto_lock_paused",
2022-12-28 13:16:00 +09:00
entity_category=EntityCategory.DIAGNOSTIC,
),
"leak": SwitchbotBinarySensorEntityDescription(
key="leak",
name=None,
device_class=BinarySensorDeviceClass.MOISTURE,
),
2021-09-24 00:10:34 +02:00
}
async def async_setup_entry(
hass: HomeAssistant,
entry: SwitchbotConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
2021-09-24 00:10:34 +02:00
) -> None:
"""Set up Switchbot curtain based on a config entry."""
coordinator = entry.runtime_data
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."""
entity_description: SwitchbotBinarySensorEntityDescription
2021-09-24 00:10:34 +02:00
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]
if self.entity_description.device_class_fn:
self._attr_device_class = self.entity_description.device_class_fn(
coordinator.model
)
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]