22
33from __future__ import annotations
44
5+ from collections .abc import Callable
6+ from dataclasses import dataclass
7+
8+ from switchbot import SwitchbotModel
9+
510from homeassistant .components .binary_sensor import (
611 BinarySensorDeviceClass ,
712 BinarySensorEntity ,
1621
1722PARALLEL_UPDATES = 0
1823
19- BINARY_SENSOR_TYPES : dict [str , BinarySensorEntityDescription ] = {
20- "calibration" : BinarySensorEntityDescription (
24+
25+ @dataclass (frozen = True , kw_only = True )
26+ class SwitchbotBinarySensorEntityDescription (BinarySensorEntityDescription ):
27+ """Describes Switchbot binary sensor entity."""
28+
29+ device_class_fn : Callable [[SwitchbotModel ], BinarySensorDeviceClass ] | None = None
30+
31+
32+ BINARY_SENSOR_TYPES : dict [str , SwitchbotBinarySensorEntityDescription ] = {
33+ "calibration" : SwitchbotBinarySensorEntityDescription (
2134 key = "calibration" ,
2235 translation_key = "calibration" ,
2336 entity_category = EntityCategory .DIAGNOSTIC ,
2437 ),
25- "motion_detected" : BinarySensorEntityDescription (
38+ "motion_detected" : SwitchbotBinarySensorEntityDescription (
2639 key = "pir_state" ,
27- device_class = BinarySensorDeviceClass .MOTION ,
40+ device_class_fn = lambda model : {
41+ SwitchbotModel .PRESENCE_SENSOR : BinarySensorDeviceClass .OCCUPANCY ,
42+ }.get (model , BinarySensorDeviceClass .MOTION ),
2843 ),
29- "contact_open" : BinarySensorEntityDescription (
44+ "contact_open" : SwitchbotBinarySensorEntityDescription (
3045 key = "contact_open" ,
3146 name = None ,
3247 device_class = BinarySensorDeviceClass .DOOR ,
3348 ),
34- "contact_timeout" : BinarySensorEntityDescription (
49+ "contact_timeout" : SwitchbotBinarySensorEntityDescription (
3550 key = "contact_timeout" ,
3651 translation_key = "door_timeout" ,
3752 device_class = BinarySensorDeviceClass .PROBLEM ,
3853 entity_category = EntityCategory .DIAGNOSTIC ,
3954 ),
40- "is_light" : BinarySensorEntityDescription (
55+ "is_light" : SwitchbotBinarySensorEntityDescription (
4156 key = "is_light" ,
4257 device_class = BinarySensorDeviceClass .LIGHT ,
4358 ),
44- "door_open" : BinarySensorEntityDescription (
59+ "door_open" : SwitchbotBinarySensorEntityDescription (
4560 key = "door_status" ,
4661 name = None ,
4762 device_class = BinarySensorDeviceClass .DOOR ,
4863 ),
49- "unclosed_alarm" : BinarySensorEntityDescription (
64+ "unclosed_alarm" : SwitchbotBinarySensorEntityDescription (
5065 key = "unclosed_alarm" ,
5166 translation_key = "door_unclosed_alarm" ,
5267 entity_category = EntityCategory .DIAGNOSTIC ,
5368 device_class = BinarySensorDeviceClass .PROBLEM ,
5469 ),
55- "unlocked_alarm" : BinarySensorEntityDescription (
70+ "unlocked_alarm" : SwitchbotBinarySensorEntityDescription (
5671 key = "unlocked_alarm" ,
5772 translation_key = "door_unlocked_alarm" ,
5873 entity_category = EntityCategory .DIAGNOSTIC ,
5974 device_class = BinarySensorDeviceClass .PROBLEM ,
6075 ),
61- "auto_lock_paused" : BinarySensorEntityDescription (
76+ "auto_lock_paused" : SwitchbotBinarySensorEntityDescription (
6277 key = "auto_lock_paused" ,
6378 translation_key = "door_auto_lock_paused" ,
6479 entity_category = EntityCategory .DIAGNOSTIC ,
6580 ),
66- "leak" : BinarySensorEntityDescription (
81+ "leak" : SwitchbotBinarySensorEntityDescription (
6782 key = "leak" ,
6883 name = None ,
6984 device_class = BinarySensorDeviceClass .MOISTURE ,
@@ -88,6 +103,8 @@ async def async_setup_entry(
88103class SwitchBotBinarySensor (SwitchbotEntity , BinarySensorEntity ):
89104 """Representation of a Switchbot binary sensor."""
90105
106+ entity_description : SwitchbotBinarySensorEntityDescription
107+
91108 def __init__ (
92109 self ,
93110 coordinator : SwitchbotDataUpdateCoordinator ,
@@ -98,6 +115,10 @@ def __init__(
98115 self ._sensor = binary_sensor
99116 self ._attr_unique_id = f"{ coordinator .base_unique_id } -{ binary_sensor } "
100117 self .entity_description = BINARY_SENSOR_TYPES [binary_sensor ]
118+ if self .entity_description .device_class_fn :
119+ self ._attr_device_class = self .entity_description .device_class_fn (
120+ coordinator .model
121+ )
101122
102123 @property
103124 def is_on (self ) -> bool :
0 commit comments