Skip to content

Commit 915afed

Browse files
RenierM26bdraco
andauthored
Add binary_sensor to switchbot (home-assistant#56415)
Co-authored-by: J. Nick Koston <[email protected]>
1 parent 0b53f73 commit 915afed

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

.coveragerc

+1
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,7 @@ omit =
10141014
homeassistant/components/swiss_public_transport/sensor.py
10151015
homeassistant/components/swisscom/device_tracker.py
10161016
homeassistant/components/switchbot/switch.py
1017+
homeassistant/components/switchbot/binary_sensor.py
10171018
homeassistant/components/switchbot/__init__.py
10181019
homeassistant/components/switchbot/const.py
10191020
homeassistant/components/switchbot/entity.py

homeassistant/components/switchbot/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
PLATFORMS_BY_TYPE = {
3030
ATTR_BOT: ["switch", "sensor"],
31-
ATTR_CURTAIN: ["cover", "sensor"],
31+
ATTR_CURTAIN: ["cover", "binary_sensor", "sensor"],
3232
}
3333

3434

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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

Comments
 (0)