mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 18:57:06 +00:00
Use SensorEntityDescription in Mazda integration (#63423)
* Use SensorEntityDescription in Mazda integration * Change lambdas to functions * Minor fixes * Address review comments
This commit is contained in:
parent
eb50de5baa
commit
8915b73f72
@ -1,9 +1,13 @@
|
|||||||
"""Platform for Mazda sensor integration."""
|
"""Platform for Mazda sensor integration."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from collections.abc import Callable
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
|
CONF_UNIT_SYSTEM_IMPERIAL,
|
||||||
LENGTH_KILOMETERS,
|
LENGTH_KILOMETERS,
|
||||||
LENGTH_MILES,
|
LENGTH_MILES,
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
@ -11,11 +15,157 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
from homeassistant.helpers.typing import StateType
|
||||||
|
from homeassistant.util.unit_system import UnitSystem
|
||||||
|
|
||||||
from . import MazdaEntity
|
from . import MazdaEntity
|
||||||
from .const import DATA_CLIENT, DATA_COORDINATOR, DOMAIN
|
from .const import DATA_CLIENT, DATA_COORDINATOR, DOMAIN
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class MazdaSensorRequiredKeysMixin:
|
||||||
|
"""Mixin for required keys."""
|
||||||
|
|
||||||
|
# Suffix to be appended to the vehicle name to obtain the sensor name
|
||||||
|
name_suffix: str
|
||||||
|
|
||||||
|
# Function to determine the value for this sensor, given the coordinator data and the configured unit system
|
||||||
|
value: Callable[[dict, UnitSystem], StateType]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class MazdaSensorEntityDescription(
|
||||||
|
SensorEntityDescription, MazdaSensorRequiredKeysMixin
|
||||||
|
):
|
||||||
|
"""Describes a Mazda sensor entity."""
|
||||||
|
|
||||||
|
# Function to determine whether the vehicle supports this sensor, given the coordinator data
|
||||||
|
is_supported: Callable[[dict], bool] = lambda data: True
|
||||||
|
|
||||||
|
# Function to determine the unit of measurement for this sensor, given the configured unit system
|
||||||
|
# Falls back to description.native_unit_of_measurement if it is not provided
|
||||||
|
unit: Callable[[UnitSystem], str | None] | None = None
|
||||||
|
|
||||||
|
|
||||||
|
def _get_distance_unit(unit_system):
|
||||||
|
"""Return the distance unit for the given unit system."""
|
||||||
|
if unit_system.name == CONF_UNIT_SYSTEM_IMPERIAL:
|
||||||
|
return LENGTH_MILES
|
||||||
|
return LENGTH_KILOMETERS
|
||||||
|
|
||||||
|
|
||||||
|
def _front_left_tire_pressure_supported(data):
|
||||||
|
"""Determine if front left tire pressure is supported."""
|
||||||
|
return data["status"]["tirePressure"]["frontLeftTirePressurePsi"] is not None
|
||||||
|
|
||||||
|
|
||||||
|
def _front_right_tire_pressure_supported(data):
|
||||||
|
"""Determine if front right tire pressure is supported."""
|
||||||
|
return data["status"]["tirePressure"]["frontRightTirePressurePsi"] is not None
|
||||||
|
|
||||||
|
|
||||||
|
def _rear_left_tire_pressure_supported(data):
|
||||||
|
"""Determine if rear left tire pressure is supported."""
|
||||||
|
return data["status"]["tirePressure"]["rearLeftTirePressurePsi"] is not None
|
||||||
|
|
||||||
|
|
||||||
|
def _rear_right_tire_pressure_supported(data):
|
||||||
|
"""Determine if rear right tire pressure is supported."""
|
||||||
|
return data["status"]["tirePressure"]["rearRightTirePressurePsi"] is not None
|
||||||
|
|
||||||
|
|
||||||
|
def _fuel_distance_remaining_value(data, unit_system):
|
||||||
|
"""Get the fuel distance remaining value."""
|
||||||
|
return round(
|
||||||
|
unit_system.length(data["status"]["fuelDistanceRemainingKm"], LENGTH_KILOMETERS)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _odometer_value(data, unit_system):
|
||||||
|
"""Get the odometer value."""
|
||||||
|
return round(unit_system.length(data["status"]["odometerKm"], LENGTH_KILOMETERS))
|
||||||
|
|
||||||
|
|
||||||
|
def _front_left_tire_pressure_value(data, unit_system):
|
||||||
|
"""Get the front left tire pressure value."""
|
||||||
|
return round(data["status"]["tirePressure"]["frontLeftTirePressurePsi"])
|
||||||
|
|
||||||
|
|
||||||
|
def _front_right_tire_pressure_value(data, unit_system):
|
||||||
|
"""Get the front right tire pressure value."""
|
||||||
|
return round(data["status"]["tirePressure"]["frontRightTirePressurePsi"])
|
||||||
|
|
||||||
|
|
||||||
|
def _rear_left_tire_pressure_value(data, unit_system):
|
||||||
|
"""Get the rear left tire pressure value."""
|
||||||
|
return round(data["status"]["tirePressure"]["rearLeftTirePressurePsi"])
|
||||||
|
|
||||||
|
|
||||||
|
def _rear_right_tire_pressure_value(data, unit_system):
|
||||||
|
"""Get the rear right tire pressure value."""
|
||||||
|
return round(data["status"]["tirePressure"]["rearRightTirePressurePsi"])
|
||||||
|
|
||||||
|
|
||||||
|
SENSOR_ENTITIES = [
|
||||||
|
MazdaSensorEntityDescription(
|
||||||
|
key="fuel_remaining_percentage",
|
||||||
|
name_suffix="Fuel Remaining Percentage",
|
||||||
|
icon="mdi:gas-station",
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
is_supported=lambda data: data["status"]["fuelRemainingPercent"] is not None,
|
||||||
|
value=lambda data, unit_system: data["status"]["fuelRemainingPercent"],
|
||||||
|
),
|
||||||
|
MazdaSensorEntityDescription(
|
||||||
|
key="fuel_distance_remaining",
|
||||||
|
name_suffix="Fuel Distance Remaining",
|
||||||
|
icon="mdi:gas-station",
|
||||||
|
unit=_get_distance_unit,
|
||||||
|
is_supported=lambda data: data["status"]["fuelDistanceRemainingKm"] is not None,
|
||||||
|
value=_fuel_distance_remaining_value,
|
||||||
|
),
|
||||||
|
MazdaSensorEntityDescription(
|
||||||
|
key="odometer",
|
||||||
|
name_suffix="Odometer",
|
||||||
|
icon="mdi:speedometer",
|
||||||
|
unit=_get_distance_unit,
|
||||||
|
is_supported=lambda data: data["status"]["odometerKm"] is not None,
|
||||||
|
value=_odometer_value,
|
||||||
|
),
|
||||||
|
MazdaSensorEntityDescription(
|
||||||
|
key="front_left_tire_pressure",
|
||||||
|
name_suffix="Front Left Tire Pressure",
|
||||||
|
icon="mdi:car-tire-alert",
|
||||||
|
native_unit_of_measurement=PRESSURE_PSI,
|
||||||
|
is_supported=_front_left_tire_pressure_supported,
|
||||||
|
value=_front_left_tire_pressure_value,
|
||||||
|
),
|
||||||
|
MazdaSensorEntityDescription(
|
||||||
|
key="front_right_tire_pressure",
|
||||||
|
name_suffix="Front Right Tire Pressure",
|
||||||
|
icon="mdi:car-tire-alert",
|
||||||
|
native_unit_of_measurement=PRESSURE_PSI,
|
||||||
|
is_supported=_front_right_tire_pressure_supported,
|
||||||
|
value=_front_right_tire_pressure_value,
|
||||||
|
),
|
||||||
|
MazdaSensorEntityDescription(
|
||||||
|
key="rear_left_tire_pressure",
|
||||||
|
name_suffix="Rear Left Tire Pressure",
|
||||||
|
icon="mdi:car-tire-alert",
|
||||||
|
native_unit_of_measurement=PRESSURE_PSI,
|
||||||
|
is_supported=_rear_left_tire_pressure_supported,
|
||||||
|
value=_rear_left_tire_pressure_value,
|
||||||
|
),
|
||||||
|
MazdaSensorEntityDescription(
|
||||||
|
key="rear_right_tire_pressure",
|
||||||
|
name_suffix="Rear Right Tire Pressure",
|
||||||
|
icon="mdi:car-tire-alert",
|
||||||
|
native_unit_of_measurement=PRESSURE_PSI,
|
||||||
|
is_supported=_rear_right_tire_pressure_supported,
|
||||||
|
value=_rear_right_tire_pressure_value,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: ConfigEntry,
|
||||||
@ -27,170 +177,37 @@ async def async_setup_entry(
|
|||||||
|
|
||||||
entities: list[SensorEntity] = []
|
entities: list[SensorEntity] = []
|
||||||
|
|
||||||
for index, _ in enumerate(coordinator.data):
|
for index, data in enumerate(coordinator.data):
|
||||||
entities.append(MazdaFuelRemainingSensor(client, coordinator, index))
|
for description in SENSOR_ENTITIES:
|
||||||
entities.append(MazdaFuelDistanceSensor(client, coordinator, index))
|
if description.is_supported(data):
|
||||||
entities.append(MazdaOdometerSensor(client, coordinator, index))
|
entities.append(
|
||||||
entities.append(MazdaFrontLeftTirePressureSensor(client, coordinator, index))
|
MazdaSensorEntity(client, coordinator, index, description)
|
||||||
entities.append(MazdaFrontRightTirePressureSensor(client, coordinator, index))
|
)
|
||||||
entities.append(MazdaRearLeftTirePressureSensor(client, coordinator, index))
|
|
||||||
entities.append(MazdaRearRightTirePressureSensor(client, coordinator, index))
|
|
||||||
|
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class MazdaFuelRemainingSensor(MazdaEntity, SensorEntity):
|
class MazdaSensorEntity(MazdaEntity, SensorEntity):
|
||||||
"""Class for the fuel remaining sensor."""
|
"""Representation of a Mazda vehicle sensor."""
|
||||||
|
|
||||||
_attr_native_unit_of_measurement = PERCENTAGE
|
entity_description: MazdaSensorEntityDescription
|
||||||
_attr_icon = "mdi:gas-station"
|
|
||||||
|
|
||||||
def __init__(self, client, coordinator, index) -> None:
|
def __init__(self, client, coordinator, index, description):
|
||||||
"""Initialize Mazda fuel remaining sensor."""
|
"""Initialize Mazda sensor."""
|
||||||
super().__init__(client, coordinator, index)
|
super().__init__(client, coordinator, index)
|
||||||
|
self.entity_description = description
|
||||||
|
|
||||||
self._attr_name = f"{self.vehicle_name} Fuel Remaining Percentage"
|
self._attr_name = f"{self.vehicle_name} {description.name_suffix}"
|
||||||
self._attr_unique_id = f"{self.vin}_fuel_remaining_percentage"
|
self._attr_unique_id = f"{self.vin}_{description.key}"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def native_unit_of_measurement(self):
|
||||||
|
"""Return the unit of measurement for the sensor, according to the configured unit system."""
|
||||||
|
if unit_fn := self.entity_description.unit:
|
||||||
|
return unit_fn(self.hass.config.units)
|
||||||
|
return self.entity_description.native_unit_of_measurement
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self):
|
def native_value(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self.data["status"]["fuelRemainingPercent"]
|
return self.entity_description.value(self.data, self.hass.config.units)
|
||||||
|
|
||||||
|
|
||||||
class MazdaFuelDistanceSensor(MazdaEntity, SensorEntity):
|
|
||||||
"""Class for the fuel distance sensor."""
|
|
||||||
|
|
||||||
_attr_icon = "mdi:gas-station"
|
|
||||||
|
|
||||||
def __init__(self, client, coordinator, index) -> None:
|
|
||||||
"""Initialize Mazda fuel distance sensor."""
|
|
||||||
super().__init__(client, coordinator, index)
|
|
||||||
|
|
||||||
self._attr_name = f"{self.vehicle_name} Fuel Distance Remaining"
|
|
||||||
self._attr_unique_id = f"{self.vin}_fuel_distance_remaining"
|
|
||||||
self._attr_native_unit_of_measurement = (
|
|
||||||
LENGTH_KILOMETERS
|
|
||||||
if coordinator.hass.config.units.is_metric
|
|
||||||
else LENGTH_MILES
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
fuel_distance_km = self.data["status"]["fuelDistanceRemainingKm"]
|
|
||||||
return (
|
|
||||||
None
|
|
||||||
if fuel_distance_km is None
|
|
||||||
else round(
|
|
||||||
self.hass.config.units.length(fuel_distance_km, LENGTH_KILOMETERS)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class MazdaOdometerSensor(MazdaEntity, SensorEntity):
|
|
||||||
"""Class for the odometer sensor."""
|
|
||||||
|
|
||||||
_attr_icon = "mdi:speedometer"
|
|
||||||
|
|
||||||
def __init__(self, client, coordinator, index) -> None:
|
|
||||||
"""Initialize Mazda odometer sensor."""
|
|
||||||
super().__init__(client, coordinator, index)
|
|
||||||
|
|
||||||
self._attr_name = f"{self.vehicle_name} Odometer"
|
|
||||||
self._attr_unique_id = f"{self.vin}_odometer"
|
|
||||||
self._attr_native_unit_of_measurement = (
|
|
||||||
LENGTH_KILOMETERS
|
|
||||||
if coordinator.hass.config.units.is_metric
|
|
||||||
else LENGTH_MILES
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
odometer_km = self.data["status"]["odometerKm"]
|
|
||||||
return (
|
|
||||||
None
|
|
||||||
if odometer_km is None
|
|
||||||
else round(self.hass.config.units.length(odometer_km, LENGTH_KILOMETERS))
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class MazdaFrontLeftTirePressureSensor(MazdaEntity, SensorEntity):
|
|
||||||
"""Class for the front left tire pressure sensor."""
|
|
||||||
|
|
||||||
_attr_native_unit_of_measurement = PRESSURE_PSI
|
|
||||||
_attr_icon = "mdi:car-tire-alert"
|
|
||||||
|
|
||||||
def __init__(self, client, coordinator, index) -> None:
|
|
||||||
"""Initialize Mazda front left tire pressure sensor."""
|
|
||||||
super().__init__(client, coordinator, index)
|
|
||||||
|
|
||||||
self._attr_name = f"{self.vehicle_name} Front Left Tire Pressure"
|
|
||||||
self._attr_unique_id = f"{self.vin}_front_left_tire_pressure"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
tire_pressure = self.data["status"]["tirePressure"]["frontLeftTirePressurePsi"]
|
|
||||||
return None if tire_pressure is None else round(tire_pressure)
|
|
||||||
|
|
||||||
|
|
||||||
class MazdaFrontRightTirePressureSensor(MazdaEntity, SensorEntity):
|
|
||||||
"""Class for the front right tire pressure sensor."""
|
|
||||||
|
|
||||||
_attr_native_unit_of_measurement = PRESSURE_PSI
|
|
||||||
_attr_icon = "mdi:car-tire-alert"
|
|
||||||
|
|
||||||
def __init__(self, client, coordinator, index) -> None:
|
|
||||||
"""Initialize Mazda front right tire pressure sensor."""
|
|
||||||
super().__init__(client, coordinator, index)
|
|
||||||
|
|
||||||
self._attr_name = f"{self.vehicle_name} Front Right Tire Pressure"
|
|
||||||
self._attr_unique_id = f"{self.vin}_front_right_tire_pressure"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
tire_pressure = self.data["status"]["tirePressure"]["frontRightTirePressurePsi"]
|
|
||||||
return None if tire_pressure is None else round(tire_pressure)
|
|
||||||
|
|
||||||
|
|
||||||
class MazdaRearLeftTirePressureSensor(MazdaEntity, SensorEntity):
|
|
||||||
"""Class for the rear left tire pressure sensor."""
|
|
||||||
|
|
||||||
_attr_native_unit_of_measurement = PRESSURE_PSI
|
|
||||||
_attr_icon = "mdi:car-tire-alert"
|
|
||||||
|
|
||||||
def __init__(self, client, coordinator, index) -> None:
|
|
||||||
"""Initialize Mazda rear left tire pressure sensor."""
|
|
||||||
super().__init__(client, coordinator, index)
|
|
||||||
|
|
||||||
self._attr_name = f"{self.vehicle_name} Rear Left Tire Pressure"
|
|
||||||
self._attr_unique_id = f"{self.vin}_rear_left_tire_pressure"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
tire_pressure = self.data["status"]["tirePressure"]["rearLeftTirePressurePsi"]
|
|
||||||
return None if tire_pressure is None else round(tire_pressure)
|
|
||||||
|
|
||||||
|
|
||||||
class MazdaRearRightTirePressureSensor(MazdaEntity, SensorEntity):
|
|
||||||
"""Class for the rear right tire pressure sensor."""
|
|
||||||
|
|
||||||
_attr_native_unit_of_measurement = PRESSURE_PSI
|
|
||||||
_attr_icon = "mdi:car-tire-alert"
|
|
||||||
|
|
||||||
def __init__(self, client, coordinator, index) -> None:
|
|
||||||
"""Initialize Mazda rear right tire pressure sensor."""
|
|
||||||
super().__init__(client, coordinator, index)
|
|
||||||
|
|
||||||
self._attr_name = f"{self.vehicle_name} Rear Right Tire Pressure"
|
|
||||||
self._attr_unique_id = f"{self.vin}_rear_right_tire_pressure"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
tire_pressure = self.data["status"]["tirePressure"]["rearRightTirePressurePsi"]
|
|
||||||
return None if tire_pressure is None else round(tire_pressure)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user