hOn/custom_components/hon/sensor.py
2023-05-28 00:30:40 +02:00

555 lines
20 KiB
Python

import logging
from dataclasses import dataclass
from homeassistant.components.sensor import (
SensorEntity,
SensorDeviceClass,
SensorStateClass,
SensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE
from homeassistant.const import (
REVOLUTIONS_PER_MINUTE,
UnitOfEnergy,
UnitOfVolume,
UnitOfMass,
UnitOfPower,
UnitOfTime,
UnitOfTemperature,
)
from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.typing import StateType
from . import const
from .const import DOMAIN
from .hon import HonEntity, unique_entities
_LOGGER = logging.getLogger(__name__)
@dataclass
class HonConfigSensorEntityDescription(SensorEntityDescription):
entity_category: EntityCategory = EntityCategory.CONFIG
@dataclass
class HonSensorEntityDescription(SensorEntityDescription):
pass
SENSORS: dict[str, tuple[SensorEntityDescription, ...]] = {
"WM": (
HonSensorEntityDescription(
key="prPhase",
name="Program Phase",
icon="mdi:washing-machine",
device_class=SensorDeviceClass.ENUM,
translation_key="program_phases_wm",
options=list(const.WASHING_PR_PHASE),
),
HonSensorEntityDescription(
key="totalElectricityUsed",
name="Total Power",
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
translation_key="energy_total",
),
HonSensorEntityDescription(
key="totalWaterUsed",
name="Total Water",
device_class=SensorDeviceClass.WATER,
state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=UnitOfVolume.LITERS,
translation_key="water_total",
),
HonSensorEntityDescription(
key="totalWashCycle",
name="Total Wash Cycle",
state_class=SensorStateClass.TOTAL_INCREASING,
icon="mdi:counter",
translation_key="cycles_total",
),
HonSensorEntityDescription(
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",
translation_key="energy_current",
),
HonSensorEntityDescription(
key="currentWaterUsed",
name="Current Water Used",
state_class=SensorStateClass.MEASUREMENT,
icon="mdi:water",
translation_key="water_current",
),
HonConfigSensorEntityDescription(
key="startProgram.weight",
name="Suggested weight",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfMass.KILOGRAMS,
icon="mdi:weight-kilogram",
translation_key="suggested_load",
),
HonSensorEntityDescription(
key="machMode",
name="Machine Status",
icon="mdi:information",
device_class=SensorDeviceClass.ENUM,
translation_key="washing_modes",
options=list(const.MACH_MODE),
),
HonSensorEntityDescription(
key="errors", name="Error", icon="mdi:math-log", translation_key="errors"
),
HonSensorEntityDescription(
key="remainingTimeMM",
name="Remaining Time",
icon="mdi:timer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTime.MINUTES,
translation_key="remaining_time",
),
HonSensorEntityDescription(
key="spinSpeed",
name="Spin Speed",
icon="mdi:speedometer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=REVOLUTIONS_PER_MINUTE,
translation_key="spin_speed",
),
HonConfigSensorEntityDescription(
key="startProgram.energyLabel",
name="Energy Label",
icon="mdi:lightning-bolt-circle",
state_class=SensorStateClass.MEASUREMENT,
translation_key="energy_label",
),
HonConfigSensorEntityDescription(
key="startProgram.liquidDetergentDose",
name="Liquid Detergent Dose",
icon="mdi:cup-water",
translation_key="det_liquid",
),
HonConfigSensorEntityDescription(
key="startProgram.powderDetergentDose",
name="Powder Detergent Dose",
icon="mdi:cup",
translation_key="det_dust",
),
HonConfigSensorEntityDescription(
key="startProgram.remainingTime",
name="Remaining Time",
icon="mdi:timer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTime.MINUTES,
translation_key="remaining_time",
),
HonSensorEntityDescription(
key="dirtyLevel",
name="Dirt level",
icon="mdi:liquid-spot",
translation_key="dirt_level",
),
HonConfigSensorEntityDescription(
key="startProgram.suggestedLoadW",
name="Suggested Load",
icon="mdi:weight-kilogram",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfMass.KILOGRAMS,
translation_key="suggested_load",
),
HonSensorEntityDescription(
key="temp",
name="Current Temperature",
icon="mdi:thermometer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
translation_key="temperature",
),
),
"TD": (
HonSensorEntityDescription(
key="machMode",
name="Machine Status",
icon="mdi:information",
device_class=SensorDeviceClass.ENUM,
translation_key="washing_modes",
options=list(const.MACH_MODE),
),
HonSensorEntityDescription(
key="errors", name="Error", icon="mdi:math-log", translation_key="errors"
),
HonSensorEntityDescription(
key="remainingTimeMM",
name="Remaining Time",
icon="mdi:timer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTime.MINUTES,
translation_key="remaining_time",
),
HonSensorEntityDescription(
key="delayTime",
name="Start Time",
icon="mdi:clock-start",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTime.MINUTES,
translation_key="delay_time",
),
HonSensorEntityDescription(
key="programName",
name="Program",
icon="mdi:tumble-dryer",
device_class=SensorDeviceClass.ENUM,
translation_key="programs_td",
options=const.PROGRAMS_TD,
),
HonSensorEntityDescription(
key="prPhase",
name="Program Phase",
icon="mdi:washing-machine",
device_class=SensorDeviceClass.ENUM,
translation_key="program_phases_td",
options=list(const.TUMBLE_DRYER_PR_PHASE),
),
HonSensorEntityDescription(
key="dryLevel",
name="Dry level",
icon="mdi:hair-dryer",
device_class=SensorDeviceClass.ENUM,
translation_key="dry_levels",
options=list(const.TUMBLE_DRYER_DRY_LEVEL),
),
HonSensorEntityDescription(
key="tempLevel",
name="Temperature level",
icon="mdi:thermometer",
translation_key="tumbledryertemplevel",
),
HonConfigSensorEntityDescription(
key="startProgram.suggestedLoadD",
name="Suggested Load",
icon="mdi:weight-kilogram",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfMass.KILOGRAMS,
translation_key="suggested_load",
),
HonConfigSensorEntityDescription(
key="startProgram.energyLabel",
name="Energy Label",
icon="mdi:lightning-bolt-circle",
state_class=SensorStateClass.MEASUREMENT,
translation_key="energy_label",
),
HonConfigSensorEntityDescription(
key="startProgram.steamLevel",
name="Steam level",
icon="mdi:smoke",
translation_key="steam_level",
),
HonSensorEntityDescription(
key="steamLevel",
name="Steam level",
icon="mdi:smoke",
translation_key="steam_level",
),
HonConfigSensorEntityDescription(
key="steamType",
name="Steam Type",
icon="mdi:weather-dust",
),
),
"OV": (
HonSensorEntityDescription(
key="remainingTimeMM",
name="Remaining Time",
icon="mdi:timer",
native_unit_of_measurement=UnitOfTime.MINUTES,
translation_key="remaining_time",
),
HonSensorEntityDescription(
key="delayTime",
name="Start Time",
icon="mdi:clock-start",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTime.MINUTES,
translation_key="delay_time",
),
HonSensorEntityDescription(
key="temp",
name="Temperature",
icon="mdi:thermometer",
translation_key="temperature",
),
HonSensorEntityDescription(
key="tempSel",
name="Temperature Selected",
icon="mdi:thermometer",
translation_key="target_temperature",
),
),
"IH": (
HonSensorEntityDescription(
key="remainingTimeMM",
name="Remaining Time",
icon="mdi:timer",
native_unit_of_measurement=UnitOfTime.MINUTES,
translation_key="remaining_time",
),
HonSensorEntityDescription(
key="temp",
name="Temperature",
icon="mdi:thermometer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
translation_key="temperature",
),
HonSensorEntityDescription(
key="errors", name="Error", icon="mdi:math-log", translation_key="errors"
),
HonSensorEntityDescription(
key="power",
name="Power",
icon="mdi:lightning-bolt",
state_class=SensorStateClass.MEASUREMENT,
translation_key="power",
),
),
"DW": (
HonConfigSensorEntityDescription(
key="startProgram.ecoIndex",
name="Eco Index",
icon="mdi:sprout",
state_class=SensorStateClass.MEASUREMENT,
),
HonConfigSensorEntityDescription(
key="startProgram.waterEfficiency",
name="Water Efficiency",
icon="mdi:water",
state_class=SensorStateClass.MEASUREMENT,
translation_key="water_efficiency",
),
HonConfigSensorEntityDescription(
key="startProgram.waterSaving",
name="Water Saving",
icon="mdi:water-percent",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=PERCENTAGE,
translation_key="water_saving",
),
HonConfigSensorEntityDescription(
key="startProgram.temp",
name="Temperature",
icon="mdi:thermometer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
translation_key="temperature",
),
HonConfigSensorEntityDescription(
key="startProgram.energyLabel",
name="Energy Label",
icon="mdi:lightning-bolt-circle",
state_class=SensorStateClass.MEASUREMENT,
translation_key="energy_label",
),
HonConfigSensorEntityDescription(
key="startProgram.remainingTime",
name="Time",
icon="mdi:timer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTime.MINUTES,
translation_key="duration",
),
HonSensorEntityDescription(
key="machMode",
name="Machine Status",
icon="mdi:information",
device_class=SensorDeviceClass.ENUM,
translation_key="washing_modes",
options=list(const.MACH_MODE),
),
HonSensorEntityDescription(
key="errors", name="Error", icon="mdi:math-log", translation_key="errors"
),
HonSensorEntityDescription(
key="remainingTimeMM",
name="Remaining Time",
icon="mdi:timer",
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=UnitOfTime.MINUTES,
translation_key="remaining_time",
),
HonSensorEntityDescription(
key="prPhase",
name="Program Phase",
icon="mdi:washing-machine",
device_class=SensorDeviceClass.ENUM,
translation_key="program_phases_dw",
options=list(const.DISHWASHER_PR_PHASE),
),
),
"AC": (
HonSensorEntityDescription(
key="tempAirOutdoor",
name="Air Temperature Outdoor",
icon="mdi:thermometer",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
HonSensorEntityDescription(
key="tempCoilerIndoor",
name="Coiler Temperature Indoor",
icon="mdi:thermometer",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
HonSensorEntityDescription(
key="tempCoilerOutdoor",
name="Coiler Temperature Outside",
icon="mdi:thermometer",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
HonSensorEntityDescription(
key="tempDefrostOutdoor",
name="Defrost Temperature Outdoor",
icon="mdi:thermometer",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
HonSensorEntityDescription(
key="tempInAirOutdoor",
name="In Air Temperature Outdoor",
icon="mdi:thermometer",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
HonSensorEntityDescription(
key="tempIndoor",
name="Indoor Temperature",
icon="mdi:thermometer",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
HonSensorEntityDescription(
key="tempOutdoor",
name="Outdoor Temperature",
icon="mdi:thermometer",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
HonSensorEntityDescription(
key="tempSel",
name="Selected Temperature",
icon="mdi:thermometer",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
),
),
"REF": (
HonSensorEntityDescription(
key="humidityEnv",
name="Room Humidity",
icon="mdi:water-percent",
device_class=SensorDeviceClass.HUMIDITY,
native_unit_of_measurement=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT,
translation_key="humidity",
),
HonSensorEntityDescription(
key="tempEnv",
name="Room Temperature",
icon="mdi:home-thermometer-outline",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
translation_key="room_temperature",
),
HonSensorEntityDescription(
key="tempZ1",
name="Temperature Fridge",
icon="mdi:thermometer",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
translation_key="fridge_temp",
),
HonSensorEntityDescription(
key="tempZ2",
name="Temperature Freezer",
icon="mdi:snowflake-thermometer",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
translation_key="freezer_temp",
),
HonSensorEntityDescription(
key="errors", name="Error", icon="mdi:math-log", translation_key="errors"
),
),
}
SENSORS["WD"] = unique_entities(SENSORS["WM"], SENSORS["TD"])
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
entities = []
for device in hass.data[DOMAIN][entry.unique_id].appliances:
for description in SENSORS.get(device.appliance_type, []):
if isinstance(description, HonSensorEntityDescription):
if not device.get(description.key):
continue
entity = HonSensorEntity(hass, entry, device, description)
elif isinstance(description, HonConfigSensorEntityDescription):
if description.key not in device.available_settings:
continue
entity = HonConfigSensorEntity(hass, entry, device, description)
else:
continue
await entity.coordinator.async_config_entry_first_refresh()
entities.append(entity)
async_add_entities(entities)
class HonSensorEntity(HonEntity, SensorEntity):
entity_description: HonSensorEntityDescription
@callback
def _handle_coordinator_update(self):
value = self._device.get(self.entity_description.key, "")
if not value and self.entity_description.state_class is not None:
self._attr_native_value = 0
self._attr_native_value = value
self.async_write_ha_state()
class HonConfigSensorEntity(HonEntity, SensorEntity):
entity_description: HonConfigSensorEntityDescription
@callback
def _handle_coordinator_update(self):
value = self._device.settings.get(self.entity_description.key, None)
if self.entity_description.state_class is not None:
if value and value.value:
print(value.value, type(value.value))
self._attr_native_value = (
float(value.value) if "." in str(value.value) else int(value.value)
)
else:
self._attr_native_value = 0
else:
self._attr_native_value = value.value
self.async_write_ha_state()