2019-04-03 15:40:03 +00:00
|
|
|
"""Support for GPSD."""
|
2022-01-04 10:08:28 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
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-01-31 17:38:14 +00:00
|
|
|
from gps3.agps3threaded import (
|
|
|
|
GPSD_PORT as DEFAULT_PORT,
|
|
|
|
HOST as DEFAULT_HOST,
|
|
|
|
AGPS3mechanism,
|
|
|
|
)
|
2016-08-13 17:37:12 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-22 18:45:17 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2024-01-31 17:38:14 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2016-08-20 22:40:16 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_LATITUDE,
|
|
|
|
ATTR_LONGITUDE,
|
2019-10-02 16:33:47 +00:00
|
|
|
ATTR_MODE,
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_HOST,
|
|
|
|
CONF_NAME,
|
2019-10-21 08:41:20 +00:00
|
|
|
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
|
2016-08-13 17:37:12 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
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-01-31 17:38:14 +00:00
|
|
|
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
2022-01-04 10:08:28 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2016-08-13 17:37:12 +00:00
|
|
|
|
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
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
)
|
2016-08-13 17:37:12 +00:00
|
|
|
|
|
|
|
|
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(
|
2022-01-04 10:08:28 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
2024-01-31 17:38:14 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2022-01-04 10:08:28 +00:00
|
|
|
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
|
|
|
|
)
|
|
|
|
)
|
2016-08-13 17:37:12 +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
|
|
|
|
_attr_name = None
|
|
|
|
|
2023-09-12 20:21:58 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
host: str,
|
|
|
|
port: int,
|
2024-01-31 17:38:14 +00:00
|
|
|
unique_id: str,
|
2023-09-12 20:21:58 +00:00
|
|
|
) -> None:
|
2016-08-13 17:37:12 +00:00
|
|
|
"""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
|
2016-08-13 17:37:12 +00:00
|
|
|
|
|
|
|
self.agps_thread = AGPS3mechanism()
|
2024-01-31 17:38:14 +00:00
|
|
|
self.agps_thread.stream_data(host=host, port=port)
|
2016-08-13 17:37:12 +00:00
|
|
|
self.agps_thread.run_thread()
|
|
|
|
|
|
|
|
@property
|
2023-09-12 20:21:58 +00:00
|
|
|
def native_value(self) -> str | None:
|
2016-08-13 17:37:12 +00:00
|
|
|
"""Return the state of GPSD."""
|
|
|
|
if self.agps_thread.data_stream.mode == 3:
|
|
|
|
return "3D Fix"
|
2018-07-23 08:16:05 +00:00
|
|
|
if self.agps_thread.data_stream.mode == 2:
|
2016-08-13 17:37:12 +00:00
|
|
|
return "2D Fix"
|
2019-01-24 07:20:20 +00:00
|
|
|
return None
|
2016-08-13 17:37:12 +00:00
|
|
|
|
|
|
|
@property
|
2023-09-12 20:21:58 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
2016-08-13 17:37:12 +00:00
|
|
|
"""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"
|