1 """Support for Traccar server binary sensors."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from typing
import Any, Literal
9 from pytraccar
import DeviceModel
12 BinarySensorDeviceClass,
14 BinarySensorEntityDescription,
21 from .const
import DOMAIN
22 from .coordinator
import TraccarServerCoordinator
23 from .entity
import TraccarServerEntity
26 @dataclass(frozen=True, kw_only=True)
28 """Describe Traccar Server sensor entity."""
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]
36 TRACCAR_SERVER_BINARY_SENSOR_ENTITY_DESCRIPTIONS: tuple[
37 TraccarServerBinarySensorEntityDescription[Any], ...
39 TraccarServerBinarySensorEntityDescription[DeviceModel](
40 key=
"attributes.motion",
42 translation_key=
"motion",
43 device_class=BinarySensorDeviceClass.MOTION,
44 value_fn=
lambda x: x[
"attributes"].
get(
"motion",
False),
46 TraccarServerBinarySensorEntityDescription[DeviceModel](
49 translation_key=
"status",
50 value_fn=
lambda x:
None if (s := x[
"status"]) ==
"unknown" else s ==
"online",
58 async_add_entities: AddEntitiesCallback,
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,
68 for entry
in coordinator.data.values()
69 for description
in TRACCAR_SERVER_BINARY_SENSOR_ENTITY_DESCRIPTIONS
74 """Represent a traccar server binary sensor."""
76 _attr_has_entity_name =
True
77 entity_description: TraccarServerBinarySensorEntityDescription[_T]
81 coordinator: TraccarServerCoordinator,
83 description: TraccarServerBinarySensorEntityDescription[_T],
85 """Initialize the Traccar Server sensor."""
86 super().
__init__(coordinator, device)
89 f
"{device['uniqueId']}_{description.data_key}_{description.key}"
94 """Return if the binary sensor is on or not."""
96 getattr(self, f
"traccar_{self.entity_description.data_key}")
None __init__(self, TraccarServerCoordinator coordinator, DeviceModel device, TraccarServerBinarySensorEntityDescription[_T] description)
web.Response get(self, web.Request request, str config_key)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)