Refactor get coordinator

This commit is contained in:
Andre Basche 2023-05-25 00:52:54 +02:00
parent 9518031f24
commit e9d1bb2056
9 changed files with 43 additions and 69 deletions

View File

@ -1,8 +1,6 @@
import logging
from dataclasses import dataclass
from pyhon import Hon
from homeassistant.components.binary_sensor import (
BinarySensorEntityDescription,
BinarySensorDeviceClass,
@ -10,8 +8,10 @@ from homeassistant.components.binary_sensor import (
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback
from pyhon import Hon
from .const import DOMAIN
from .hon import HonCoordinator, HonEntity, unique_entities
from .hon import HonEntity, unique_entities, get_coordinator
_LOGGER = logging.getLogger(__name__)
@ -251,14 +251,9 @@ BINARY_SENSORS["WD"] = unique_entities(BINARY_SENSORS["WM"], BINARY_SENSORS["TD"
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
hon: Hon = hass.data[DOMAIN][entry.unique_id]
coordinators = hass.data[DOMAIN]["coordinators"]
appliances = []
for device in hon.appliances:
if device.unique_id in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id]
else:
coordinator = HonCoordinator(hass, device)
hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := BINARY_SENSORS.get(device.appliance_type):

View File

@ -1,7 +1,6 @@
import logging
import pkg_resources
from homeassistant.components import persistent_notification
from homeassistant.components.button import ButtonEntityDescription, ButtonEntity
from homeassistant.config_entries import ConfigEntry
@ -10,7 +9,7 @@ from pyhon import Hon
from pyhon.appliance import HonAppliance
from .const import DOMAIN
from .hon import HonCoordinator, HonEntity
from .hon import HonEntity, get_coordinator
_LOGGER = logging.getLogger(__name__)
@ -42,14 +41,9 @@ BUTTONS: dict[str, tuple[ButtonEntityDescription, ...]] = {
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
hon: Hon = hass.data[DOMAIN][entry.unique_id]
coordinators = hass.data[DOMAIN]["coordinators"]
appliances = []
for device in hon.appliances:
if device.unique_id in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id]
else:
coordinator = HonCoordinator(hass, device)
hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := BUTTONS.get(device.appliance_type):

View File

@ -1,8 +1,5 @@
import logging
from pyhon import Hon
from pyhon.appliance import HonAppliance
from homeassistant.components.climate import (
ClimateEntity,
ClimateEntityDescription,
@ -23,8 +20,11 @@ from homeassistant.const import (
TEMP_CELSIUS,
)
from homeassistant.core import callback
from pyhon import Hon
from pyhon.appliance import HonAppliance
from .const import HON_HVAC_MODE, HON_FAN, HON_HVAC_PROGRAM, DOMAIN
from .hon import HonEntity, HonCoordinator
from .hon import HonEntity, get_coordinator
_LOGGER = logging.getLogger(__name__)
@ -42,14 +42,9 @@ CLIMATES = {
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
hon: Hon = hass.data[DOMAIN][entry.unique_id]
coordinators = hass.data[DOMAIN]["coordinators"]
appliances = []
for device in hon.appliances:
if device.unique_id in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id]
else:
coordinator = HonCoordinator(hass, device)
hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := CLIMATES.get(device.appliance_type):

View File

@ -1,9 +1,9 @@
import logging
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)

View File

@ -1,11 +1,11 @@
import logging
from datetime import timedelta
from pyhon.appliance import HonAppliance
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from pyhon.appliance import HonAppliance
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
@ -59,3 +59,13 @@ def unique_entities(base_entities, new_entities):
if entity.key not in existing_entities:
result.append(entity)
return tuple(result)
def get_coordinator(hass, appliance):
coordinators = hass.data[DOMAIN]["coordinators"]
if appliance.unique_id in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][appliance.unique_id]
else:
coordinator = HonCoordinator(hass, appliance)
hass.data[DOMAIN]["coordinators"][appliance.unique_id] = coordinator
return coordinator

View File

@ -14,7 +14,7 @@ from pyhon.parameter.fixed import HonParameterFixed
from pyhon.parameter.range import HonParameterRange
from .const import DOMAIN
from .hon import HonEntity, HonCoordinator, unique_entities
from .hon import HonEntity, unique_entities, get_coordinator
NUMBERS: dict[str, tuple[NumberEntityDescription, ...]] = {
"WM": (
@ -175,14 +175,9 @@ NUMBERS["WD"] = unique_entities(NUMBERS["WM"], NUMBERS["TD"])
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
hon: Hon = hass.data[DOMAIN][entry.unique_id]
coordinators = hass.data[DOMAIN]["coordinators"]
appliances = []
for device in hon.appliances:
if device.unique_id in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id]
else:
coordinator = HonCoordinator(hass, device)
hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := NUMBERS.get(device.appliance_type):

View File

@ -2,17 +2,17 @@ from __future__ import annotations
import logging
from pyhon import Hon
from pyhon.appliance import HonAppliance
from pyhon.parameter.fixed import HonParameterFixed
from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfTemperature, UnitOfTime, REVOLUTIONS_PER_MINUTE
from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory
from pyhon import Hon
from pyhon.appliance import HonAppliance
from pyhon.parameter.fixed import HonParameterFixed
from .const import DOMAIN
from .hon import HonEntity, HonCoordinator, unique_entities
from .hon import HonEntity, unique_entities, get_coordinator
_LOGGER = logging.getLogger(__name__)
@ -139,14 +139,9 @@ SELECTS["WD"] = unique_entities(SELECTS["WM"], SELECTS["TD"])
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
hon: Hon = hass.data[DOMAIN][entry.unique_id]
coordinators = hass.data[DOMAIN]["coordinators"]
appliances = []
for device in hon.appliances:
if device.unique_id in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id]
else:
coordinator = HonCoordinator(hass, device)
hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := SELECTS.get(device.appliance_type):

View File

@ -1,7 +1,5 @@
import logging
from pyhon import Hon
from homeassistant.components.sensor import (
SensorEntity,
SensorDeviceClass,
@ -22,9 +20,11 @@ from homeassistant.const import (
from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.typing import StateType
from pyhon import Hon
from . import const
from .const import DOMAIN
from .hon import HonCoordinator, HonEntity, unique_entities
from .hon import HonEntity, unique_entities, get_coordinator
_LOGGER = logging.getLogger(__name__)
@ -512,14 +512,9 @@ SENSORS["WD"] = unique_entities(SENSORS["WM"], SENSORS["TD"])
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
hon: Hon = hass.data[DOMAIN][entry.unique_id]
coordinators = hass.data[DOMAIN]["coordinators"]
appliances = []
for device in hon.appliances:
if device.unique_id in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id]
else:
coordinator = HonCoordinator(hass, device)
hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := SENSORS.get(device.appliance_type):

View File

@ -2,17 +2,17 @@ import logging
from dataclasses import dataclass
from typing import Any
from pyhon import Hon
from pyhon.appliance import HonAppliance
from pyhon.parameter.base import HonParameter
from pyhon.parameter.range import HonParameterRange
from homeassistant.components.switch import SwitchEntityDescription, SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import callback
from pyhon import Hon
from pyhon.appliance import HonAppliance
from pyhon.parameter.base import HonParameter
from pyhon.parameter.range import HonParameterRange
from .const import DOMAIN
from .hon import HonCoordinator, HonEntity, unique_entities
from .hon import HonEntity, unique_entities, get_coordinator
_LOGGER = logging.getLogger(__name__)
@ -355,14 +355,9 @@ SWITCHES["WD"] = unique_entities(SWITCHES["WD"], SWITCHES["TD"])
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
hon: Hon = hass.data[DOMAIN][entry.unique_id]
coordinators = hass.data[DOMAIN]["coordinators"]
appliances = []
for device in hon.appliances:
if device.unique_id in coordinators:
coordinator = hass.data[DOMAIN]["coordinators"][device.unique_id]
else:
coordinator = HonCoordinator(hass, device)
hass.data[DOMAIN]["coordinators"][device.unique_id] = coordinator
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := SWITCHES.get(device.appliance_type):