2024-02-02 13:44:50 +00:00
|
|
|
"""Sensor platform for GPSD integration."""
|
2024-03-08 13:52:48 +00:00
|
|
|
|
2022-01-04 10:08:28 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-02-02 13:44:50 +00:00
|
|
|
from collections.abc import Callable
|
|
|
|
from dataclasses import dataclass
|
2024-08-13 08:37:43 +00:00
|
|
|
from datetime import datetime
|
2016-08-13 17:37:12 +00:00
|
|
|
import logging
|
2023-09-12 20:21:58 +00:00
|
|
|
from typing import Any
|
2016-08-13 17:37:12 +00:00
|
|
|
|
2024-08-12 19:31:42 +00:00
|
|
|
from gps3.agps3threaded import AGPS3mechanism
|
2016-08-13 17:37:12 +00:00
|
|
|
|
2024-01-31 19:42:14 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
2024-02-02 13:44:50 +00:00
|
|
|
SensorEntityDescription,
|
2024-01-31 19:42:14 +00:00
|
|
|
)
|
2024-08-13 08:37:43 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_LATITUDE,
|
|
|
|
ATTR_LONGITUDE,
|
|
|
|
ATTR_MODE,
|
|
|
|
ATTR_TIME,
|
|
|
|
EntityCategory,
|
|
|
|
UnitOfLength,
|
|
|
|
UnitOfSpeed,
|
|
|
|
)
|
2024-08-12 19:31:42 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2024-01-31 17:38:14 +00:00
|
|
|
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
2022-01-04 10:08:28 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2024-08-13 08:37:43 +00:00
|
|
|
from homeassistant.helpers.typing import StateType
|
|
|
|
from homeassistant.util import dt as dt_util
|
2016-08-13 17:37:12 +00:00
|
|
|
|
2024-07-19 16:25:07 +00:00
|
|
|
from . import GPSDConfigEntry
|
2024-01-31 17:38:14 +00:00
|
|
|
from .const import DOMAIN
|
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-08-13 17:37:12 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_CLIMB = "climb"
|
|
|
|
ATTR_ELEVATION = "elevation"
|
|
|
|
ATTR_GPS_TIME = "gps_time"
|
|
|
|
ATTR_SPEED = "speed"
|
2016-08-13 17:37:12 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "GPS"
|
2016-08-13 17:37:12 +00:00
|
|
|
|
2024-02-02 13:44:50 +00:00
|
|
|
_MODE_VALUES = {2: "2d_fix", 3: "3d_fix"}
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
|
|
class GpsdSensorDescription(SensorEntityDescription):
|
|
|
|
"""Class describing GPSD sensor entities."""
|
|
|
|
|
2024-08-13 08:37:43 +00:00
|
|
|
value_fn: Callable[[AGPS3mechanism], StateType | datetime]
|
2024-02-02 13:44:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
SENSOR_TYPES: tuple[GpsdSensorDescription, ...] = (
|
|
|
|
GpsdSensorDescription(
|
2024-08-13 08:37:43 +00:00
|
|
|
key=ATTR_MODE,
|
|
|
|
translation_key=ATTR_MODE,
|
2024-02-02 13:44:50 +00:00
|
|
|
name=None,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
device_class=SensorDeviceClass.ENUM,
|
|
|
|
options=list(_MODE_VALUES.values()),
|
|
|
|
value_fn=lambda agps_thread: _MODE_VALUES.get(agps_thread.data_stream.mode),
|
|
|
|
),
|
2024-08-13 08:37:43 +00:00
|
|
|
GpsdSensorDescription(
|
|
|
|
key=ATTR_LATITUDE,
|
|
|
|
translation_key=ATTR_LATITUDE,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
value_fn=lambda agps_thread: agps_thread.data_stream.lat,
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
),
|
|
|
|
GpsdSensorDescription(
|
|
|
|
key=ATTR_LONGITUDE,
|
|
|
|
translation_key=ATTR_LONGITUDE,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
value_fn=lambda agps_thread: agps_thread.data_stream.lon,
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
),
|
|
|
|
GpsdSensorDescription(
|
|
|
|
key=ATTR_ELEVATION,
|
|
|
|
translation_key=ATTR_ELEVATION,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
device_class=SensorDeviceClass.DISTANCE,
|
|
|
|
native_unit_of_measurement=UnitOfLength.METERS,
|
|
|
|
value_fn=lambda agps_thread: agps_thread.data_stream.alt,
|
|
|
|
suggested_display_precision=2,
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
),
|
|
|
|
GpsdSensorDescription(
|
|
|
|
key=ATTR_TIME,
|
|
|
|
translation_key=ATTR_TIME,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
device_class=SensorDeviceClass.TIMESTAMP,
|
|
|
|
value_fn=lambda agps_thread: dt_util.parse_datetime(
|
|
|
|
agps_thread.data_stream.time
|
|
|
|
),
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
),
|
|
|
|
GpsdSensorDescription(
|
|
|
|
key=ATTR_SPEED,
|
|
|
|
translation_key=ATTR_SPEED,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
device_class=SensorDeviceClass.SPEED,
|
|
|
|
native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND,
|
|
|
|
value_fn=lambda agps_thread: agps_thread.data_stream.speed,
|
|
|
|
suggested_display_precision=2,
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
),
|
|
|
|
GpsdSensorDescription(
|
|
|
|
key=ATTR_CLIMB,
|
|
|
|
translation_key=ATTR_CLIMB,
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
device_class=SensorDeviceClass.SPEED,
|
|
|
|
native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND,
|
|
|
|
value_fn=lambda agps_thread: agps_thread.data_stream.climb,
|
|
|
|
suggested_display_precision=2,
|
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
),
|
2024-02-02 13:44:50 +00:00
|
|
|
)
|
|
|
|
|
2016-08-13 17:37:12 +00:00
|
|
|
|
2024-01-31 17:38:14 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
2024-07-19 16:25:07 +00:00
|
|
|
config_entry: GPSDConfigEntry,
|
2024-01-31 17:38:14 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up the GPSD component."""
|
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
GpsdSensor(
|
2024-07-19 16:25:07 +00:00
|
|
|
config_entry.runtime_data,
|
2024-01-31 17:38:14 +00:00
|
|
|
config_entry.entry_id,
|
2024-02-02 13:44:50 +00:00
|
|
|
description,
|
2024-01-31 17:38:14 +00:00
|
|
|
)
|
2024-02-02 13:44:50 +00:00
|
|
|
for description in SENSOR_TYPES
|
2024-01-31 17:38:14 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-03-22 18:45:17 +00:00
|
|
|
class GpsdSensor(SensorEntity):
|
2016-08-13 17:37:12 +00:00
|
|
|
"""Representation of a GPS receiver available via GPSD."""
|
|
|
|
|
2024-01-31 17:38:14 +00:00
|
|
|
_attr_has_entity_name = True
|
2024-02-02 13:44:50 +00:00
|
|
|
|
|
|
|
entity_description: GpsdSensorDescription
|
2024-01-31 17:38:14 +00:00
|
|
|
|
2023-09-12 20:21:58 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2024-07-19 16:25:07 +00:00
|
|
|
agps_thread: AGPS3mechanism,
|
2024-01-31 17:38:14 +00:00
|
|
|
unique_id: str,
|
2024-02-02 13:44:50 +00:00
|
|
|
description: GpsdSensorDescription,
|
2023-09-12 20:21:58 +00:00
|
|
|
) -> None:
|
2016-08-13 17:37:12 +00:00
|
|
|
"""Initialize the GPSD sensor."""
|
2024-02-02 13:44:50 +00:00
|
|
|
self.entity_description = description
|
2024-01-31 17:38:14 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, unique_id)},
|
|
|
|
entry_type=DeviceEntryType.SERVICE,
|
|
|
|
)
|
2024-02-02 13:44:50 +00:00
|
|
|
self._attr_unique_id = f"{unique_id}-{self.entity_description.key}"
|
2016-08-13 17:37:12 +00:00
|
|
|
|
2024-07-19 16:25:07 +00:00
|
|
|
self.agps_thread = agps_thread
|
2016-08-13 17:37:12 +00:00
|
|
|
|
|
|
|
@property
|
2024-08-13 08:37:43 +00:00
|
|
|
def native_value(self) -> StateType | datetime:
|
2016-08-13 17:37:12 +00:00
|
|
|
"""Return the state of GPSD."""
|
2024-08-13 08:37:43 +00:00
|
|
|
value = self.entity_description.value_fn(self.agps_thread)
|
|
|
|
return None if value == "n/a" else value
|
2016-08-13 17:37:12 +00:00
|
|
|
|
2024-08-13 08:37:43 +00:00
|
|
|
# Deprecated since Home Assistant 2024.9.0
|
|
|
|
# Can be removed completely in 2025.3.0
|
2016-08-13 17:37:12 +00:00
|
|
|
@property
|
2024-08-13 08:37:43 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
2016-08-13 17:37:12 +00:00
|
|
|
"""Return the state attributes of the GPS."""
|
2024-08-13 08:37:43 +00:00
|
|
|
if self.entity_description.key != ATTR_MODE:
|
|
|
|
return None
|
|
|
|
|
2016-08-13 17:37:12 +00:00
|
|
|
return {
|
|
|
|
ATTR_LATITUDE: self.agps_thread.data_stream.lat,
|
|
|
|
ATTR_LONGITUDE: self.agps_thread.data_stream.lon,
|
|
|
|
ATTR_ELEVATION: self.agps_thread.data_stream.alt,
|
|
|
|
ATTR_GPS_TIME: self.agps_thread.data_stream.time,
|
|
|
|
ATTR_SPEED: self.agps_thread.data_stream.speed,
|
|
|
|
ATTR_CLIMB: self.agps_thread.data_stream.climb,
|
|
|
|
ATTR_MODE: self.agps_thread.data_stream.mode,
|
|
|
|
}
|