1
mirror of https://github.com/home-assistant/core synced 2024-07-15 09:42:11 +02:00

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 <joostlek@outlook.com>

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
Isak Nyberg 2023-12-28 19:27:06 +01:00 committed by GitHub
parent 454201d0a8
commit 756292234e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 2 deletions

View File

@ -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."""

View File

@ -64,6 +64,9 @@
},
"record_adjustments": {
"name": "Record number of adjustments"
},
"record_distance": {
"name": "Record distance"
}
}
}