1 """Interfaces with TotalConnect sensors."""
3 from collections.abc
import Callable
4 from dataclasses
import dataclass
7 from total_connect_client.location
import TotalConnectLocation
8 from total_connect_client.zone
import TotalConnectZone
11 BinarySensorDeviceClass,
13 BinarySensorEntityDescription,
20 from .const
import DOMAIN
21 from .coordinator
import TotalConnectDataUpdateCoordinator
22 from .entity
import TotalConnectLocationEntity, TotalConnectZoneEntity
24 LOW_BATTERY =
"low_battery"
29 _LOGGER = logging.getLogger(__name__)
32 @dataclass(frozen=True, kw_only=True)
34 """Describes TotalConnect binary sensor entity."""
36 device_class_fn: Callable[[TotalConnectZone], BinarySensorDeviceClass] |
None =
None
37 is_on_fn: Callable[[TotalConnectZone], bool]
41 """Return the device class of a TotalConnect security zone."""
42 if zone.is_type_fire():
43 return BinarySensorDeviceClass.SMOKE
44 if zone.is_type_carbon_monoxide():
45 return BinarySensorDeviceClass.GAS
46 if zone.is_type_motion():
47 return BinarySensorDeviceClass.MOTION
48 if zone.is_type_medical():
49 return BinarySensorDeviceClass.SAFETY
50 if zone.is_type_temperature():
51 return BinarySensorDeviceClass.PROBLEM
52 return BinarySensorDeviceClass.DOOR
58 device_class_fn=get_security_zone_device_class,
59 is_on_fn=
lambda zone: zone.is_faulted()
or zone.is_triggered(),
62 NO_BUTTON_BINARY_SENSORS: tuple[TotalConnectZoneBinarySensorEntityDescription, ...] = (
65 device_class=BinarySensorDeviceClass.BATTERY,
66 entity_category=EntityCategory.DIAGNOSTIC,
67 is_on_fn=
lambda zone: zone.is_low_battery(),
71 device_class=BinarySensorDeviceClass.TAMPER,
72 entity_category=EntityCategory.DIAGNOSTIC,
73 is_on_fn=
lambda zone: zone.is_tampered(),
78 @dataclass(frozen=True, kw_only=True)
80 """Describes TotalConnect binary sensor entity."""
82 is_on_fn: Callable[[TotalConnectLocation], bool]
85 LOCATION_BINARY_SENSORS: tuple[TotalConnectAlarmBinarySensorEntityDescription, ...] = (
88 device_class=BinarySensorDeviceClass.BATTERY,
89 entity_category=EntityCategory.DIAGNOSTIC,
90 is_on_fn=
lambda location: location.is_low_battery(),
94 device_class=BinarySensorDeviceClass.TAMPER,
95 entity_category=EntityCategory.DIAGNOSTIC,
96 is_on_fn=
lambda location: location.is_cover_tampered(),
100 device_class=BinarySensorDeviceClass.POWER,
101 entity_category=EntityCategory.DIAGNOSTIC,
102 is_on_fn=
lambda location: location.is_ac_loss(),
106 device_class=BinarySensorDeviceClass.SMOKE,
107 is_on_fn=
lambda location: location.arming_state.is_triggered_fire(),
110 key=
"carbon_monoxide",
111 device_class=BinarySensorDeviceClass.CO,
112 is_on_fn=
lambda location: location.arming_state.is_triggered_gas(),
116 translation_key=
"police",
117 is_on_fn=
lambda location: location.arming_state.is_triggered_police(),
123 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
125 """Set up TotalConnect device sensors based on a config entry."""
128 coordinator: TotalConnectDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
130 client_locations = coordinator.client.locations
132 for location_id, location
in client_locations.items():
135 for description
in LOCATION_BINARY_SENSORS
138 for zone
in location.zones.values():
141 coordinator, SECURITY_BINARY_SENSOR, zone, location_id
145 if not zone.is_type_button():
153 for description
in NO_BUTTON_BINARY_SENSORS
160 """Represent a TotalConnect zone."""
162 entity_description: TotalConnectZoneBinarySensorEntityDescription
166 coordinator: TotalConnectDataUpdateCoordinator,
167 entity_description: TotalConnectZoneBinarySensorEntityDescription,
168 zone: TotalConnectZone,
171 """Initialize the TotalConnect status."""
172 super().
__init__(coordinator, zone, location_id, entity_description.key)
175 "zone_id": zone.zoneid,
176 "location_id": location_id,
177 "partition": zone.partition,
182 """Return the state of the entity."""
187 """Return the class of this zone."""
190 return super().device_class
194 """Represent a TotalConnect alarm device binary sensors."""
196 entity_description: TotalConnectAlarmBinarySensorEntityDescription
200 coordinator: TotalConnectDataUpdateCoordinator,
201 entity_description: TotalConnectAlarmBinarySensorEntityDescription,
202 location: TotalConnectLocation,
204 """Initialize the TotalConnect alarm device binary sensor."""
205 super().
__init__(coordinator, location)
207 self.
_attr_unique_id_attr_unique_id = f
"{location.location_id}_{entity_description.key}"
209 "location_id": location.location_id,
214 """Return the state of the entity."""
None __init__(self, TotalConnectDataUpdateCoordinator coordinator, TotalConnectAlarmBinarySensorEntityDescription entity_description, TotalConnectLocation location)
_attr_extra_state_attributes
BinarySensorDeviceClass|None device_class(self)
None __init__(self, TotalConnectDataUpdateCoordinator coordinator, TotalConnectZoneBinarySensorEntityDescription entity_description, TotalConnectZone zone, str location_id)
_attr_extra_state_attributes
BinarySensorDeviceClass get_security_zone_device_class(TotalConnectZone zone)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)