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,6 +1,5 @@
"""The Screenlogic integration."""
import asyncio
from collections import defaultdict
from datetime import timedelta
import logging
@ -73,31 +72,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
await coordinator.async_config_entry_first_refresh()
device_data = defaultdict(list)
for circuit in coordinator.data["circuits"]:
device_data["switch"].append(circuit)
for sensor in coordinator.data["sensors"]:
if sensor == "chem_alarm":
device_data["binary_sensor"].append(sensor)
else:
if coordinator.data["sensors"][sensor]["value"] != 0:
device_data["sensor"].append(sensor)
for pump in coordinator.data["pumps"]:
if (
coordinator.data["pumps"][pump]["data"] != 0
and "currentWatts" in coordinator.data["pumps"][pump]
):
device_data["pump"].append(pump)
for body in coordinator.data["bodies"]:
device_data["body"].append(body)
hass.data[DOMAIN][entry.entry_id] = {
"coordinator": coordinator,
"devices": device_data,
"listener": entry.add_update_listener(async_update_listener),
}

View file

@ -1,7 +1,7 @@
"""Support for a ScreenLogic Binary Sensor."""
import logging
from screenlogicpy.const import DEVICE_TYPE, ON_OFF
from screenlogicpy.const import DATA as SL_DATA, DEVICE_TYPE, ON_OFF
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_PROBLEM,
@ -19,16 +19,16 @@ SL_DEVICE_TYPE_TO_HA_DEVICE_CLASS = {DEVICE_TYPE.ALARM: DEVICE_CLASS_PROBLEM}
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 binary sensor
entities.append(ScreenLogicBinarySensor(coordinator, "chem_alarm"))
for binary_sensor in data["devices"]["binary_sensor"]:
entities.append(ScreenLogicBinarySensor(coordinator, binary_sensor))
async_add_entities(entities)
class ScreenLogicBinarySensor(ScreenlogicEntity, BinarySensorEntity):
"""Representation of a ScreenLogic binary sensor entity."""
"""Representation of the basic ScreenLogic binary sensor entity."""
@property
def name(self):
@ -49,4 +49,4 @@ class ScreenLogicBinarySensor(ScreenlogicEntity, BinarySensorEntity):
@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]

View file

@ -1,7 +1,7 @@
"""Support for a ScreenLogic heating device."""
import logging
from screenlogicpy.const import EQUIPMENT, HEAT_MODE
from screenlogicpy.const import DATA as SL_DATA, EQUIPMENT, HEAT_MODE
from homeassistant.components.climate import ClimateEntity
from homeassistant.components.climate.const import (
@ -37,11 +37,11 @@ SUPPORTED_PRESETS = [
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"]
for body in data["devices"]["body"]:
for body in coordinator.data[SL_DATA.KEY_BODIES]:
entities.append(ScreenLogicClimate(coordinator, body))
async_add_entities(entities)
@ -217,4 +217,4 @@ class ScreenLogicClimate(ScreenlogicEntity, ClimateEntity, RestoreEntity):
@property
def body(self):
"""Shortcut to access body data."""
return self.coordinator.data["bodies"][self._data_key]
return self.coordinator.data[SL_DATA.KEY_BODIES][self._data_key]

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,13 +25,21 @@ 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"]:
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 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))
@ -39,7 +47,7 @@ async def async_setup_entry(hass, config_entry, async_add_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]

View file

@ -1,7 +1,7 @@
"""Support for a ScreenLogic 'circuit' switch."""
import logging
from screenlogicpy.const import ON_OFF
from screenlogicpy.const import DATA as SL_DATA, ON_OFF
from homeassistant.components.switch import SwitchEntity
@ -14,11 +14,11 @@ _LOGGER = logging.getLogger(__name__)
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"]
for circuit in coordinator.data[SL_DATA.KEY_CIRCUITS]:
entities.append(ScreenLogicSwitch(coordinator, circuit))
for switch in data["devices"]["switch"]:
entities.append(ScreenLogicSwitch(coordinator, switch))
async_add_entities(entities)
@ -60,4 +60,4 @@ class ScreenLogicSwitch(ScreenlogicEntity, SwitchEntity):
@property
def circuit(self):
"""Shortcut to access the circuit."""
return self.coordinator.data["circuits"][self._data_key]
return self.coordinator.data[SL_DATA.KEY_CIRCUITS][self._data_key]