1
mirror of https://github.com/home-assistant/core synced 2024-10-07 10:13:38 +02:00

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."""
from __future__ import annotations
import logging
from plugwise.smile import Smile
from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
@ -18,6 +22,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from .const import (
COOL_ICON,
@ -298,18 +303,17 @@ async def async_setup_entry(
class SmileSensor(SmileGateway, SensorEntity):
"""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."""
super().__init__(api, coordinator, name, dev_id)
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:
self._entity_name = "Auxiliary"
@ -321,47 +325,29 @@ class SmileSensor(SmileGateway, SensorEntity):
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):
"""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."""
super().__init__(api, coordinator, name, dev_id, sensor)
self._icon = None
self._model = sensor_type[SENSOR_MAP_MODEL]
self._unit_of_measurement = sensor_type[SENSOR_MAP_UOM]
self._dev_class = sensor_type[SENSOR_MAP_DEVICE_CLASS]
self._state_class = sensor_type[SENSOR_MAP_STATE_CLASS]
self._attr_native_unit_of_measurement = sensor_type[SENSOR_MAP_UOM]
self._attr_device_class = sensor_type[SENSOR_MAP_DEVICE_CLASS]
self._attr_state_class = sensor_type[SENSOR_MAP_STATE_CLASS]
@callback
def _async_process_data(self):
def _async_process_data(self) -> None:
"""Update the entity."""
if not (data := self._api.get_device_data(self._dev_id)):
_LOGGER.error("Received no data for device %s", self._entity_name)
@ -369,8 +355,8 @@ class PwThermostatSensor(SmileSensor):
return
if data.get(self._sensor) is not None:
self._state = data[self._sensor]
self._icon = CUSTOM_ICONS.get(self._sensor, self._icon)
self._attr_native_value = data[self._sensor]
self._attr_icon = CUSTOM_ICONS.get(self._sensor, self.icon)
self.async_write_ha_state()
@ -378,7 +364,14 @@ class PwThermostatSensor(SmileSensor):
class PwAuxDeviceSensor(SmileSensor):
"""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."""
super().__init__(api, coordinator, name, dev_id, sensor)
@ -386,7 +379,7 @@ class PwAuxDeviceSensor(SmileSensor):
self._heating_state = False
@callback
def _async_process_data(self):
def _async_process_data(self) -> None:
"""Update the entity."""
if not (data := self._api.get_device_data(self._dev_id)):
_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:
self._cooling_state = data["cooling_state"]
self._state = "idle"
self._icon = IDLE_ICON
self._attr_native_value = "idle"
self._attr_icon = IDLE_ICON
if self._heating_state:
self._state = "heating"
self._icon = FLAME_ICON
self._attr_native_value = "heating"
self._attr_icon = FLAME_ICON
if self._cooling_state:
self._state = "cooling"
self._icon = COOL_ICON
self._attr_native_value = "cooling"
self._attr_icon = COOL_ICON
self.async_write_ha_state()
@ -413,24 +406,32 @@ class PwAuxDeviceSensor(SmileSensor):
class PwPowerSensor(SmileSensor):
"""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."""
super().__init__(api, coordinator, name, dev_id, sensor)
self._icon = None
self._model = model
if model is None:
self._model = sensor_type[SENSOR_MAP_MODEL]
self._unit_of_measurement = sensor_type[SENSOR_MAP_UOM]
self._dev_class = sensor_type[SENSOR_MAP_DEVICE_CLASS]
self._state_class = sensor_type[SENSOR_MAP_STATE_CLASS]
self._attr_native_unit_of_measurement = sensor_type[SENSOR_MAP_UOM]
self._attr_device_class = sensor_type[SENSOR_MAP_DEVICE_CLASS]
self._attr_state_class = sensor_type[SENSOR_MAP_STATE_CLASS]
if dev_id == self._api.gateway_id:
self._model = "P1 DSMR"
@callback
def _async_process_data(self):
def _async_process_data(self) -> None:
"""Update the entity."""
if not (data := self._api.get_device_data(self._dev_id)):
_LOGGER.error("Received no data for device %s", self._entity_name)
@ -438,7 +439,7 @@ class PwPowerSensor(SmileSensor):
return
if data.get(self._sensor) is not None:
self._state = data[self._sensor]
self._icon = CUSTOM_ICONS.get(self._sensor, self._icon)
self._attr_native_value = data[self._sensor]
self._attr_icon = CUSTOM_ICONS.get(self._sensor, self.icon)
self.async_write_ha_state()