hOn/custom_components/hon/sensor.py

411 lines
14 KiB
Python
Raw Normal View History

2023-02-19 02:58:21 +01:00
import logging
2023-04-10 07:09:54 +02:00
from pyhon import Hon
2023-02-19 02:58:21 +01:00
from homeassistant.components.sensor import (
SensorEntity,
SensorDeviceClass,
SensorStateClass,
SensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
2023-04-08 04:44:47 +02:00
from homeassistant.const import (
REVOLUTIONS_PER_MINUTE,
UnitOfEnergy,
UnitOfVolume,
UnitOfMass,
UnitOfPower,
UnitOfTime,
2023-04-10 19:51:16 +02:00
UnitOfTemperature,
2023-04-08 04:44:47 +02:00
)
2023-02-19 02:58:21 +01:00
from homeassistant.core import callback
2023-03-03 18:23:30 +01:00
from homeassistant.helpers.entity import EntityCategory
2023-02-19 02:58:21 +01:00
from homeassistant.helpers.typing import StateType
2023-04-16 21:46:17 +02:00
from homeassistant.const import PERCENTAGE
2023-03-03 18:23:30 +01:00
2023-02-19 02:58:21 +01:00
from .const import DOMAIN
2023-03-03 18:23:30 +01:00
from .hon import HonCoordinator, HonEntity
2023-02-19 02:58:21 +01:00
_LOGGER = logging.getLogger(__name__)
SENSORS: dict[str, tuple[SensorEntityDescription, ...]] = {
"WM": (
2023-04-22 02:47:55 +02:00
SensorEntityDescription(
key="prPhase",
name="Program Phase",
icon="mdi:washing-machine",
translation_key="program_phases_wm",
),
2023-02-19 02:58:21 +01:00
SensorEntityDescription(
key="totalElectricityUsed",
name="Total Power",
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
2023-04-10 19:51:16 +02:00
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
2023-02-19 02:58:21 +01:00
),
SensorEntityDescription(
key="totalWaterUsed",
name="Total Water",
device_class=SensorDeviceClass.WATER,
state_class=SensorStateClass.TOTAL_INCREASING,
2023-04-10 19:51:16 +02:00
native_unit_of_measurement=UnitOfVolume.LITERS,
2023-02-19 02:58:21 +01:00
),
SensorEntityDescription(
key="totalWashCycle",
name="Total Wash Cycle",
state_class=SensorStateClass.TOTAL_INCREASING,
2023-04-10 19:51:16 +02:00
icon="mdi:counter",
2023-02-19 02:58:21 +01:00
),
SensorEntityDescription(
key="currentElectricityUsed",
name="Current Electricity Used",
state_class=SensorStateClass.MEASUREMENT,
2023-03-05 00:54:57 +01:00
device_class=SensorDeviceClass.POWER,
native_unit_of_measurement=UnitOfPower.KILO_WATT,
2023-04-10 19:51:16 +02:00
icon="mdi:lightning-bolt",
2023-02-19 02:58:21 +01:00
),
SensorEntityDescription(
key="currentWaterUsed",
name="Current Water Used",
state_class=SensorStateClass.MEASUREMENT,
2023-04-10 19:51:16 +02:00
icon="mdi:water",
2023-02-19 02:58:21 +01:00
),
2023-03-03 18:23:30 +01:00
SensorEntityDescription(
key="startProgram.weight",
2023-03-05 19:19:52 +01:00
name="Suggested weight",
2023-03-03 18:23:30 +01:00
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.CONFIG,
native_unit_of_measurement=UnitOfMass.KILOGRAMS,
2023-04-10 19:51:16 +02:00
icon="mdi:weight-kilogram",
2023-03-03 18:23:30 +01:00
),
2023-03-05 00:54:57 +01:00
SensorEntityDescription(
key="machMode",
2023-03-08 23:00:55 +01:00
name="Machine Status",
2023-03-05 19:19:52 +01:00
icon="mdi:information",
2023-04-22 02:47:55 +02:00
translation_key="washing_modes",
2023-03-05 00:54:57 +01:00
),
SensorEntityDescription(
2023-04-10 19:51:16 +02:00
key="errors", name="Error", icon="mdi:math-log", translation_key="errors"
2023-03-05 00:54:57 +01:00
),
2023-03-05 21:16:09 +01:00
SensorEntityDescription(
key="remainingTimeMM",
name="Remaining Time",
icon="mdi:timer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTime.MINUTES,
),
SensorEntityDescription(
key="spinSpeed",
name="Spin Speed",
2023-03-22 22:57:14 +01:00
icon="mdi:speedometer",
2023-03-05 21:16:09 +01:00
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTime.MINUTES,
),
2023-03-21 12:59:39 +01:00
),
"TD": (
SensorEntityDescription(
key="machMode",
name="Machine Status",
icon="mdi:information",
2023-04-22 02:47:55 +02:00
translation_key="washing_modes",
2023-03-21 12:59:39 +01:00
),
SensorEntityDescription(
2023-04-10 19:51:16 +02:00
key="errors", name="Error", icon="mdi:math-log", translation_key="errors"
2023-03-21 12:59:39 +01:00
),
SensorEntityDescription(
key="remainingTimeMM",
name="Remaining Time",
icon="mdi:timer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTime.MINUTES,
),
SensorEntityDescription(
key="delayTime",
name="Start Time",
icon="mdi:clock-start",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTime.MINUTES,
),
SensorEntityDescription(
key="prCode",
name="Program",
icon="mdi:tumble-dryer",
2023-04-10 19:51:16 +02:00
translation_key="tumbledryerprogram",
2023-03-21 12:59:39 +01:00
),
SensorEntityDescription(
key="prPhase",
name="Program Phase",
2023-04-22 02:47:55 +02:00
icon="mdi:washing-machine",
translation_key="program_phases_td",
2023-03-21 12:59:39 +01:00
),
SensorEntityDescription(
key="dryLevel",
name="Dry level",
icon="mdi:hair-dryer",
2023-04-22 23:09:57 +02:00
translation_key="dry_levels",
2023-03-21 12:59:39 +01:00
),
SensorEntityDescription(
key="tempLevel",
name="Temperature level",
icon="mdi:thermometer",
2023-04-10 19:51:16 +02:00
translation_key="tumbledryertemplevel",
2023-03-21 12:59:39 +01:00
),
2023-04-07 13:52:55 +02:00
),
2023-04-08 04:44:47 +02:00
"WD": (
SensorEntityDescription(
key="totalElectricityUsed",
name="Total Power",
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
),
SensorEntityDescription(
key="totalWaterUsed",
name="Total Water",
device_class=SensorDeviceClass.WATER,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfVolume.LITERS,
),
SensorEntityDescription(
key="totalWashCycle",
name="Total Wash Cycle",
state_class=SensorStateClass.TOTAL_INCREASING,
icon="mdi:counter",
),
SensorEntityDescription(
key="currentElectricityUsed",
name="Current Electricity Used",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.POWER,
native_unit_of_measurement=UnitOfPower.KILO_WATT,
icon="mdi:lightning-bolt",
),
SensorEntityDescription(
key="currentWaterUsed",
name="Current Water Used",
state_class=SensorStateClass.MEASUREMENT,
icon="mdi:water",
),
SensorEntityDescription(
key="startProgram.weight",
name="Suggested weight",
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.CONFIG,
native_unit_of_measurement=UnitOfMass.KILOGRAMS,
icon="mdi:weight-kilogram",
),
2023-04-08 04:44:47 +02:00
SensorEntityDescription(
key="machMode",
name="Machine Status",
icon="mdi:information",
2023-04-22 02:47:55 +02:00
translation_key="washing_modes",
2023-04-08 04:44:47 +02:00
),
SensorEntityDescription(
key="spinSpeed",
name="Spin Speed",
icon="mdi:fast-forward-outline",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=REVOLUTIONS_PER_MINUTE,
),
SensorEntityDescription(
key="remainingTimeMM",
name="Remaining Time",
icon="mdi:timer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTime.MINUTES,
),
SensorEntityDescription(
key="prCode",
name="Current Program",
icon="mdi:tumble-dryer",
),
SensorEntityDescription(
key="prPhase",
name="Program Phase",
2023-04-22 02:47:55 +02:00
icon="mdi:washing-machine",
translation_key="program_phases_wm",
2023-04-08 04:44:47 +02:00
),
SensorEntityDescription(
key="dryLevel",
name="Dry level",
icon="mdi:hair-dryer",
),
SensorEntityDescription(
key="dirtyLevel",
name="Dirt level",
icon="mdi:liquid-spot",
),
SensorEntityDescription(
key="steamLevel",
name="Steam level",
icon="mdi:smoke",
),
SensorEntityDescription(
key="temp",
name="Current Temperature",
icon="mdi:thermometer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
),
2023-04-07 13:52:55 +02:00
"OV": (
SensorEntityDescription(
key="remainingTimeMM",
name="Remaining Time",
icon="mdi:timer",
native_unit_of_measurement=UnitOfTime.MINUTES,
),
SensorEntityDescription(
key="delayTime",
name="Start Time",
icon="mdi:clock-start",
),
SensorEntityDescription(
key="temp",
name="Temperature",
icon="mdi:thermometer",
),
SensorEntityDescription(
key="tempSel",
name="Temperature Selected",
icon="mdi:thermometer",
),
2023-04-08 04:44:47 +02:00
),
2023-04-16 13:55:08 +02:00
"IH": (
2023-04-15 04:27:40 +02:00
SensorEntityDescription(
key="remainingTimeMM",
name="Remaining Time",
icon="mdi:timer",
native_unit_of_measurement=UnitOfTime.MINUTES,
),
SensorEntityDescription(
key="temp",
name="Temperature",
icon="mdi:thermometer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
SensorEntityDescription(key="errors", name="Error", icon="mdi:math-log"),
2023-04-18 23:44:23 +02:00
SensorEntityDescription(
key="power",
name="Power",
icon="mdi:lightning-bolt",
state_class=SensorStateClass.MEASUREMENT,
),
2023-04-15 04:27:40 +02:00
),
2023-04-16 21:46:17 +02:00
"DW": (
SensorEntityDescription(
key="startProgram.ecoIndex",
name="Eco Index",
icon="mdi:sprout",
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.CONFIG,
),
SensorEntityDescription(
key="startProgram.waterEfficiency",
name="Water Efficiency",
icon="mdi:water",
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.CONFIG,
),
SensorEntityDescription(
key="startProgram.waterSaving",
name="Water Saving",
icon="mdi:water-percent",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=PERCENTAGE,
entity_category=EntityCategory.CONFIG,
),
SensorEntityDescription(
key="startProgram.temp",
name="Temperature",
icon="mdi:thermometer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
entity_category=EntityCategory.CONFIG,
),
SensorEntityDescription(
key="startProgram.energyLabel",
name="Energy Label",
icon="mdi:lightning-bolt-circle",
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.CONFIG,
),
SensorEntityDescription(
key="startProgram.remainingTime",
name="Time",
icon="mdi:timer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTime.MINUTES,
entity_category=EntityCategory.CONFIG,
),
SensorEntityDescription(
key="machMode",
name="Machine Status",
icon="mdi:information",
2023-04-22 02:47:55 +02:00
translation_key="washing_modes",
2023-04-16 21:46:17 +02:00
),
SensorEntityDescription(
key="errors", name="Error", icon="mdi:math-log", translation_key="errors"
),
SensorEntityDescription(
key="remainingTimeMM",
name="Remaining Time",
icon="mdi:timer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTime.MINUTES,
),
2023-04-22 02:47:55 +02:00
SensorEntityDescription(
key="prPhase",
name="Program Phase",
icon="mdi:washing-machine",
translation_key="program_phases_dw",
),
2023-04-16 21:46:17 +02:00
),
2023-02-19 02:58:21 +01:00
}
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
2023-04-10 07:09:54 +02:00
hon: Hon = hass.data[DOMAIN][entry.unique_id]
2023-02-19 02:58:21 +01:00
coordinators = hass.data[DOMAIN]["coordinators"]
appliances = []
2023-04-10 07:09:54 +02:00
for device in hon.appliances:
if device.unique_id in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id]
2023-02-19 02:58:21 +01:00
else:
coordinator = HonCoordinator(hass, device)
hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator
2023-02-19 02:58:21 +01:00
await coordinator.async_config_entry_first_refresh()
2023-03-08 23:00:55 +01:00
if descriptions := SENSORS.get(device.appliance_type):
2023-02-19 02:58:21 +01:00
for description in descriptions:
2023-03-08 23:00:55 +01:00
if not device.get(description.key):
2023-04-10 19:51:16 +02:00
_LOGGER.warning(
"[%s] Can't setup %s", device.appliance_type, description.key
)
2023-03-05 19:19:52 +01:00
continue
2023-04-10 19:51:16 +02:00
appliances.extend(
[HonSensorEntity(hass, coordinator, entry, device, description)]
2023-02-19 02:58:21 +01:00
)
async_add_entities(appliances)
class HonSensorEntity(HonEntity, SensorEntity):
def __init__(self, hass, coordinator, entry, device, description) -> None:
super().__init__(hass, entry, coordinator, device)
self._coordinator = coordinator
self.entity_description = description
self._attr_unique_id = f"{super().unique_id}{description.key}"
@property
def native_value(self) -> StateType:
2023-03-08 23:00:55 +01:00
return self._device.get(self.entity_description.key, "")
2023-02-19 02:58:21 +01:00
@callback
def _handle_coordinator_update(self):
2023-03-08 23:00:55 +01:00
self._attr_native_value = self._device.get(self.entity_description.key, "")
2023-02-19 02:58:21 +01:00
self.async_write_ha_state()