Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GPSd satellites sensors #137320

Merged
merged 4 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions homeassistant/components/gpsd/icons.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
},
"elevation": {
"default": "mdi:arrow-up-down"
},
"total_satellites": {
"default": "mdi:satellite-variant"
},
"used_satellites": {
"default": "mdi:satellite-variant"
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions homeassistant/components/gpsd/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import (
ATTR_LATITUDE,
Expand All @@ -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"

Check warning on line 44 in homeassistant/components/gpsd/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/gpsd/sensor.py#L43-L44

Added lines #L43 - L44 were not covered by tests

DEFAULT_NAME = "GPS"

_MODE_VALUES = {2: "2d_fix", 3: "3d_fix"}


def count_total_satellites_fn(agps_thread: AGPS3mechanism) -> int | None:

Check warning on line 51 in homeassistant/components/gpsd/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/gpsd/sensor.py#L51

Added line #L51 was not covered by tests
"""Count the number of total satellites."""
satellites = agps_thread.data_stream.satellites
return None if satellites == "n/a" else len(satellites)

Check warning on line 54 in homeassistant/components/gpsd/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/gpsd/sensor.py#L53-L54

Added lines #L53 - L54 were not covered by tests


def count_used_satellites_fn(agps_thread: AGPS3mechanism) -> int | None:

Check warning on line 57 in homeassistant/components/gpsd/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/gpsd/sensor.py#L57

Added line #L57 was not covered by tests
"""Count the number of used satellites."""
satellites = agps_thread.data_stream.satellites
if satellites == "n/a":
return None

Check warning on line 61 in homeassistant/components/gpsd/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/gpsd/sensor.py#L59-L61

Added lines #L59 - L61 were not covered by tests

return sum(

Check warning on line 63 in homeassistant/components/gpsd/sensor.py

View check run for this annotation

Codecov / codecov/patch

homeassistant/components/gpsd/sensor.py#L63

Added line #L63 was not covered by tests
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."""
Expand Down Expand Up @@ -116,6 +136,22 @@
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,
),
)


Expand Down
8 changes: 8 additions & 0 deletions homeassistant/components/gpsd/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Expand Down