mirror of https://github.com/home-assistant/core
Add entity description to GPSD (#109320)
This commit is contained in:
parent
343086a6c8
commit
0c3541c194
|
@ -1,6 +1,8 @@
|
|||
"""Support for GPSD."""
|
||||
"""Sensor platform for GPSD integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
|
@ -15,6 +17,7 @@ from homeassistant.components.sensor import (
|
|||
PLATFORM_SCHEMA,
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||
from homeassistant.const import (
|
||||
|
@ -24,6 +27,7 @@ from homeassistant.const import (
|
|||
CONF_HOST,
|
||||
CONF_NAME,
|
||||
CONF_PORT,
|
||||
EntityCategory,
|
||||
)
|
||||
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
@ -43,6 +47,28 @@ ATTR_SPEED = "speed"
|
|||
|
||||
DEFAULT_NAME = "GPS"
|
||||
|
||||
_MODE_VALUES = {2: "2d_fix", 3: "3d_fix"}
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class GpsdSensorDescription(SensorEntityDescription):
|
||||
"""Class describing GPSD sensor entities."""
|
||||
|
||||
value_fn: Callable[[AGPS3mechanism], str | None]
|
||||
|
||||
|
||||
SENSOR_TYPES: tuple[GpsdSensorDescription, ...] = (
|
||||
GpsdSensorDescription(
|
||||
key="mode",
|
||||
translation_key="mode",
|
||||
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),
|
||||
),
|
||||
)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
|
@ -64,7 +90,9 @@ async def async_setup_entry(
|
|||
config_entry.data[CONF_HOST],
|
||||
config_entry.data[CONF_PORT],
|
||||
config_entry.entry_id,
|
||||
description,
|
||||
)
|
||||
for description in SENSOR_TYPES
|
||||
]
|
||||
)
|
||||
|
||||
|
@ -101,23 +129,23 @@ class GpsdSensor(SensorEntity):
|
|||
"""Representation of a GPS receiver available via GPSD."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
_attr_name = None
|
||||
_attr_translation_key = "mode"
|
||||
_attr_device_class = SensorDeviceClass.ENUM
|
||||
_attr_options = ["2d_fix", "3d_fix"]
|
||||
|
||||
entity_description: GpsdSensorDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
host: str,
|
||||
port: int,
|
||||
unique_id: str,
|
||||
description: GpsdSensorDescription,
|
||||
) -> None:
|
||||
"""Initialize the GPSD sensor."""
|
||||
self.entity_description = description
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, unique_id)},
|
||||
entry_type=DeviceEntryType.SERVICE,
|
||||
)
|
||||
self._attr_unique_id = f"{unique_id}-mode"
|
||||
self._attr_unique_id = f"{unique_id}-{self.entity_description.key}"
|
||||
|
||||
self.agps_thread = AGPS3mechanism()
|
||||
self.agps_thread.stream_data(host=host, port=port)
|
||||
|
@ -126,11 +154,7 @@ class GpsdSensor(SensorEntity):
|
|||
@property
|
||||
def native_value(self) -> str | None:
|
||||
"""Return the state of GPSD."""
|
||||
if self.agps_thread.data_stream.mode == 3:
|
||||
return "3d_fix"
|
||||
if self.agps_thread.data_stream.mode == 2:
|
||||
return "2d_fix"
|
||||
return None
|
||||
return self.entity_description.value_fn(self.agps_thread)
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> dict[str, Any]:
|
||||
|
|
Loading…
Reference in New Issue