Centralizes Toon data, reducing API calls (#23988)
* Centralizes Toon data, reducing API calls Fixes #21825 Signed-off-by: Franck Nijhof <frenck@addons.community> * Fixes bad copy past action in services.yaml Signed-off-by: Franck Nijhof <frenck@addons.community> * Addresses review comments Signed-off-by: Franck Nijhof <frenck@addons.community> * 👕 Fixes too many blank lines * Unsub dispatcher
This commit is contained in:
1 parent
f3e542542a
commit
3ce1049d21
6 files changed
+538
-198
No files matched your search
@@ -1,6 +1,5 @@
|
||||
"""Support for Toon binary sensors."""
|
||||
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
@@ -8,62 +7,123 @@ from homeassistant.components.binary_sensor import BinarySensorDevice
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
|
||||
from . import (ToonEntity, ToonDisplayDeviceEntity, ToonBoilerDeviceEntity,
|
||||
ToonBoilerModuleDeviceEntity)
|
||||
from .const import DATA_TOON_CLIENT, DOMAIN
|
||||
from . import (
|
||||
ToonData,
|
||||
ToonEntity,
|
||||
ToonDisplayDeviceEntity,
|
||||
ToonBoilerDeviceEntity,
|
||||
ToonBoilerModuleDeviceEntity,
|
||||
)
|
||||
from .const import DATA_TOON, DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=5)
|
||||
SCAN_INTERVAL = timedelta(seconds=300)
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry,
|
||||
async_add_entities) -> None:
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistantType, entry: ConfigEntry, async_add_entities
|
||||
) -> None:
|
||||
"""Set up a Toon binary sensor based on a config entry."""
|
||||
toon = hass.data[DATA_TOON_CLIENT][entry.entry_id]
|
||||
toon = hass.data[DATA_TOON][entry.entry_id]
|
||||
|
||||
sensors = [
|
||||
ToonBoilerModuleBinarySensor(toon, 'thermostat_info',
|
||||
'boiler_connected', None,
|
||||
'Boiler Module Connection',
|
||||
'mdi:check-network-outline',
|
||||
'connectivity'),
|
||||
|
||||
ToonDisplayBinarySensor(toon, 'thermostat_info', 'active_state', 4,
|
||||
"Toon Holiday Mode", 'mdi:airport', None),
|
||||
|
||||
ToonDisplayBinarySensor(toon, 'thermostat_info', 'next_program', None,
|
||||
"Toon Program", 'mdi:calendar-clock', None),
|
||||
ToonBoilerModuleBinarySensor(
|
||||
toon,
|
||||
'thermostat_info',
|
||||
'boiler_connected',
|
||||
None,
|
||||
'Boiler Module Connection',
|
||||
'mdi:check-network-outline',
|
||||
'connectivity',
|
||||
),
|
||||
ToonDisplayBinarySensor(
|
||||
toon,
|
||||
'thermostat_info',
|
||||
'active_state',
|
||||
4,
|
||||
"Toon Holiday Mode",
|
||||
'mdi:airport',
|
||||
None,
|
||||
),
|
||||
ToonDisplayBinarySensor(
|
||||
toon,
|
||||
'thermostat_info',
|
||||
'next_program',
|
||||
None,
|
||||
"Toon Program",
|
||||
'mdi:calendar-clock',
|
||||
None,
|
||||
),
|
||||
]
|
||||
|
||||
if toon.thermostat_info.have_ot_boiler:
|
||||
sensors.extend([
|
||||
ToonBoilerBinarySensor(toon, 'thermostat_info',
|
||||
'ot_communication_error', '0',
|
||||
"OpenTherm Connection",
|
||||
'mdi:check-network-outline',
|
||||
'connectivity'),
|
||||
ToonBoilerBinarySensor(toon, 'thermostat_info', 'error_found', 255,
|
||||
"Boiler Status", 'mdi:alert', 'problem',
|
||||
inverted=True),
|
||||
ToonBoilerBinarySensor(toon, 'thermostat_info', 'burner_info',
|
||||
None, "Boiler Burner", 'mdi:fire', None),
|
||||
ToonBoilerBinarySensor(toon, 'thermostat_info', 'burner_info', '2',
|
||||
"Hot Tap Water", 'mdi:water-pump', None),
|
||||
ToonBoilerBinarySensor(toon, 'thermostat_info', 'burner_info', '3',
|
||||
"Boiler Preheating", 'mdi:fire', None),
|
||||
])
|
||||
sensors.extend(
|
||||
[
|
||||
ToonBoilerBinarySensor(
|
||||
toon,
|
||||
'thermostat_info',
|
||||
'ot_communication_error',
|
||||
'0',
|
||||
"OpenTherm Connection",
|
||||
'mdi:check-network-outline',
|
||||
'connectivity',
|
||||
),
|
||||
ToonBoilerBinarySensor(
|
||||
toon,
|
||||
'thermostat_info',
|
||||
'error_found',
|
||||
255,
|
||||
"Boiler Status",
|
||||
'mdi:alert',
|
||||
'problem',
|
||||
inverted=True,
|
||||
),
|
||||
ToonBoilerBinarySensor(
|
||||
toon,
|
||||
'thermostat_info',
|
||||
'burner_info',
|
||||
None,
|
||||
"Boiler Burner",
|
||||
'mdi:fire',
|
||||
None,
|
||||
),
|
||||
ToonBoilerBinarySensor(
|
||||
toon,
|
||||
'thermostat_info',
|
||||
'burner_info',
|
||||
'2',
|
||||
"Hot Tap Water",
|
||||
'mdi:water-pump',
|
||||
None,
|
||||
),
|
||||
ToonBoilerBinarySensor(
|
||||
toon,
|
||||
'thermostat_info',
|
||||
'burner_info',
|
||||
'3',
|
||||
"Boiler Preheating",
|
||||
'mdi:fire',
|
||||
None,
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
async_add_entities(sensors)
|
||||
async_add_entities(sensors, True)
|
||||
|
||||
|
||||
class ToonBinarySensor(ToonEntity, BinarySensorDevice):
|
||||
"""Defines an Toon binary sensor."""
|
||||
|
||||
def __init__(self, toon, section: str, measurement: str, on_value: Any,
|
||||
name: str, icon: str, device_class: str,
|
||||
inverted: bool = False) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
toon: ToonData,
|
||||
section: str,
|
||||
measurement: str,
|
||||
on_value: Any,
|
||||
name: str,
|
||||
icon: str,
|
||||
device_class: str,
|
||||
inverted: bool = False,
|
||||
) -> None:
|
||||
"""Initialize the Toon sensor."""
|
||||
self._state = inverted
|
||||
self._device_class = device_class
|
||||
@@ -77,8 +137,16 @@ class ToonBinarySensor(ToonEntity, BinarySensorDevice):
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return the unique ID for this binary sensor."""
|
||||
return '_'.join([DOMAIN, self.toon.agreement.id, 'binary_sensor',
|
||||
self.section, self.measurement, str(self.on_value)])
|
||||
return '_'.join(
|
||||
[
|
||||
DOMAIN,
|
||||
self.toon.agreement.id,
|
||||
'binary_sensor',
|
||||
self.section,
|
||||
self.measurement,
|
||||
str(self.on_value),
|
||||
]
|
||||
)
|
||||
|
||||
@property
|
||||
def device_class(self) -> str:
|
||||
@@ -118,8 +186,9 @@ class ToonDisplayBinarySensor(ToonBinarySensor, ToonDisplayDeviceEntity):
|
||||
pass
|
||||
|
||||
|
||||
class ToonBoilerModuleBinarySensor(ToonBinarySensor,
|
||||
ToonBoilerModuleDeviceEntity):
|
||||
class ToonBoilerModuleBinarySensor(
|
||||
ToonBinarySensor, ToonBoilerModuleDeviceEntity
|
||||
):
|
||||
"""Defines a Boiler module binary sensor."""
|
||||
|
||||
pass
|
||||
Reference in new issue
Block a user