Use EntityDescription - econet (#55680)

* Use EntityDescription - econet

* Resolve name constants
This commit is contained in:
Marc Mueller 2021-09-03 22:34:51 +02:00 committed by GitHub
parent 798f487ea4
commit 76ce0f6ea7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,6 @@
"""Support for Rheem EcoNet water heaters.""" """Support for Rheem EcoNet water heaters."""
from __future__ import annotations
from pyeconet.equipment import EquipmentType from pyeconet.equipment import EquipmentType
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
@ -7,77 +9,72 @@ from homeassistant.components.binary_sensor import (
DEVICE_CLASS_POWER, DEVICE_CLASS_POWER,
DEVICE_CLASS_SOUND, DEVICE_CLASS_SOUND,
BinarySensorEntity, BinarySensorEntity,
BinarySensorEntityDescription,
) )
from . import EcoNetEntity from . import EcoNetEntity
from .const import DOMAIN, EQUIPMENT from .const import DOMAIN, EQUIPMENT
SENSOR_NAME_RUNNING = "running" BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
SENSOR_NAME_SHUTOFF_VALVE = "shutoff_valve" BinarySensorEntityDescription(
SENSOR_NAME_RUNNING = "running" key="shutoff_valve_open",
SENSOR_NAME_SCREEN_LOCKED = "screen_locked" name="shutoff_valve",
SENSOR_NAME_BEEP_ENABLED = "beep_enabled" device_class=DEVICE_CLASS_OPENING,
),
ATTR = "attr" BinarySensorEntityDescription(
DEVICE_CLASS = "device_class" key="running",
SENSORS = { name="running",
SENSOR_NAME_SHUTOFF_VALVE: { device_class=DEVICE_CLASS_POWER,
ATTR: "shutoff_valve_open", ),
DEVICE_CLASS: DEVICE_CLASS_OPENING, BinarySensorEntityDescription(
}, key="screen_locked",
SENSOR_NAME_RUNNING: {ATTR: "running", DEVICE_CLASS: DEVICE_CLASS_POWER}, name="screen_locked",
SENSOR_NAME_SCREEN_LOCKED: { device_class=DEVICE_CLASS_LOCK,
ATTR: "screen_locked", ),
DEVICE_CLASS: DEVICE_CLASS_LOCK, BinarySensorEntityDescription(
}, key="beep_enabled",
SENSOR_NAME_BEEP_ENABLED: { name="beep_enabled",
ATTR: "beep_enabled", device_class=DEVICE_CLASS_SOUND,
DEVICE_CLASS: DEVICE_CLASS_SOUND, ),
}, )
}
async def async_setup_entry(hass, entry, async_add_entities): async def async_setup_entry(hass, entry, async_add_entities):
"""Set up EcoNet binary sensor based on a config entry.""" """Set up EcoNet binary sensor based on a config entry."""
equipment = hass.data[DOMAIN][EQUIPMENT][entry.entry_id] equipment = hass.data[DOMAIN][EQUIPMENT][entry.entry_id]
binary_sensors = []
all_equipment = equipment[EquipmentType.WATER_HEATER].copy() all_equipment = equipment[EquipmentType.WATER_HEATER].copy()
all_equipment.extend(equipment[EquipmentType.THERMOSTAT].copy()) all_equipment.extend(equipment[EquipmentType.THERMOSTAT].copy())
for _equip in all_equipment:
for sensor_name, sensor in SENSORS.items():
if getattr(_equip, sensor[ATTR], None) is not None:
binary_sensors.append(EcoNetBinarySensor(_equip, sensor_name))
async_add_entities(binary_sensors) entities = [
EcoNetBinarySensor(_equip, description)
for _equip in all_equipment
for description in BINARY_SENSOR_TYPES
if getattr(_equip, description.key, None) is not None
]
async_add_entities(entities)
class EcoNetBinarySensor(EcoNetEntity, BinarySensorEntity): class EcoNetBinarySensor(EcoNetEntity, BinarySensorEntity):
"""Define a Econet binary sensor.""" """Define a Econet binary sensor."""
def __init__(self, econet_device, device_name): def __init__(self, econet_device, description: BinarySensorEntityDescription):
"""Initialize.""" """Initialize."""
super().__init__(econet_device) super().__init__(econet_device)
self.entity_description = description
self._econet = econet_device self._econet = econet_device
self._device_name = device_name
@property @property
def is_on(self): def is_on(self):
"""Return true if the binary sensor is on.""" """Return true if the binary sensor is on."""
return getattr(self._econet, SENSORS[self._device_name][ATTR]) return getattr(self._econet, self.entity_description.key)
@property
def device_class(self):
"""Return the class of this sensor, from DEVICE_CLASSES."""
return SENSORS[self._device_name][DEVICE_CLASS]
@property @property
def name(self): def name(self):
"""Return the name of the entity.""" """Return the name of the entity."""
return f"{self._econet.device_name}_{self._device_name}" return f"{self._econet.device_name}_{self.entity_description.name}"
@property @property
def unique_id(self): def unique_id(self):
"""Return the unique ID of the entity.""" """Return the unique ID of the entity."""
return ( return f"{self._econet.device_id}_{self._econet.device_name}_{self.entity_description.name}"
f"{self._econet.device_id}_{self._econet.device_name}_{self._device_name}"
)