From 756292234ec4183220803098233ca3336379038f Mon Sep 17 00:00:00 2001 From: Isak Nyberg <36712644+IsakNyberg@users.noreply.github.com> Date: Thu, 28 Dec 2023 19:27:06 +0100 Subject: [PATCH] Add Record distance sensor to MyPermobil (#106519) * add record-distance-sensor * simplify UOM property * remove uom for record_distance description * remove redundant code * Update homeassistant/components/permobil/sensor.py Co-authored-by: Joost Lekkerkerker --------- Co-authored-by: Joost Lekkerkerker --- homeassistant/components/permobil/sensor.py | 29 +++++++++++++++++-- .../components/permobil/strings.json | 3 ++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/permobil/sensor.py b/homeassistant/components/permobil/sensor.py index a48741b0886d..8a504248f5ac 100644 --- a/homeassistant/components/permobil/sensor.py +++ b/homeassistant/components/permobil/sensor.py @@ -15,6 +15,8 @@ from mypermobil import ( BATTERY_MAX_DISTANCE_LEFT, BATTERY_STATE_OF_CHARGE, BATTERY_STATE_OF_HEALTH, + RECORDS_DISTANCE, + RECORDS_DISTANCE_UNIT, RECORDS_SEATING, USAGE_ADJUSTMENTS, USAGE_DISTANCE, @@ -32,7 +34,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity -from .const import BATTERY_ASSUMED_VOLTAGE, DOMAIN +from .const import BATTERY_ASSUMED_VOLTAGE, DOMAIN, KM, MILES from .coordinator import MyPermobilCoordinator _LOGGER = logging.getLogger(__name__) @@ -159,7 +161,7 @@ SENSOR_DESCRIPTIONS: tuple[PermobilSensorEntityDescription, ...] = ( state_class=SensorStateClass.TOTAL_INCREASING, ), PermobilSensorEntityDescription( - # Largest number of adjustemnts in a single 24h period, never resets + # Largest number of adjustemnts in a single 24h period, monotonically increasing, never resets value_fn=lambda data: data.records[RECORDS_SEATING[0]], available_fn=lambda data: RECORDS_SEATING[0] in data.records, key="record_adjustments", @@ -168,8 +170,22 @@ SENSOR_DESCRIPTIONS: tuple[PermobilSensorEntityDescription, ...] = ( native_unit_of_measurement="adjustments", state_class=SensorStateClass.TOTAL_INCREASING, ), + PermobilSensorEntityDescription( + # Record of largest distance travelled in a day, monotonically increasing, never resets + value_fn=lambda data: data.records[RECORDS_DISTANCE[0]], + available_fn=lambda data: RECORDS_DISTANCE[0] in data.records, + key="record_distance", + translation_key="record_distance", + icon="mdi:map-marker-distance", + state_class=SensorStateClass.TOTAL_INCREASING, + ), ) +DISTANCE_UNITS: dict[Any, UnitOfLength] = { + KM: UnitOfLength.KILOMETERS, + MILES: UnitOfLength.MILES, +} + async def async_setup_entry( hass: HomeAssistant, @@ -209,6 +225,15 @@ class PermobilSensor(CoordinatorEntity[MyPermobilCoordinator], SensorEntity): f"{coordinator.p_api.email}_{self.entity_description.key}" ) + @property + def native_unit_of_measurement(self) -> str | None: + """Return the unit of measurement of the sensor.""" + if self.entity_description.key == "record_distance": + return DISTANCE_UNITS.get( + self.coordinator.data.records[RECORDS_DISTANCE_UNIT[0]] + ) + return self.entity_description.native_unit_of_measurement + @property def available(self) -> bool: """Return True if the sensor has value.""" diff --git a/homeassistant/components/permobil/strings.json b/homeassistant/components/permobil/strings.json index b0b630eff082..b500bbdb9ea8 100644 --- a/homeassistant/components/permobil/strings.json +++ b/homeassistant/components/permobil/strings.json @@ -64,6 +64,9 @@ }, "record_adjustments": { "name": "Record number of adjustments" + }, + "record_distance": { + "name": "Record distance" } } }