diff --git a/homeassistant/components/gpsd/icons.json b/homeassistant/components/gpsd/icons.json index 59d904f918c5ae..3605bdc6d70f40 100644 --- a/homeassistant/components/gpsd/icons.json +++ b/homeassistant/components/gpsd/icons.json @@ -16,6 +16,12 @@ }, "elevation": { "default": "mdi:arrow-up-down" + }, + "total_satellites": { + "default": "mdi:satellite-variant" + }, + "used_satellites": { + "default": "mdi:satellite-variant" } } } diff --git a/homeassistant/components/gpsd/sensor.py b/homeassistant/components/gpsd/sensor.py index 1bac41ecaaead9..70d32f88a65f8f 100644 --- a/homeassistant/components/gpsd/sensor.py +++ b/homeassistant/components/gpsd/sensor.py @@ -14,6 +14,7 @@ SensorDeviceClass, SensorEntity, SensorEntityDescription, + SensorStateClass, ) from homeassistant.const import ( ATTR_LATITUDE, @@ -39,12 +40,31 @@ ATTR_ELEVATION = "elevation" ATTR_GPS_TIME = "gps_time" ATTR_SPEED = "speed" +ATTR_TOTAL_SATELLITES = "total_satellites" +ATTR_USED_SATELLITES = "used_satellites" DEFAULT_NAME = "GPS" _MODE_VALUES = {2: "2d_fix", 3: "3d_fix"} +def count_total_satellites_fn(agps_thread: AGPS3mechanism) -> int | None: + """Count the number of total satellites.""" + satellites = agps_thread.data_stream.satellites + return None if satellites == "n/a" else len(satellites) + + +def count_used_satellites_fn(agps_thread: AGPS3mechanism) -> int | None: + """Count the number of used satellites.""" + satellites = agps_thread.data_stream.satellites + if satellites == "n/a": + return None + + return sum( + 1 for sat in satellites if isinstance(sat, dict) and sat.get("used", False) + ) + + @dataclass(frozen=True, kw_only=True) class GpsdSensorDescription(SensorEntityDescription): """Class describing GPSD sensor entities.""" @@ -116,6 +136,22 @@ class GpsdSensorDescription(SensorEntityDescription): suggested_display_precision=2, entity_registry_enabled_default=False, ), + GpsdSensorDescription( + key=ATTR_TOTAL_SATELLITES, + translation_key=ATTR_TOTAL_SATELLITES, + entity_category=EntityCategory.DIAGNOSTIC, + state_class=SensorStateClass.MEASUREMENT, + value_fn=count_total_satellites_fn, + entity_registry_enabled_default=False, + ), + GpsdSensorDescription( + key=ATTR_USED_SATELLITES, + translation_key=ATTR_USED_SATELLITES, + entity_category=EntityCategory.DIAGNOSTIC, + state_class=SensorStateClass.MEASUREMENT, + value_fn=count_used_satellites_fn, + entity_registry_enabled_default=False, + ), ) diff --git a/homeassistant/components/gpsd/strings.json b/homeassistant/components/gpsd/strings.json index 867edf0b5a8a22..a5d6c570b54ddd 100644 --- a/homeassistant/components/gpsd/strings.json +++ b/homeassistant/components/gpsd/strings.json @@ -50,6 +50,14 @@ }, "mode": { "name": "[%key:common::config_flow::data::mode%]" } } + }, + "total_satellites": { + "name": "Total satellites", + "unit_of_measurement": "satellites" + }, + "used_satellites": { + "name": "Used satellites", + "unit_of_measurement": "satellites" } } }