Refactor screenlogic API data selection (#49682)

This commit is contained in:
Kevin Worrel 2021-04-25 16:17:42 -07:00 committed by GitHub
parent 73b7a68e97
commit 6f1273cf1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 78 deletions

View file

@ -1,7 +1,7 @@
"""Support for a ScreenLogic Sensor."""
import logging
from screenlogicpy.const import DEVICE_TYPE
from screenlogicpy.const import DATA as SL_DATA, DEVICE_TYPE
from homeassistant.components.sensor import (
DEVICE_CLASS_POWER,
@ -25,21 +25,29 @@ SL_DEVICE_TYPE_TO_HA_DEVICE_CLASS = {
async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up entry."""
entities = []
data = hass.data[DOMAIN][config_entry.entry_id]
coordinator = data["coordinator"]
coordinator = hass.data[DOMAIN][config_entry.entry_id]["coordinator"]
# Generic sensors
for sensor in data["devices"]["sensor"]:
entities.append(ScreenLogicSensor(coordinator, sensor))
for sensor in coordinator.data[SL_DATA.KEY_SENSORS]:
if sensor == "chem_alarm":
continue
if coordinator.data[SL_DATA.KEY_SENSORS][sensor]["value"] != 0:
entities.append(ScreenLogicSensor(coordinator, sensor))
# Pump sensors
for pump in data["devices"]["pump"]:
for pump_key in PUMP_SENSORS:
entities.append(ScreenLogicPumpSensor(coordinator, pump, pump_key))
for pump in coordinator.data[SL_DATA.KEY_PUMPS]:
if (
coordinator.data[SL_DATA.KEY_PUMPS][pump]["data"] != 0
and "currentWatts" in coordinator.data[SL_DATA.KEY_PUMPS][pump]
):
for pump_key in PUMP_SENSORS:
entities.append(ScreenLogicPumpSensor(coordinator, pump, pump_key))
async_add_entities(entities)
class ScreenLogicSensor(ScreenlogicEntity, SensorEntity):
"""Representation of a ScreenLogic sensor entity."""
"""Representation of the basic ScreenLogic sensor entity."""
@property
def name(self):
@ -54,8 +62,8 @@ class ScreenLogicSensor(ScreenlogicEntity, SensorEntity):
@property
def device_class(self):
"""Device class of the sensor."""
device_class = self.sensor.get("device_type")
return SL_DEVICE_TYPE_TO_HA_DEVICE_CLASS.get(device_class)
device_type = self.sensor.get("device_type")
return SL_DEVICE_TYPE_TO_HA_DEVICE_CLASS.get(device_type)
@property
def state(self):
@ -66,10 +74,10 @@ class ScreenLogicSensor(ScreenlogicEntity, SensorEntity):
@property
def sensor(self):
"""Shortcut to access the sensor data."""
return self.coordinator.data["sensors"][self._data_key]
return self.coordinator.data[SL_DATA.KEY_SENSORS][self._data_key]
class ScreenLogicPumpSensor(ScreenlogicEntity, SensorEntity):
class ScreenLogicPumpSensor(ScreenLogicSensor):
"""Representation of a ScreenLogic pump sensor entity."""
def __init__(self, coordinator, pump, key):
@ -79,27 +87,6 @@ class ScreenLogicPumpSensor(ScreenlogicEntity, SensorEntity):
self._key = key
@property
def name(self):
"""Return the pump sensor name."""
return f"{self.gateway_name} {self.pump_sensor['name']}"
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return self.pump_sensor.get("unit")
@property
def device_class(self):
"""Return the device class."""
device_class = self.pump_sensor.get("device_type")
return SL_DEVICE_TYPE_TO_HA_DEVICE_CLASS.get(device_class)
@property
def state(self):
"""State of the pump sensor."""
return self.pump_sensor["value"]
@property
def pump_sensor(self):
def sensor(self):
"""Shortcut to access the pump sensor data."""
return self.coordinator.data["pumps"][self._pump_id][self._key]
return self.coordinator.data[SL_DATA.KEY_PUMPS][self._pump_id][self._key]