Add binary_sensor to switchbot (#56415)
Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
parent
0b53f73fe2
commit
915afedcfc
3 changed files with 77 additions and 1 deletions
|
@ -1014,6 +1014,7 @@ omit =
|
|||
homeassistant/components/swiss_public_transport/sensor.py
|
||||
homeassistant/components/swisscom/device_tracker.py
|
||||
homeassistant/components/switchbot/switch.py
|
||||
homeassistant/components/switchbot/binary_sensor.py
|
||||
homeassistant/components/switchbot/__init__.py
|
||||
homeassistant/components/switchbot/const.py
|
||||
homeassistant/components/switchbot/entity.py
|
||||
|
|
|
@ -28,7 +28,7 @@ from .coordinator import SwitchbotDataUpdateCoordinator
|
|||
|
||||
PLATFORMS_BY_TYPE = {
|
||||
ATTR_BOT: ["switch", "sensor"],
|
||||
ATTR_CURTAIN: ["cover", "sensor"],
|
||||
ATTR_CURTAIN: ["cover", "binary_sensor", "sensor"],
|
||||
}
|
||||
|
||||
|
||||
|
|
75
homeassistant/components/switchbot/binary_sensor.py
Normal file
75
homeassistant/components/switchbot/binary_sensor.py
Normal file
|
@ -0,0 +1,75 @@
|
|||
"""Support for SwitchBot binary sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_MAC, CONF_NAME
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DATA_COORDINATOR, DOMAIN
|
||||
from .coordinator import SwitchbotDataUpdateCoordinator
|
||||
from .entity import SwitchbotEntity
|
||||
|
||||
PARALLEL_UPDATES = 1
|
||||
|
||||
BINARY_SENSOR_TYPES: dict[str, BinarySensorEntityDescription] = {
|
||||
"calibration": BinarySensorEntityDescription(
|
||||
key="calibration",
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
) -> None:
|
||||
"""Set up Switchbot curtain based on a config entry."""
|
||||
coordinator: SwitchbotDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
|
||||
DATA_COORDINATOR
|
||||
]
|
||||
|
||||
if not coordinator.data[entry.unique_id].get("data"):
|
||||
return
|
||||
|
||||
async_add_entities(
|
||||
[
|
||||
SwitchBotBinarySensor(
|
||||
coordinator,
|
||||
entry.unique_id,
|
||||
binary_sensor,
|
||||
entry.data[CONF_MAC],
|
||||
entry.data[CONF_NAME],
|
||||
)
|
||||
for binary_sensor in coordinator.data[entry.unique_id]["data"]
|
||||
if binary_sensor in BINARY_SENSOR_TYPES
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
class SwitchBotBinarySensor(SwitchbotEntity, BinarySensorEntity):
|
||||
"""Representation of a Switchbot binary sensor."""
|
||||
|
||||
coordinator: SwitchbotDataUpdateCoordinator
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: SwitchbotDataUpdateCoordinator,
|
||||
idx: str | None,
|
||||
binary_sensor: str,
|
||||
mac: str,
|
||||
switchbot_name: str,
|
||||
) -> None:
|
||||
"""Initialize the Switchbot sensor."""
|
||||
super().__init__(coordinator, idx, mac, name=switchbot_name)
|
||||
self._sensor = binary_sensor
|
||||
self._attr_unique_id = f"{idx}-{binary_sensor}"
|
||||
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]
|
Loading…
Add table
Reference in a new issue