Small cleanup in Plugwise sensors (#65765)

This commit is contained in:
Franck Nijhof 2022-02-05 14:20:07 +01:00 committed by GitHub
parent fa09cf663e
commit 9f8c0685e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,10 @@
"""Plugwise Sensor component for Home Assistant.""" """Plugwise Sensor component for Home Assistant."""
from __future__ import annotations
import logging import logging
from plugwise.smile import Smile
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass, SensorDeviceClass,
SensorEntity, SensorEntity,
@ -18,6 +22,7 @@ from homeassistant.const import (
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import ( from .const import (
COOL_ICON, COOL_ICON,
@ -298,18 +303,17 @@ async def async_setup_entry(
class SmileSensor(SmileGateway, SensorEntity): class SmileSensor(SmileGateway, SensorEntity):
"""Represent Smile Sensors.""" """Represent Smile Sensors."""
def __init__(self, api, coordinator, name, dev_id, sensor): def __init__(
self,
api: Smile,
coordinator: DataUpdateCoordinator,
name: str,
dev_id: str,
sensor: str,
) -> None:
"""Initialise the sensor.""" """Initialise the sensor."""
super().__init__(api, coordinator, name, dev_id) super().__init__(api, coordinator, name, dev_id)
self._sensor = sensor self._sensor = sensor
self._dev_class = None
self._icon = None
self._state = None
self._state_class = None
self._unit_of_measurement = None
if dev_id == self._api.heater_id: if dev_id == self._api.heater_id:
self._entity_name = "Auxiliary" self._entity_name = "Auxiliary"
@ -321,47 +325,29 @@ class SmileSensor(SmileGateway, SensorEntity):
self._unique_id = f"{dev_id}-{sensor}" self._unique_id = f"{dev_id}-{sensor}"
@property
def device_class(self):
"""Device class of this entity."""
return self._dev_class
@property
def icon(self):
"""Return the icon of this entity."""
return self._icon
@property
def native_value(self):
"""Return the state of this entity."""
return self._state
@property
def native_unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement
@property
def state_class(self):
"""Return the state_class of this entity."""
return self._state_class
class PwThermostatSensor(SmileSensor): class PwThermostatSensor(SmileSensor):
"""Thermostat (or generic) sensor devices.""" """Thermostat (or generic) sensor devices."""
def __init__(self, api, coordinator, name, dev_id, sensor, sensor_type): def __init__(
self,
api: Smile,
coordinator: DataUpdateCoordinator,
name: str,
dev_id: str,
sensor: str,
sensor_type: list[str],
) -> None:
"""Set up the Plugwise API.""" """Set up the Plugwise API."""
super().__init__(api, coordinator, name, dev_id, sensor) super().__init__(api, coordinator, name, dev_id, sensor)
self._icon = None
self._model = sensor_type[SENSOR_MAP_MODEL] self._model = sensor_type[SENSOR_MAP_MODEL]
self._unit_of_measurement = sensor_type[SENSOR_MAP_UOM] self._attr_native_unit_of_measurement = sensor_type[SENSOR_MAP_UOM]
self._dev_class = sensor_type[SENSOR_MAP_DEVICE_CLASS] self._attr_device_class = sensor_type[SENSOR_MAP_DEVICE_CLASS]
self._state_class = sensor_type[SENSOR_MAP_STATE_CLASS] self._attr_state_class = sensor_type[SENSOR_MAP_STATE_CLASS]
@callback @callback
def _async_process_data(self): def _async_process_data(self) -> None:
"""Update the entity.""" """Update the entity."""
if not (data := self._api.get_device_data(self._dev_id)): if not (data := self._api.get_device_data(self._dev_id)):
_LOGGER.error("Received no data for device %s", self._entity_name) _LOGGER.error("Received no data for device %s", self._entity_name)
@ -369,8 +355,8 @@ class PwThermostatSensor(SmileSensor):
return return
if data.get(self._sensor) is not None: if data.get(self._sensor) is not None:
self._state = data[self._sensor] self._attr_native_value = data[self._sensor]
self._icon = CUSTOM_ICONS.get(self._sensor, self._icon) self._attr_icon = CUSTOM_ICONS.get(self._sensor, self.icon)
self.async_write_ha_state() self.async_write_ha_state()
@ -378,7 +364,14 @@ class PwThermostatSensor(SmileSensor):
class PwAuxDeviceSensor(SmileSensor): class PwAuxDeviceSensor(SmileSensor):
"""Auxiliary Device Sensors.""" """Auxiliary Device Sensors."""
def __init__(self, api, coordinator, name, dev_id, sensor): def __init__(
self,
api: Smile,
coordinator: DataUpdateCoordinator,
name: str,
dev_id: str,
sensor: str,
) -> None:
"""Set up the Plugwise API.""" """Set up the Plugwise API."""
super().__init__(api, coordinator, name, dev_id, sensor) super().__init__(api, coordinator, name, dev_id, sensor)
@ -386,7 +379,7 @@ class PwAuxDeviceSensor(SmileSensor):
self._heating_state = False self._heating_state = False
@callback @callback
def _async_process_data(self): def _async_process_data(self) -> None:
"""Update the entity.""" """Update the entity."""
if not (data := self._api.get_device_data(self._dev_id)): if not (data := self._api.get_device_data(self._dev_id)):
_LOGGER.error("Received no data for device %s", self._entity_name) _LOGGER.error("Received no data for device %s", self._entity_name)
@ -398,14 +391,14 @@ class PwAuxDeviceSensor(SmileSensor):
if data.get("cooling_state") is not None: if data.get("cooling_state") is not None:
self._cooling_state = data["cooling_state"] self._cooling_state = data["cooling_state"]
self._state = "idle" self._attr_native_value = "idle"
self._icon = IDLE_ICON self._attr_icon = IDLE_ICON
if self._heating_state: if self._heating_state:
self._state = "heating" self._attr_native_value = "heating"
self._icon = FLAME_ICON self._attr_icon = FLAME_ICON
if self._cooling_state: if self._cooling_state:
self._state = "cooling" self._attr_native_value = "cooling"
self._icon = COOL_ICON self._attr_icon = COOL_ICON
self.async_write_ha_state() self.async_write_ha_state()
@ -413,24 +406,32 @@ class PwAuxDeviceSensor(SmileSensor):
class PwPowerSensor(SmileSensor): class PwPowerSensor(SmileSensor):
"""Power sensor entities.""" """Power sensor entities."""
def __init__(self, api, coordinator, name, dev_id, sensor, sensor_type, model): def __init__(
self,
api: Smile,
coordinator: DataUpdateCoordinator,
name: str,
dev_id: str,
sensor: str,
sensor_type: list[str],
model: str | None,
) -> None:
"""Set up the Plugwise API.""" """Set up the Plugwise API."""
super().__init__(api, coordinator, name, dev_id, sensor) super().__init__(api, coordinator, name, dev_id, sensor)
self._icon = None
self._model = model self._model = model
if model is None: if model is None:
self._model = sensor_type[SENSOR_MAP_MODEL] self._model = sensor_type[SENSOR_MAP_MODEL]
self._unit_of_measurement = sensor_type[SENSOR_MAP_UOM] self._attr_native_unit_of_measurement = sensor_type[SENSOR_MAP_UOM]
self._dev_class = sensor_type[SENSOR_MAP_DEVICE_CLASS] self._attr_device_class = sensor_type[SENSOR_MAP_DEVICE_CLASS]
self._state_class = sensor_type[SENSOR_MAP_STATE_CLASS] self._attr_state_class = sensor_type[SENSOR_MAP_STATE_CLASS]
if dev_id == self._api.gateway_id: if dev_id == self._api.gateway_id:
self._model = "P1 DSMR" self._model = "P1 DSMR"
@callback @callback
def _async_process_data(self): def _async_process_data(self) -> None:
"""Update the entity.""" """Update the entity."""
if not (data := self._api.get_device_data(self._dev_id)): if not (data := self._api.get_device_data(self._dev_id)):
_LOGGER.error("Received no data for device %s", self._entity_name) _LOGGER.error("Received no data for device %s", self._entity_name)
@ -438,7 +439,7 @@ class PwPowerSensor(SmileSensor):
return return
if data.get(self._sensor) is not None: if data.get(self._sensor) is not None:
self._state = data[self._sensor] self._attr_native_value = data[self._sensor]
self._icon = CUSTOM_ICONS.get(self._sensor, self._icon) self._attr_icon = CUSTOM_ICONS.get(self._sensor, self.icon)
self.async_write_ha_state() self.async_write_ha_state()