hOn/custom_components/hOn/sensor.py

123 lines
4.2 KiB
Python
Raw Normal View History

2023-02-19 02:58:21 +01:00
import logging
from pyhon import HonConnection
from homeassistant.components.sensor import (
SensorEntity,
SensorDeviceClass,
SensorStateClass,
SensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
2023-03-05 00:54:57 +01:00
from homeassistant.const import UnitOfEnergy, UnitOfVolume, UnitOfMass, UnitOfPower
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-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": (
SensorEntityDescription(
key="totalElectricityUsed",
name="Total Power",
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.TOTAL_INCREASING,
2023-03-03 18:23:30 +01: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-03-03 18:23:30 +01: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,
icon="mdi:counter"
),
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-02-19 02:58:21 +01:00
icon="mdi:lightning-bolt"
),
SensorEntityDescription(
key="currentWaterUsed",
name="Current Water Used",
state_class=SensorStateClass.MEASUREMENT,
icon="mdi:water"
),
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,
icon="mdi:weight-kilogram"
),
2023-03-05 00:54:57 +01:00
SensorEntityDescription(
key="machMode",
2023-03-05 19:19:52 +01:00
name="Machine Last Status",
icon="mdi:information",
2023-03-05 00:54:57 +01:00
translation_key="mode"
),
SensorEntityDescription(
key="errors",
name="Last Error",
2023-03-05 19:19:52 +01:00
icon="mdi:math-log",
2023-03-05 00:54:57 +01:00
translation_key="errors"
),
2023-03-05 19:19:52 +01:00
2023-02-19 02:58:21 +01:00
)
}
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
hon: HonConnection = hass.data[DOMAIN][entry.unique_id]
coordinators = hass.data[DOMAIN]["coordinators"]
appliances = []
for device in hon.devices:
if device.mac_address in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][device.mac_address]
else:
coordinator = HonCoordinator(hass, device)
hass.data[DOMAIN]["coordinators"][device.mac_address] = coordinator
await coordinator.async_config_entry_first_refresh()
if descriptions := SENSORS.get(device.appliance_type_name):
for description in descriptions:
2023-03-05 19:19:52 +01:00
if not device.data.get(description.key):
continue
2023-02-19 02:58:21 +01:00
appliances.extend([
HonSensorEntity(hass, coordinator, entry, device, description)]
)
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-03 18:23:30 +01:00
return self._device.data.get(self.entity_description.key, "")
2023-02-19 02:58:21 +01:00
@callback
def _handle_coordinator_update(self):
2023-03-03 18:23:30 +01:00
self._attr_native_value = self._device.data.get(self.entity_description.key, "")
2023-02-19 02:58:21 +01:00
self.async_write_ha_state()