Use EntityDescription - econet (#55680)

* Use EntityDescription - econet

* Resolve name constants
pull/55696/head
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
1 changed files with 38 additions and 41 deletions

View File

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