1 """Sensor platform for GPSD integration."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from datetime
import datetime
11 from gps3.agps3threaded
import AGPS3mechanism
16 SensorEntityDescription,
33 from .
import GPSDConfigEntry
34 from .const
import DOMAIN
36 _LOGGER = logging.getLogger(__name__)
39 ATTR_ELEVATION =
"elevation"
40 ATTR_GPS_TIME =
"gps_time"
45 _MODE_VALUES = {2:
"2d_fix", 3:
"3d_fix"}
48 @dataclass(frozen=True, kw_only=True)
50 """Class describing GPSD sensor entities."""
52 value_fn: Callable[[AGPS3mechanism], StateType | datetime]
55 SENSOR_TYPES: tuple[GpsdSensorDescription, ...] = (
58 translation_key=ATTR_MODE,
60 entity_category=EntityCategory.DIAGNOSTIC,
61 device_class=SensorDeviceClass.ENUM,
62 options=
list(_MODE_VALUES.values()),
63 value_fn=
lambda agps_thread: _MODE_VALUES.get(agps_thread.data_stream.mode),
67 translation_key=ATTR_LATITUDE,
68 entity_category=EntityCategory.DIAGNOSTIC,
69 value_fn=
lambda agps_thread: agps_thread.data_stream.lat,
70 entity_registry_enabled_default=
False,
74 translation_key=ATTR_LONGITUDE,
75 entity_category=EntityCategory.DIAGNOSTIC,
76 value_fn=
lambda agps_thread: agps_thread.data_stream.lon,
77 entity_registry_enabled_default=
False,
81 translation_key=ATTR_ELEVATION,
82 entity_category=EntityCategory.DIAGNOSTIC,
83 device_class=SensorDeviceClass.DISTANCE,
84 native_unit_of_measurement=UnitOfLength.METERS,
85 value_fn=
lambda agps_thread: agps_thread.data_stream.alt,
86 suggested_display_precision=2,
87 entity_registry_enabled_default=
False,
91 translation_key=ATTR_TIME,
92 entity_category=EntityCategory.DIAGNOSTIC,
93 device_class=SensorDeviceClass.TIMESTAMP,
94 value_fn=
lambda agps_thread: dt_util.parse_datetime(
95 agps_thread.data_stream.time
97 entity_registry_enabled_default=
False,
101 translation_key=ATTR_SPEED,
102 entity_category=EntityCategory.DIAGNOSTIC,
103 device_class=SensorDeviceClass.SPEED,
104 native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND,
105 value_fn=
lambda agps_thread: agps_thread.data_stream.speed,
106 suggested_display_precision=2,
107 entity_registry_enabled_default=
False,
111 translation_key=ATTR_CLIMB,
112 entity_category=EntityCategory.DIAGNOSTIC,
113 device_class=SensorDeviceClass.SPEED,
114 native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND,
115 value_fn=
lambda agps_thread: agps_thread.data_stream.climb,
116 suggested_display_precision=2,
117 entity_registry_enabled_default=
False,
124 config_entry: GPSDConfigEntry,
125 async_add_entities: AddEntitiesCallback,
127 """Set up the GPSD component."""
131 config_entry.runtime_data,
132 config_entry.entry_id,
135 for description
in SENSOR_TYPES
141 """Representation of a GPS receiver available via GPSD."""
143 _attr_has_entity_name =
True
145 entity_description: GpsdSensorDescription
149 agps_thread: AGPS3mechanism,
151 description: GpsdSensorDescription,
153 """Initialize the GPSD sensor."""
156 identifiers={(DOMAIN, unique_id)},
157 entry_type=DeviceEntryType.SERVICE,
165 """Return the state of GPSD."""
167 return None if value ==
"n/a" else value
173 """Return the state attributes of the GPS."""
178 ATTR_LATITUDE: self.
agps_threadagps_thread.data_stream.lat,
179 ATTR_LONGITUDE: self.
agps_threadagps_thread.data_stream.lon,
180 ATTR_ELEVATION: self.
agps_threadagps_thread.data_stream.alt,
181 ATTR_GPS_TIME: self.
agps_threadagps_thread.data_stream.time,
182 ATTR_SPEED: self.
agps_threadagps_thread.data_stream.speed,
183 ATTR_CLIMB: self.
agps_threadagps_thread.data_stream.climb,
184 ATTR_MODE: self.
agps_threadagps_thread.data_stream.mode,
dict[str, Any]|None extra_state_attributes(self)
StateType|datetime native_value(self)
None __init__(self, AGPS3mechanism agps_thread, str unique_id, GpsdSensorDescription description)
None async_setup_entry(HomeAssistant hass, GPSDConfigEntry config_entry, AddEntitiesCallback async_add_entities)