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
pull/132783/head
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,
PowerDict,
StatusDict,
UsageDict,
get_sleep_config,
history_stats,
location_data,
@ -41,6 +42,7 @@ class StarlinkData:
status: StatusDict
obstruction: ObstructionDict
alert: AlertDict
usage: UsageDict
consumption: PowerDict
@ -60,12 +62,15 @@ class StarlinkUpdateCoordinator(DataUpdateCoordinator[StarlinkData]):
def _get_starlink_data(self) -> StarlinkData:
"""Retrieve Starlink data."""
channel_context = self.channel_context
location = location_data(channel_context)
sleep = get_sleep_config(channel_context)
status, obstruction, alert = status_data(channel_context)
statistics = history_stats(parse_samples=-1, context=channel_context)
return StarlinkData(location, sleep, status, obstruction, alert, statistics[-1])
context = self.channel_context
status = status_data(context)
location = location_data(context)
sleep = get_sleep_config(context)
status, obstruction, alert = status_data(context)
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 with asyncio.timeout(4):

View File

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

View File

@ -19,6 +19,7 @@ from homeassistant.const import (
EntityCategory,
UnitOfDataRate,
UnitOfEnergy,
UnitOfInformation,
UnitOfPower,
UnitOfTime,
)
@ -122,6 +123,24 @@ SENSORS: tuple[StarlinkSensorEntityDescription, ...] = (
native_unit_of_measurement=PERCENTAGE,
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(
key="power",
device_class=SensorDeviceClass.POWER,

View File

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

View File

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