1 """Platform for sensor integration."""
5 from laundrify_aio
import LaundrifyDevice
6 from laundrify_aio.exceptions
import LaundrifyDeviceException
20 from .const
import DOMAIN
21 from .coordinator
import LaundrifyUpdateCoordinator
23 _LOGGER = logging.getLogger(__name__)
27 hass: HomeAssistant, config: ConfigEntry, async_add_entities: AddEntitiesCallback
29 """Add power sensor for passed config_entry in HA."""
31 coordinator: LaundrifyUpdateCoordinator = hass.data[DOMAIN][config.entry_id][
35 sensor_entities: list[LaundrifyPowerSensor | LaundrifyEnergySensor] = []
36 for device
in coordinator.data.values():
44 """Base class for Laundrify sensors."""
46 _attr_has_entity_name =
True
48 def __init__(self, device: LaundrifyDevice) ->
None:
49 """Initialize the sensor."""
56 """Representation of a Power sensor."""
58 _attr_device_class = SensorDeviceClass.POWER
59 _attr_native_unit_of_measurement = UnitOfPower.WATT
60 _attr_state_class = SensorStateClass.MEASUREMENT
61 _attr_suggested_display_precision = 0
64 """Fetch latest power measurement from the device."""
66 power = await self.
_device_device.get_power()
67 except LaundrifyDeviceException
as err:
68 _LOGGER.debug(
"Couldn't load power for %s: %s", self.
_attr_unique_id_attr_unique_id, err)
71 _LOGGER.debug(
"Retrieved power for %s: %s", self.
_attr_unique_id_attr_unique_id, power)
78 CoordinatorEntity[LaundrifyUpdateCoordinator], LaundrifyBaseSensor
80 """Representation of an Energy sensor."""
82 _attr_device_class = SensorDeviceClass.ENERGY
83 _attr_native_unit_of_measurement = UnitOfEnergy.WATT_HOUR
84 _attr_state_class = SensorStateClass.TOTAL
85 _attr_suggested_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR
86 _attr_suggested_display_precision = 2
89 self, coordinator: LaundrifyUpdateCoordinator, device: LaundrifyDevice
91 """Initialize the sensor."""
92 CoordinatorEntity.__init__(self, coordinator)
93 LaundrifyBaseSensor.__init__(self, device)
97 """Return the total energy of the device."""
98 device = self.coordinator.data[self.
_device_device.id]
99 return float(device.totalEnergy)
None __init__(self, LaundrifyDevice device)
None __init__(self, LaundrifyUpdateCoordinator coordinator, LaundrifyDevice device)
None async_setup_entry(HomeAssistant hass, ConfigEntry config, AddEntitiesCallback async_add_entities)