Add Starlink usage sensors (#132738)

* Add usage metrics returned from history_stats

* Add upload and download usage sensors

* Add strings for upload and download usage sensors

* Add usage to test_diagnostics.ambr

* Add icons for upload and download usage sensors

* Add suggested_unit_of_measurement to GIGABYTES
This commit is contained in:
David Rapan 2024-12-10 08:26:42 +01:00 committed by GitHub
parent 580a8d66b2
commit 397091cc7d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 46 additions and 6 deletions

View File

@ -16,6 +16,7 @@ from starlink_grpc import (
ObstructionDict, ObstructionDict,
PowerDict, PowerDict,
StatusDict, StatusDict,
UsageDict,
get_sleep_config, get_sleep_config,
history_stats, history_stats,
location_data, location_data,
@ -41,6 +42,7 @@ class StarlinkData:
status: StatusDict status: StatusDict
obstruction: ObstructionDict obstruction: ObstructionDict
alert: AlertDict alert: AlertDict
usage: UsageDict
consumption: PowerDict consumption: PowerDict
@ -60,12 +62,15 @@ class StarlinkUpdateCoordinator(DataUpdateCoordinator[StarlinkData]):
def _get_starlink_data(self) -> StarlinkData: def _get_starlink_data(self) -> StarlinkData:
"""Retrieve Starlink data.""" """Retrieve Starlink data."""
channel_context = self.channel_context context = self.channel_context
location = location_data(channel_context) status = status_data(context)
sleep = get_sleep_config(channel_context) location = location_data(context)
status, obstruction, alert = status_data(channel_context) sleep = get_sleep_config(context)
statistics = history_stats(parse_samples=-1, context=channel_context) status, obstruction, alert = status_data(context)
return StarlinkData(location, sleep, status, obstruction, alert, statistics[-1]) usage, consumption = history_stats(parse_samples=-1, context=context)[-2:]
return StarlinkData(
location, sleep, status, obstruction, alert, usage, consumption
)
async def _async_update_data(self) -> StarlinkData: async def _async_update_data(self) -> StarlinkData:
async with asyncio.timeout(4): async with asyncio.timeout(4):

View File

@ -18,6 +18,12 @@
}, },
"last_boot_time": { "last_boot_time": {
"default": "mdi:clock" "default": "mdi:clock"
},
"upload": {
"default": "mdi:upload"
},
"download": {
"default": "mdi:download"
} }
} }
} }

View File

@ -19,6 +19,7 @@ from homeassistant.const import (
EntityCategory, EntityCategory,
UnitOfDataRate, UnitOfDataRate,
UnitOfEnergy, UnitOfEnergy,
UnitOfInformation,
UnitOfPower, UnitOfPower,
UnitOfTime, UnitOfTime,
) )
@ -122,6 +123,24 @@ SENSORS: tuple[StarlinkSensorEntityDescription, ...] = (
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
value_fn=lambda data: data.status["pop_ping_drop_rate"] * 100, value_fn=lambda data: data.status["pop_ping_drop_rate"] * 100,
), ),
StarlinkSensorEntityDescription(
key="upload",
translation_key="upload",
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfInformation.BYTES,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
value_fn=lambda data: data.usage["upload_usage"],
),
StarlinkSensorEntityDescription(
key="download",
translation_key="download",
device_class=SensorDeviceClass.DATA_SIZE,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfInformation.BYTES,
suggested_unit_of_measurement=UnitOfInformation.GIGABYTES,
value_fn=lambda data: data.usage["download_usage"],
),
StarlinkSensorEntityDescription( StarlinkSensorEntityDescription(
key="power", key="power",
device_class=SensorDeviceClass.POWER, device_class=SensorDeviceClass.POWER,

View File

@ -70,6 +70,12 @@
}, },
"ping_drop_rate": { "ping_drop_rate": {
"name": "Ping drop rate" "name": "Ping drop rate"
},
"upload": {
"name": "Upload"
},
"download": {
"name": "Download"
} }
}, },
"switch": { "switch": {

View File

@ -86,5 +86,9 @@
'uplink_throughput_bps': 11802.771484375, 'uplink_throughput_bps': 11802.771484375,
'uptime': 804138, 'uptime': 804138,
}), }),
'usage': dict({
'download_usage': 72504227,
'upload_usage': 5719755,
}),
}) })
# --- # ---