diff --git a/custom_components/audiconnect/binary_sensor.py b/custom_components/audiconnect/binary_sensor.py index 8ecca610..e2066836 100644 --- a/custom_components/audiconnect/binary_sensor.py +++ b/custom_components/audiconnect/binary_sensor.py @@ -2,7 +2,7 @@ import logging -from homeassistant.components.binary_sensor import DEVICE_CLASSES, BinarySensorEntity +from homeassistant.components.binary_sensor import BinarySensorEntity from homeassistant.const import CONF_USERNAME from .audi_entity import AudiEntity @@ -37,7 +37,5 @@ def is_on(self): @property def device_class(self): - """Return the class of this sensor, from DEVICE_CLASSES.""" - if self._instrument.device_class in DEVICE_CLASSES: - return self._instrument.device_class - return None + """Return the device_class of this sensor.""" + return self._instrument.device_class diff --git a/custom_components/audiconnect/dashboard.py b/custom_components/audiconnect/dashboard.py index b63b3b21..c11792fd 100644 --- a/custom_components/audiconnect/dashboard.py +++ b/custom_components/audiconnect/dashboard.py @@ -3,6 +3,22 @@ import logging import re +from homeassistant.components.sensor import ( + SensorDeviceClass, + SensorStateClass, +) + +from homeassistant.components.binary_sensor import BinarySensorDeviceClass + +from homeassistant.const import ( + PERCENTAGE, + UnitOfTime, + UnitOfLength, + UnitOfTemperature, + UnitOfPower, + UnitOfElectricCurrent, +) + _LOGGER = logging.getLogger(__name__) @@ -125,10 +141,13 @@ def attributes(self): class Sensor(Instrument): - def __init__(self, attr, name, icon, unit): + def __init__( + self, attr, name, icon=None, unit=None, state_class=None, device_class=None + ): super().__init__(component="sensor", attr=attr, name=name, icon=icon) - self.device_class = None + self.device_class = device_class self._unit = unit + self.state_class = state_class self._convert = False def configurate(self, unit_system=None, **config): @@ -170,7 +189,7 @@ def unit(self): class BinarySensor(Instrument): - def __init__(self, attr, name, device_class, icon=None): + def __init__(self, attr, name, device_class=None, icon=None): super().__init__(component="binary_sensor", attr=attr, name=name, icon=icon) self.device_class = device_class @@ -321,6 +340,7 @@ def __init__(self, attr, name): super().__init__(component="sensor", attr=attr, name=name) self.device_class = None self.unit = None + self.state_class = None @property def is_mutable(self): @@ -364,6 +384,7 @@ def __init__(self): ) self.device_class = "timestamp" self.unit = None + self.state_class = None @property def is_mutable(self): @@ -390,217 +411,296 @@ def create_instruments(): TripData(attr="longterm_reset", name="LongTerm Trip User Reset"), Lock(), Preheater(), - Sensor(attr="model", name="Model", icon="mdi:car-info", unit=None), - Sensor(attr="mileage", name="Mileage", icon="mdi:speedometer", unit="km"), + Sensor( + attr="model", + name="Model", + icon="mdi:car-info", + ), + Sensor( + attr="mileage", + name="Mileage", + icon="mdi:counter", + unit=UnitOfLength.KILOMETERS, + state_class=SensorStateClass.TOTAL_INCREASING, + device_class=SensorDeviceClass.DISTANCE, + ), Sensor( attr="service_adblue_distance", name="AdBlue range", - icon="mdi:gas-station", - unit="km", + icon="mdi:map-marker-distance", + unit=UnitOfLength.KILOMETERS, + device_class=SensorDeviceClass.DISTANCE, + ), + Sensor( + attr="range", + name="Range", + icon="mdi:map-marker-distance", + unit=UnitOfLength.KILOMETERS, + device_class=SensorDeviceClass.DISTANCE, ), - Sensor(attr="range", name="Range", icon="mdi:gas-station", unit="km"), Sensor( attr="hybrid_range", name="hybrid Range", - icon="mdi:gas-station-outline", - unit="km", + icon="mdi:map-marker-distance", + unit=UnitOfLength.KILOMETERS, + device_class=SensorDeviceClass.DISTANCE, ), Sensor( attr="service_inspection_time", name="Service inspection time", icon="mdi:room-service-outline", - unit="days", + unit=UnitOfTime.DAYS, ), Sensor( attr="service_inspection_distance", name="Service inspection distance", icon="mdi:room-service-outline", - unit="km", + unit=UnitOfLength.KILOMETERS, + device_class=SensorDeviceClass.DISTANCE, ), Sensor( - attr="oil_change_time", name="Oil change time", icon="mdi:oil", unit="days" + attr="oil_change_time", + name="Oil change time", + icon="mdi:oil", + unit=UnitOfTime.DAYS, ), Sensor( attr="oil_change_distance", name="Oil change distance", icon="mdi:oil", - unit="km", + unit=UnitOfLength.KILOMETERS, + device_class=SensorDeviceClass.DISTANCE, + ), + Sensor( + attr="oil_level", + name="Oil level", + icon="mdi:oil", + unit=PERCENTAGE, ), - Sensor(attr="oil_level", name="Oil level", icon="mdi:oil", unit="%"), Sensor( attr="charging_state", name="Charging state", icon="mdi:car-battery", - unit=None, ), - Sensor(attr="charging_mode", name="Charging mode", icon=None, unit=None), - Sensor(attr="energy_flow", name="Energy flow", icon=None, unit=None), + Sensor( + attr="charging_mode", + name="Charging mode", + ), + Sensor( + attr="energy_flow", + name="Energy flow", + ), Sensor( attr="max_charge_current", name="Max charge current", icon="mdi:current-ac", - unit="A", + unit=UnitOfElectricCurrent.AMPERE, + device_class=SensorDeviceClass.CURRENT, ), Sensor( attr="primary_engine_type", name="Primary engine type", icon="mdi:engine", - unit=None, ), Sensor( attr="secondary_engine_type", name="Secondary engine type", icon="mdi:engine", - unit=None, ), Sensor( attr="primary_engine_range", name="Primary engine range", - icon="mdi:gas-station-outline", - unit="km", + icon="mdi:map-marker-distance", + unit=UnitOfLength.KILOMETERS, + device_class=SensorDeviceClass.DISTANCE, ), Sensor( attr="secondary_engine_range", name="Secondary engine range", - icon="mdi:gas-station-outline", - unit="km", + icon="mdi:map-marker-distance", + unit=UnitOfLength.KILOMETERS, + device_class=SensorDeviceClass.DISTANCE, ), Sensor( attr="primary_engine_range_percent", name="Primary engine Percent", - icon="mdi:gas-station-outline", - unit="%", + icon="mdi:gauge", + unit=PERCENTAGE, ), Sensor( attr="car_type", name="Car Type", icon="mdi:car-info", - unit=None, ), Sensor( attr="secondary_engine_range_percent", name="Secondary engine Percent", - icon="mdi:gas-station-outline", - unit="%", + icon="mdi:gauge", + unit=PERCENTAGE, ), Sensor( - attr="charging_power", name="Charging power", icon="mdi:flash", unit="kW" + attr="charging_power", + name="Charging power", + icon="mdi:flash", + unit=UnitOfPower.KILO_WATT, + device_class=SensorDeviceClass.POWER, ), Sensor( attr="actual_charge_rate", name="Charging rate", icon="mdi:electron-framework", - unit=None, ), - Sensor(attr="tank_level", name="Tank level", icon="mdi:gas-station", unit="%"), + Sensor( + attr="tank_level", + name="Tank level", + icon="mdi:gauge", + unit=PERCENTAGE, + ), Sensor( attr="state_of_charge", name="State of charge", icon="mdi:ev-station", - unit="%", + unit=PERCENTAGE, ), Sensor( attr="remaining_charging_time", name="Remaining charge time", icon="mdi:battery-charging", - unit=None, ), - Sensor(attr="plug_state", name="Plug state", icon="mdi:power-plug", unit=None), + Sensor( + attr="plug_state", + name="Plug state", + icon="mdi:ev-plug-type1", + ), Sensor( attr="plug_lock_state", name="Plug Lock state", - icon="mdi:power-plug", - unit=None, + icon="mdi:ev-plug-type1", ), Sensor( attr="external_power", name="External Power", icon="mdi:ev-station", - unit=None, ), Sensor( attr="plug_led_color", name="Plug LED Color", - icon="mdi:power-plug", - unit=None, + icon="mdi:ev-plug-type1", ), Sensor( attr="doors_trunk_status", name="Doors/trunk state", icon="mdi:car-door", - unit=None, ), Sensor( attr="climatisation_state", name="Climatisation state", icon="mdi:air-conditioner", - unit=None, ), Sensor( attr="outdoor_temperature", name="Outdoor Temperature", icon="mdi:temperature-celsius", - unit="°C", + unit=UnitOfTemperature.CELSIUS, + device_class=SensorDeviceClass.TEMPERATURE, ), Sensor( attr="preheater_duration", name="Preheater runtime", icon="mdi:clock", - unit="Min", + unit=UnitOfTime.MINUTES, ), Sensor( attr="preheater_remaining", name="Preheater remaining", icon="mdi:clock", - unit="Min", + unit=UnitOfTime.MINUTES, + ), + BinarySensor( + attr="sun_roof", + name="Sun roof", + device_class=BinarySensorDeviceClass.WINDOW, ), - BinarySensor(attr="sun_roof", name="Sun roof", device_class="window"), BinarySensor( attr="parking_light", name="Parking light", - device_class="safety", + device_class=BinarySensorDeviceClass.SAFETY, icon="mdi:lightbulb", ), - BinarySensor(attr="any_window_open", name="Windows", device_class="window"), - BinarySensor(attr="any_door_unlocked", name="Doors lock", device_class="lock"), - BinarySensor(attr="any_door_open", name="Doors", device_class="door"), - BinarySensor(attr="trunk_unlocked", name="Trunk lock", device_class="lock"), - BinarySensor(attr="trunk_open", name="Trunk", device_class="door"), - BinarySensor(attr="hood_open", name="Hood", device_class="door"), BinarySensor( - attr="left_front_door_open", name="Left front door", device_class="door" + attr="any_window_open", + name="Windows", + device_class=BinarySensorDeviceClass.WINDOW, + ), + BinarySensor( + attr="any_door_unlocked", + name="Doors lock", + device_class=BinarySensorDeviceClass.LOCK, + ), + BinarySensor( + attr="any_door_open", + name="Doors", + device_class=BinarySensorDeviceClass.DOOR, + ), + BinarySensor( + attr="trunk_unlocked", + name="Trunk lock", + device_class=BinarySensorDeviceClass.LOCK, + ), + BinarySensor( + attr="trunk_open", + name="Trunk", + device_class=BinarySensorDeviceClass.DOOR, + ), + BinarySensor( + attr="hood_open", + name="Hood", + device_class=BinarySensorDeviceClass.DOOR, + ), + BinarySensor( + attr="left_front_door_open", + name="Left front door", + device_class=BinarySensorDeviceClass.DOOR, ), BinarySensor( - attr="right_front_door_open", name="Right front door", device_class="door" + attr="right_front_door_open", + name="Right front door", + device_class=BinarySensorDeviceClass.DOOR, ), BinarySensor( - attr="left_rear_door_open", name="Left rear door", device_class="door" + attr="left_rear_door_open", + name="Left rear door", + device_class=BinarySensorDeviceClass.DOOR, ), BinarySensor( - attr="right_rear_door_open", name="Right rear door", device_class="door" + attr="right_rear_door_open", + name="Right rear door", + device_class=BinarySensorDeviceClass.DOOR, ), BinarySensor( attr="left_front_window_open", name="Left front window", - device_class="window", + device_class=BinarySensorDeviceClass.WINDOW, ), BinarySensor( attr="right_front_window_open", name="Right front window", - device_class="window", + device_class=BinarySensorDeviceClass.WINDOW, ), BinarySensor( - attr="left_rear_window_open", name="Left rear window", device_class="window" + attr="left_rear_window_open", + name="Left rear window", + device_class=BinarySensorDeviceClass.WINDOW, ), BinarySensor( attr="right_rear_window_open", name="Right rear window", - device_class="window", + device_class=BinarySensorDeviceClass.WINDOW, ), BinarySensor( attr="braking_status", name="Braking status", - device_class="safety", + device_class=BinarySensorDeviceClass.SAFETY, icon="mdi:car-brake-abs", ), ] diff --git a/custom_components/audiconnect/sensor.py b/custom_components/audiconnect/sensor.py index 6952368a..2e3eff4d 100644 --- a/custom_components/audiconnect/sensor.py +++ b/custom_components/audiconnect/sensor.py @@ -2,7 +2,7 @@ import logging -from homeassistant.components.sensor import DEVICE_CLASSES, SensorEntity +from homeassistant.components.sensor import SensorEntity from homeassistant.const import CONF_USERNAME from .audi_entity import AudiEntity @@ -43,10 +43,10 @@ def unit_of_measurement(self): @property def device_class(self): - """Return the class of this sensor, from DEVICE_CLASSES.""" - if ( - self._instrument.device_class is not None - and self._instrument.device_class in DEVICE_CLASSES - ): - return self._instrument.device_class - return None + """Return the device_class.""" + return self._instrument.device_class + + @property + def state_class(self): + """Return the state_class.""" + return self._instrument.state_class