From db8b33c8b3342bada7a7774aedc1ed4a533325a1 Mon Sep 17 00:00:00 2001 From: Jan Rieger <271149+jrieger@users.noreply.github.com> Date: Tue, 4 Feb 2025 13:12:39 +0100 Subject: [PATCH 1/3] Add GPSd satellites sensors --- homeassistant/components/gpsd/icons.json | 6 ++++ homeassistant/components/gpsd/sensor.py | 38 ++++++++++++++++++++++ homeassistant/components/gpsd/strings.json | 6 ++++ 3 files changed, 50 insertions(+) diff --git a/homeassistant/components/gpsd/icons.json b/homeassistant/components/gpsd/icons.json index 59d904f918c5a..3605bdc6d70f4 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 1bac41ecaaead..ef90a9d9cf73e 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 used 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,24 @@ 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, + native_unit_of_measurement="satellites", + 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, + native_unit_of_measurement="satellites", + 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 867edf0b5a8a2..e841caaa4bc63 100644 --- a/homeassistant/components/gpsd/strings.json +++ b/homeassistant/components/gpsd/strings.json @@ -50,6 +50,12 @@ }, "mode": { "name": "[%key:common::config_flow::data::mode%]" } } + }, + "total_satellites": { + "name": "Total satellites" + }, + "used_satellites": { + "name": "Used satellites" } } } From 7cb1ee6defa81e4d312ab1ccbf8879a6e2deca13 Mon Sep 17 00:00:00 2001 From: Jan Rieger <271149+jrieger@users.noreply.github.com> Date: Tue, 4 Feb 2025 13:35:22 +0100 Subject: [PATCH 2/3] Fix comment --- homeassistant/components/gpsd/sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/gpsd/sensor.py b/homeassistant/components/gpsd/sensor.py index ef90a9d9cf73e..0a70df4c7bb89 100644 --- a/homeassistant/components/gpsd/sensor.py +++ b/homeassistant/components/gpsd/sensor.py @@ -49,7 +49,7 @@ def count_total_satellites_fn(agps_thread: AGPS3mechanism) -> int | None: - """Count the number of used satellites.""" + """Count the number of total satellites.""" satellites = agps_thread.data_stream.satellites return None if satellites == "n/a" else len(satellites) From 6ca87645d9b3ad8107e480d503780e5ced2afecb Mon Sep 17 00:00:00 2001 From: Jan Rieger <271149+jrieger@users.noreply.github.com> Date: Tue, 4 Feb 2025 17:08:45 +0100 Subject: [PATCH 3/3] Make unit of measurement translatable --- homeassistant/components/gpsd/sensor.py | 2 -- homeassistant/components/gpsd/strings.json | 6 ++++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/gpsd/sensor.py b/homeassistant/components/gpsd/sensor.py index 0a70df4c7bb89..70d32f88a65f8 100644 --- a/homeassistant/components/gpsd/sensor.py +++ b/homeassistant/components/gpsd/sensor.py @@ -141,7 +141,6 @@ class GpsdSensorDescription(SensorEntityDescription): translation_key=ATTR_TOTAL_SATELLITES, entity_category=EntityCategory.DIAGNOSTIC, state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement="satellites", value_fn=count_total_satellites_fn, entity_registry_enabled_default=False, ), @@ -150,7 +149,6 @@ class GpsdSensorDescription(SensorEntityDescription): translation_key=ATTR_USED_SATELLITES, entity_category=EntityCategory.DIAGNOSTIC, state_class=SensorStateClass.MEASUREMENT, - native_unit_of_measurement="satellites", 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 e841caaa4bc63..a5d6c570b54dd 100644 --- a/homeassistant/components/gpsd/strings.json +++ b/homeassistant/components/gpsd/strings.json @@ -52,10 +52,12 @@ } }, "total_satellites": { - "name": "Total satellites" + "name": "Total satellites", + "unit_of_measurement": "satellites" }, "used_satellites": { - "name": "Used satellites" + "name": "Used satellites", + "unit_of_measurement": "satellites" } } }