core/homeassistant/components/gpsd/sensor.py

149 lines
4.2 KiB
Python
Raw Normal View History

"""Support for GPSD."""
from __future__ import annotations
import logging
from typing import Any
2024-01-31 17:38:14 +00:00
from gps3.agps3threaded import (
GPSD_PORT as DEFAULT_PORT,
HOST as DEFAULT_HOST,
AGPS3mechanism,
)
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
2024-01-31 17:38:14 +00:00
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
ATTR_LATITUDE,
ATTR_LONGITUDE,
ATTR_MODE,
2019-07-31 19:25:30 +00:00
CONF_HOST,
CONF_NAME,
CONF_PORT,
2019-07-31 19:25:30 +00:00
)
2024-01-31 17:38:14 +00:00
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
import homeassistant.helpers.config_validation as cv
2024-01-31 17:38:14 +00:00
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2024-01-31 17:38:14 +00:00
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
2024-01-31 17:38:14 +00:00
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
ATTR_CLIMB = "climb"
ATTR_ELEVATION = "elevation"
ATTR_GPS_TIME = "gps_time"
ATTR_SPEED = "speed"
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "GPS"
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
}
)
2024-01-31 17:38:14 +00:00
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the GPSD component."""
async_add_entities(
[
GpsdSensor(
config_entry.data[CONF_HOST],
config_entry.data[CONF_PORT],
config_entry.entry_id,
)
]
)
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
2024-01-31 17:38:14 +00:00
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
2024-01-31 17:38:14 +00:00
"""Initialize gpsd import from config."""
async_create_issue(
hass,
HOMEASSISTANT_DOMAIN,
f"deprecated_yaml_{DOMAIN}",
is_fixable=False,
breaks_in_ha_version="2024.9.0",
severity=IssueSeverity.WARNING,
translation_key="deprecated_yaml",
translation_placeholders={
"domain": DOMAIN,
"integration_title": "GPSD",
},
)
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=config
)
)
class GpsdSensor(SensorEntity):
"""Representation of a GPS receiver available via GPSD."""
2024-01-31 17:38:14 +00:00
_attr_has_entity_name = True
_attr_name = None
def __init__(
self,
host: str,
port: int,
2024-01-31 17:38:14 +00:00
unique_id: str,
) -> None:
"""Initialize the GPSD sensor."""
2024-01-31 17:38:14 +00:00
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, unique_id)},
entry_type=DeviceEntryType.SERVICE,
)
self._attr_unique_id = unique_id
self.agps_thread = AGPS3mechanism()
2024-01-31 17:38:14 +00:00
self.agps_thread.stream_data(host=host, port=port)
self.agps_thread.run_thread()
@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
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes of the GPS."""
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,
}
2023-09-14 07:27:16 +00:00
@property
def icon(self) -> str:
"""Return the icon of the sensor."""
mode = self.agps_thread.data_stream.mode
if isinstance(mode, int) and mode >= 2:
return "mdi:crosshairs-gps"
return "mdi:crosshairs"