Refactor waterfurnace to use entity descriptions (#83824)

This commit is contained in:
epenet 2022-12-12 17:14:53 +01:00 committed by GitHub
parent 2d206e7e31
commit a99a92a2e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,73 +5,91 @@ from homeassistant.components.sensor import (
ENTITY_ID_FORMAT, ENTITY_ID_FORMAT,
SensorDeviceClass, SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription,
) )
from homeassistant.const import PERCENTAGE, POWER_WATT, TEMP_FAHRENHEIT from homeassistant.const import PERCENTAGE, UnitOfPower, UnitOfTemperature
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util import slugify from homeassistant.util import slugify
from . import DOMAIN as WF_DOMAIN, UPDATE_TOPIC from . import DOMAIN as WF_DOMAIN, UPDATE_TOPIC, WaterFurnaceData
class WFSensorConfig:
"""Water Furnace Sensor configuration."""
def __init__(
self,
friendly_name,
field,
icon="mdi:gauge",
unit_of_measurement=None,
device_class=None,
):
"""Initialize configuration."""
self.device_class = device_class
self.friendly_name = friendly_name
self.field = field
self.icon = icon
self.unit_of_measurement = unit_of_measurement
SENSORS = [ SENSORS = [
WFSensorConfig("Furnace Mode", "mode"), SensorEntityDescription(name="Furnace Mode", key="mode", icon="mdi:gauge"),
WFSensorConfig("Total Power", "totalunitpower", "mdi:flash", POWER_WATT), SensorEntityDescription(
WFSensorConfig( name="Total Power",
"Active Setpoint", key="totalunitpower",
"tstatactivesetpoint", native_unit_of_measurement=UnitOfPower.WATT,
None, device_class=SensorDeviceClass.POWER,
TEMP_FAHRENHEIT,
SensorDeviceClass.TEMPERATURE,
), ),
WFSensorConfig( SensorEntityDescription(
"Leaving Air", name="Active Setpoint",
"leavingairtemp", key="tstatactivesetpoint",
None, native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
TEMP_FAHRENHEIT, device_class=SensorDeviceClass.TEMPERATURE,
SensorDeviceClass.TEMPERATURE,
), ),
WFSensorConfig( SensorEntityDescription(
"Room Temp", name="Leaving Air",
"tstatroomtemp", key="leavingairtemp",
None, native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
TEMP_FAHRENHEIT, device_class=SensorDeviceClass.TEMPERATURE,
SensorDeviceClass.TEMPERATURE,
), ),
WFSensorConfig("Loop Temp", "enteringwatertemp", None, TEMP_FAHRENHEIT), SensorEntityDescription(
WFSensorConfig( name="Room Temp",
"Humidity Set Point", "tstathumidsetpoint", "mdi:water-percent", PERCENTAGE key="tstatroomtemp",
native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
device_class=SensorDeviceClass.TEMPERATURE,
), ),
WFSensorConfig( SensorEntityDescription(
"Humidity", "tstatrelativehumidity", "mdi:water-percent", PERCENTAGE name="Loop Temp",
key="enteringwatertemp",
native_unit_of_measurement=UnitOfTemperature.FAHRENHEIT,
device_class=SensorDeviceClass.TEMPERATURE,
),
SensorEntityDescription(
name="Humidity Set Point",
key="tstathumidsetpoint",
icon="mdi:water-percent",
native_unit_of_measurement=PERCENTAGE,
),
SensorEntityDescription(
name="Humidity",
key="tstatrelativehumidity",
icon="mdi:water-percent",
native_unit_of_measurement=PERCENTAGE,
),
SensorEntityDescription(
name="Compressor Power",
key="compressorpower",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
),
SensorEntityDescription(
name="Fan Power",
key="fanpower",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
),
SensorEntityDescription(
name="Aux Power",
key="auxpower",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
),
SensorEntityDescription(
name="Loop Pump Power",
key="looppumppower",
native_unit_of_measurement=UnitOfPower.WATT,
device_class=SensorDeviceClass.POWER,
),
SensorEntityDescription(
name="Compressor Speed", key="actualcompressorspeed", icon="mdi:speedometer"
),
SensorEntityDescription(
name="Fan Speed", key="airflowcurrentspeed", icon="mdi:fan"
), ),
WFSensorConfig("Compressor Power", "compressorpower", "mdi:flash", POWER_WATT),
WFSensorConfig("Fan Power", "fanpower", "mdi:flash", POWER_WATT),
WFSensorConfig("Aux Power", "auxpower", "mdi:flash", POWER_WATT),
WFSensorConfig("Loop Pump Power", "looppumppower", "mdi:flash", POWER_WATT),
WFSensorConfig("Compressor Speed", "actualcompressorspeed", "mdi:speedometer"),
WFSensorConfig("Fan Speed", "airflowcurrentspeed", "mdi:fan"),
] ]
@ -87,8 +105,8 @@ def setup_platform(
sensors = [] sensors = []
client = hass.data[WF_DOMAIN] client = hass.data[WF_DOMAIN]
for sconfig in SENSORS: for description in SENSORS:
sensors.append(WaterFurnaceSensor(client, sconfig)) sensors.append(WaterFurnaceSensor(client, description))
add_entities(sensors) add_entities(sensors)
@ -98,41 +116,18 @@ class WaterFurnaceSensor(SensorEntity):
_attr_should_poll = False _attr_should_poll = False
def __init__(self, client, config): def __init__(
self, client: WaterFurnaceData, description: SensorEntityDescription
) -> None:
"""Initialize the sensor.""" """Initialize the sensor."""
self.client = client self.client = client
self._name = config.friendly_name self.entity_description = description
self._attr = config.field
self._state = None
self._icon = config.icon
self._unit_of_measurement = config.unit_of_measurement
self._attr_device_class = config.device_class
# This ensures that the sensors are isolated per waterfurnace unit # This ensures that the sensors are isolated per waterfurnace unit
self.entity_id = ENTITY_ID_FORMAT.format( self.entity_id = ENTITY_ID_FORMAT.format(
f"wf_{slugify(self.client.unit)}_{slugify(self._attr)}" f"wf_{slugify(self.client.unit)}_{slugify(description.key)}"
) )
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def native_value(self):
"""Return the state of the sensor."""
return self._state
@property
def icon(self):
"""Return icon."""
return self._icon
@property
def native_unit_of_measurement(self):
"""Return the units of measurement."""
return self._unit_of_measurement
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Register callbacks.""" """Register callbacks."""
self.async_on_remove( self.async_on_remove(
@ -145,5 +140,7 @@ class WaterFurnaceSensor(SensorEntity):
def async_update_callback(self): def async_update_callback(self):
"""Update state.""" """Update state."""
if self.client.data is not None: if self.client.data is not None:
self._state = getattr(self.client.data, self._attr, None) self._attr_native_value = getattr(
self.client.data, self.entity_description.key, None
)
self.async_write_ha_state() self.async_write_ha_state()