Home Assistant Unofficial Reference 2024.12.1
binary_sensor.py
Go to the documentation of this file.
1 """Support for Traccar server binary sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any, Literal
8 
9 from pytraccar import DeviceModel
10 
12  BinarySensorDeviceClass,
13  BinarySensorEntity,
14  BinarySensorEntityDescription,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.const import EntityCategory
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from .const import DOMAIN
22 from .coordinator import TraccarServerCoordinator
23 from .entity import TraccarServerEntity
24 
25 
26 @dataclass(frozen=True, kw_only=True)
27 class TraccarServerBinarySensorEntityDescription[_T](BinarySensorEntityDescription):
28  """Describe Traccar Server sensor entity."""
29 
30  data_key: Literal["position", "device", "geofence", "attributes"]
31  entity_registry_enabled_default = False
32  entity_category = EntityCategory.DIAGNOSTIC
33  value_fn: Callable[[_T], bool | None]
34 
35 
36 TRACCAR_SERVER_BINARY_SENSOR_ENTITY_DESCRIPTIONS: tuple[
37  TraccarServerBinarySensorEntityDescription[Any], ...
38 ] = (
39  TraccarServerBinarySensorEntityDescription[DeviceModel](
40  key="attributes.motion",
41  data_key="position",
42  translation_key="motion",
43  device_class=BinarySensorDeviceClass.MOTION,
44  value_fn=lambda x: x["attributes"].get("motion", False),
45  ),
46  TraccarServerBinarySensorEntityDescription[DeviceModel](
47  key="status",
48  data_key="device",
49  translation_key="status",
50  value_fn=lambda x: None if (s := x["status"]) == "unknown" else s == "online",
51  ),
52 )
53 
54 
56  hass: HomeAssistant,
57  entry: ConfigEntry,
58  async_add_entities: AddEntitiesCallback,
59 ) -> None:
60  """Set up binary sensor entities."""
61  coordinator: TraccarServerCoordinator = hass.data[DOMAIN][entry.entry_id]
64  coordinator=coordinator,
65  device=entry["device"],
66  description=description,
67  )
68  for entry in coordinator.data.values()
69  for description in TRACCAR_SERVER_BINARY_SENSOR_ENTITY_DESCRIPTIONS
70  )
71 
72 
73 class TraccarServerBinarySensor[_T](TraccarServerEntity, BinarySensorEntity):
74  """Represent a traccar server binary sensor."""
75 
76  _attr_has_entity_name = True
77  entity_description: TraccarServerBinarySensorEntityDescription[_T]
78 
79  def __init__(
80  self,
81  coordinator: TraccarServerCoordinator,
82  device: DeviceModel,
83  description: TraccarServerBinarySensorEntityDescription[_T],
84  ) -> None:
85  """Initialize the Traccar Server sensor."""
86  super().__init__(coordinator, device)
87  self.entity_descriptionentity_description = description
88  self._attr_unique_id_attr_unique_id_attr_unique_id = (
89  f"{device['uniqueId']}_{description.data_key}_{description.key}"
90  )
91 
92  @property
93  def is_on(self) -> bool | None:
94  """Return if the binary sensor is on or not."""
95  return self.entity_descriptionentity_description.value_fn(
96  getattr(self, f"traccar_{self.entity_description.data_key}")
97  )
None __init__(self, TraccarServerCoordinator coordinator, DeviceModel device, TraccarServerBinarySensorEntityDescription[_T] description)
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)