mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
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 __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@ -15,6 +17,7 @@ from homeassistant.components.sensor import (
|
|||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
SensorDeviceClass,
|
SensorDeviceClass,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
|
SensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
@ -24,6 +27,7 @@ from homeassistant.const import (
|
|||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
CONF_NAME,
|
CONF_NAME,
|
||||||
CONF_PORT,
|
CONF_PORT,
|
||||||
|
EntityCategory,
|
||||||
)
|
)
|
||||||
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
|
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
@ -43,6 +47,28 @@ ATTR_SPEED = "speed"
|
|||||||
|
|
||||||
DEFAULT_NAME = "GPS"
|
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(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
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_HOST],
|
||||||
config_entry.data[CONF_PORT],
|
config_entry.data[CONF_PORT],
|
||||||
config_entry.entry_id,
|
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."""
|
"""Representation of a GPS receiver available via GPSD."""
|
||||||
|
|
||||||
_attr_has_entity_name = True
|
_attr_has_entity_name = True
|
||||||
_attr_name = None
|
|
||||||
_attr_translation_key = "mode"
|
entity_description: GpsdSensorDescription
|
||||||
_attr_device_class = SensorDeviceClass.ENUM
|
|
||||||
_attr_options = ["2d_fix", "3d_fix"]
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
host: str,
|
host: str,
|
||||||
port: int,
|
port: int,
|
||||||
unique_id: str,
|
unique_id: str,
|
||||||
|
description: GpsdSensorDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the GPSD sensor."""
|
"""Initialize the GPSD sensor."""
|
||||||
|
self.entity_description = description
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
identifiers={(DOMAIN, unique_id)},
|
identifiers={(DOMAIN, unique_id)},
|
||||||
entry_type=DeviceEntryType.SERVICE,
|
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 = AGPS3mechanism()
|
||||||
self.agps_thread.stream_data(host=host, port=port)
|
self.agps_thread.stream_data(host=host, port=port)
|
||||||
@ -126,11 +154,7 @@ class GpsdSensor(SensorEntity):
|
|||||||
@property
|
@property
|
||||||
def native_value(self) -> str | None:
|
def native_value(self) -> str | None:
|
||||||
"""Return the state of GPSD."""
|
"""Return the state of GPSD."""
|
||||||
if self.agps_thread.data_stream.mode == 3:
|
return self.entity_description.value_fn(self.agps_thread)
|
||||||
return "3d_fix"
|
|
||||||
if self.agps_thread.data_stream.mode == 2:
|
|
||||||
return "2d_fix"
|
|
||||||
return None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self) -> dict[str, Any]:
|
def extra_state_attributes(self) -> dict[str, Any]:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user