Refactor entry setup

This commit is contained in:
Andre Basche 2023-05-25 01:30:33 +02:00
parent e9d1bb2056
commit 696dc136eb
8 changed files with 101 additions and 151 deletions

View File

@ -8,10 +8,9 @@ from homeassistant.components.binary_sensor import (
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback from homeassistant.core import callback
from pyhon import Hon
from .const import DOMAIN from .const import DOMAIN
from .hon import HonEntity, unique_entities, get_coordinator from .hon import HonEntity, unique_entities
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -250,28 +249,22 @@ BINARY_SENSORS["WD"] = unique_entities(BINARY_SENSORS["WM"], BINARY_SENSORS["TD"
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None: async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
hon: Hon = hass.data[DOMAIN][entry.unique_id] entities = []
appliances = [] for device in hass.data[DOMAIN][entry.unique_id].appliances:
for device in hon.appliances: for description in BINARY_SENSORS.get(device.appliance_type, []):
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := BINARY_SENSORS.get(device.appliance_type):
for description in descriptions:
if not device.get(description.key): if not device.get(description.key):
continue continue
appliances.append( entity = HonBinarySensorEntity(hass, entry, device, description)
HonBinarySensorEntity(hass, coordinator, entry, device, description) await entity.coordinator.async_config_entry_first_refresh()
) entities.append(entity)
async_add_entities(entities)
async_add_entities(appliances)
class HonBinarySensorEntity(HonEntity, BinarySensorEntity): class HonBinarySensorEntity(HonEntity, BinarySensorEntity):
entity_description: HonBinarySensorEntityDescription entity_description: HonBinarySensorEntityDescription
def __init__(self, hass, coordinator, entry, device, description) -> None: def __init__(self, hass, entry, device, description) -> None:
super().__init__(hass, entry, coordinator, device) super().__init__(hass, entry, device)
self.entity_description = description self.entity_description = description
self._attr_unique_id = f"{super().unique_id}{description.key}" self._attr_unique_id = f"{super().unique_id}{description.key}"

View File

@ -5,11 +5,10 @@ from homeassistant.components import persistent_notification
from homeassistant.components.button import ButtonEntityDescription, ButtonEntity from homeassistant.components.button import ButtonEntityDescription, ButtonEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from pyhon import Hon
from pyhon.appliance import HonAppliance from pyhon.appliance import HonAppliance
from .const import DOMAIN from .const import DOMAIN
from .hon import HonEntity, get_coordinator from .hon import HonEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -40,29 +39,22 @@ BUTTONS: dict[str, tuple[ButtonEntityDescription, ...]] = {
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None: async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
hon: Hon = hass.data[DOMAIN][entry.unique_id] entities = []
appliances = [] for device in hass.data[DOMAIN][entry.unique_id].appliances:
for device in hon.appliances: for description in BUTTONS.get(device.appliance_type, []):
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := BUTTONS.get(device.appliance_type):
for description in descriptions:
if not device.commands.get(description.key): if not device.commands.get(description.key):
continue continue
appliances.extend( entity = HonButtonEntity(hass, entry, device, description)
[HonButtonEntity(hass, coordinator, entry, device, description)] await entity.coordinator.async_config_entry_first_refresh()
) entities.append(entity)
appliances.extend([HonFeatureRequestButton(hass, coordinator, entry, device)]) entities.append(HonFeatureRequestButton(hass, entry, device))
await entities[-1].coordinator.async_config_entry_first_refresh()
async_add_entities(appliances) async_add_entities(entities)
class HonButtonEntity(HonEntity, ButtonEntity): class HonButtonEntity(HonEntity, ButtonEntity):
def __init__( def __init__(self, hass, entry, device: HonAppliance, description) -> None:
self, hass, coordinator, entry, device: HonAppliance, description super().__init__(hass, entry, device)
) -> None:
super().__init__(hass, entry, coordinator, device)
self.entity_description = description self.entity_description = description
self._attr_unique_id = f"{super().unique_id}{description.key}" self._attr_unique_id = f"{super().unique_id}{description.key}"
@ -81,8 +73,8 @@ class HonButtonEntity(HonEntity, ButtonEntity):
class HonFeatureRequestButton(HonEntity, ButtonEntity): class HonFeatureRequestButton(HonEntity, ButtonEntity):
def __init__(self, hass, coordinator, entry, device: HonAppliance) -> None: def __init__(self, hass, entry, device: HonAppliance) -> None:
super().__init__(hass, entry, coordinator, device) super().__init__(hass, entry, device)
self._attr_unique_id = f"{super().unique_id}_log_device_info" self._attr_unique_id = f"{super().unique_id}_log_device_info"
self._attr_icon = "mdi:information" self._attr_icon = "mdi:information"

View File

@ -20,11 +20,10 @@ from homeassistant.const import (
TEMP_CELSIUS, TEMP_CELSIUS,
) )
from homeassistant.core import callback from homeassistant.core import callback
from pyhon import Hon
from pyhon.appliance import HonAppliance from pyhon.appliance import HonAppliance
from .const import HON_HVAC_MODE, HON_FAN, HON_HVAC_PROGRAM, DOMAIN from .const import HON_HVAC_MODE, HON_FAN, HON_HVAC_PROGRAM, DOMAIN
from .hon import HonEntity, get_coordinator from .hon import HonEntity
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -41,27 +40,20 @@ CLIMATES = {
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None: async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
hon: Hon = hass.data[DOMAIN][entry.unique_id] entities = []
appliances = [] for device in hass.data[DOMAIN][entry.unique_id].appliances:
for device in hon.appliances: for description in CLIMATES.get(device.appliance_type, []):
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := CLIMATES.get(device.appliance_type):
for description in descriptions:
if description.key not in list(device.commands): if description.key not in list(device.commands):
continue continue
appliances.append( entity = HonClimateEntity(hass, entry, device, description)
HonClimateEntity(hass, coordinator, entry, device, description) await entity.coordinator.async_config_entry_first_refresh()
) entities.append(entity)
async_add_entities(appliances) async_add_entities(entities)
class HonClimateEntity(HonEntity, ClimateEntity): class HonClimateEntity(HonEntity, ClimateEntity):
def __init__( def __init__(self, hass, entry, device: HonAppliance, description) -> None:
self, hass, coordinator, entry, device: HonAppliance, description super().__init__(hass, entry, device)
) -> None:
super().__init__(hass, entry, coordinator, device)
self.entity_description = description self.entity_description = description
self._attr_unique_id = f"{super().unique_id}climate" self._attr_unique_id = f"{super().unique_id}climate"

View File

@ -14,7 +14,8 @@ _LOGGER = logging.getLogger(__name__)
class HonEntity(CoordinatorEntity): class HonEntity(CoordinatorEntity):
_attr_has_entity_name = True _attr_has_entity_name = True
def __init__(self, hass, entry, coordinator, device: HonAppliance) -> None: def __init__(self, hass, entry, device: HonAppliance) -> None:
coordinator = get_coordinator(hass, device)
super().__init__(coordinator) super().__init__(coordinator)
self._hon = hass.data[DOMAIN][entry.unique_id] self._hon = hass.data[DOMAIN][entry.unique_id]

View File

@ -8,13 +8,12 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfTime, UnitOfTemperature from homeassistant.const import UnitOfTime, UnitOfTemperature
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
from pyhon import Hon
from pyhon.parameter.base import HonParameter from pyhon.parameter.base import HonParameter
from pyhon.parameter.fixed import HonParameterFixed from pyhon.parameter.fixed import HonParameterFixed
from pyhon.parameter.range import HonParameterRange from pyhon.parameter.range import HonParameterRange
from .const import DOMAIN from .const import DOMAIN
from .hon import HonEntity, unique_entities, get_coordinator from .hon import HonEntity, unique_entities
NUMBERS: dict[str, tuple[NumberEntityDescription, ...]] = { NUMBERS: dict[str, tuple[NumberEntityDescription, ...]] = {
"WM": ( "WM": (
@ -174,26 +173,20 @@ NUMBERS["WD"] = unique_entities(NUMBERS["WM"], NUMBERS["TD"])
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None: async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
hon: Hon = hass.data[DOMAIN][entry.unique_id] entities = []
appliances = [] for device in hass.data[DOMAIN][entry.unique_id].appliances:
for device in hon.appliances: for description in NUMBERS.get(device.appliance_type, []):
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := NUMBERS.get(device.appliance_type):
for description in descriptions:
if description.key not in device.available_settings: if description.key not in device.available_settings:
continue continue
appliances.extend( entity = HonNumberEntity(hass, entry, device, description)
[HonNumberEntity(hass, coordinator, entry, device, description)] await entity.coordinator.async_config_entry_first_refresh()
) entities.append(entity)
async_add_entities(entities)
async_add_entities(appliances)
class HonNumberEntity(HonEntity, NumberEntity): class HonNumberEntity(HonEntity, NumberEntity):
def __init__(self, hass, coordinator, entry, device, description) -> None: def __init__(self, hass, entry, device, description) -> None:
super().__init__(hass, entry, coordinator, device) super().__init__(hass, entry, device)
self._data = device.settings[description.key] self._data = device.settings[description.key]
self.entity_description = description self.entity_description = description

View File

@ -7,12 +7,11 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfTemperature, UnitOfTime, REVOLUTIONS_PER_MINUTE from homeassistant.const import UnitOfTemperature, UnitOfTime, REVOLUTIONS_PER_MINUTE
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
from pyhon import Hon
from pyhon.appliance import HonAppliance from pyhon.appliance import HonAppliance
from pyhon.parameter.fixed import HonParameterFixed from pyhon.parameter.fixed import HonParameterFixed
from .const import DOMAIN from .const import DOMAIN
from .hon import HonEntity, unique_entities, get_coordinator from .hon import HonEntity, unique_entities
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -138,27 +137,20 @@ SELECTS["WD"] = unique_entities(SELECTS["WM"], SELECTS["TD"])
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None: async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
hon: Hon = hass.data[DOMAIN][entry.unique_id] entities = []
appliances = [] for device in hass.data[DOMAIN][entry.unique_id].appliances:
for device in hon.appliances: for description in SELECTS.get(device.appliance_type, []):
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := SELECTS.get(device.appliance_type):
for description in descriptions:
if description.key not in device.available_settings: if description.key not in device.available_settings:
continue continue
appliances.extend( entity = HonSelectEntity(hass, entry, device, description)
[HonSelectEntity(hass, coordinator, entry, device, description)] await entity.coordinator.async_config_entry_first_refresh()
) entities.append(entity)
async_add_entities(appliances) async_add_entities(entities)
class HonSelectEntity(HonEntity, SelectEntity): class HonSelectEntity(HonEntity, SelectEntity):
def __init__( def __init__(self, hass, entry, device: HonAppliance, description) -> None:
self, hass, coordinator, entry, device: HonAppliance, description super().__init__(hass, entry, device)
) -> None:
super().__init__(hass, entry, coordinator, device)
self.entity_description = description self.entity_description = description
self._attr_unique_id = f"{super().unique_id}{description.key}" self._attr_unique_id = f"{super().unique_id}{description.key}"

View File

@ -20,11 +20,10 @@ from homeassistant.const import (
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers.entity import EntityCategory from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
from pyhon import Hon
from . import const from . import const
from .const import DOMAIN from .const import DOMAIN
from .hon import HonEntity, unique_entities, get_coordinator from .hon import HonEntity, unique_entities
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -511,28 +510,23 @@ SENSORS["WD"] = unique_entities(SENSORS["WM"], SENSORS["TD"])
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None: async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
hon: Hon = hass.data[DOMAIN][entry.unique_id] entities = []
appliances = [] for device in hass.data[DOMAIN][entry.unique_id].appliances:
for device in hon.appliances: for description in SENSORS.get(device.appliance_type, []):
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := SENSORS.get(device.appliance_type):
for description in descriptions:
if not device.get(description.key) and not device.settings.get( if not device.get(description.key) and not device.settings.get(
description.key description.key
): ):
continue continue
appliances.extend( entity = HonSensorEntity(hass, entry, device, description)
[HonSensorEntity(hass, coordinator, entry, device, description)] await entity.coordinator.async_config_entry_first_refresh()
) entities.append(entity)
async_add_entities(appliances) async_add_entities(entities)
class HonSensorEntity(HonEntity, SensorEntity): class HonSensorEntity(HonEntity, SensorEntity):
def __init__(self, hass, coordinator, entry, device, description) -> None: def __init__(self, hass, entry, device, description) -> None:
super().__init__(hass, entry, coordinator, device) super().__init__(hass, entry, device)
self.entity_description = description self.entity_description = description
self._attr_unique_id = f"{super().unique_id}{description.key}" self._attr_unique_id = f"{super().unique_id}{description.key}"

View File

@ -6,13 +6,12 @@ from homeassistant.components.switch import SwitchEntityDescription, SwitchEntit
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import callback from homeassistant.core import callback
from pyhon import Hon
from pyhon.appliance import HonAppliance from pyhon.appliance import HonAppliance
from pyhon.parameter.base import HonParameter from pyhon.parameter.base import HonParameter
from pyhon.parameter.range import HonParameterRange from pyhon.parameter.range import HonParameterRange
from .const import DOMAIN from .const import DOMAIN
from .hon import HonEntity, unique_entities, get_coordinator from .hon import HonEntity, unique_entities
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -354,14 +353,9 @@ SWITCHES["WD"] = unique_entities(SWITCHES["WD"], SWITCHES["TD"])
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None: async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
hon: Hon = hass.data[DOMAIN][entry.unique_id] entities = []
appliances = [] for device in hass.data[DOMAIN][entry.unique_id].appliances:
for device in hon.appliances: for description in SWITCHES.get(device.appliance_type, []):
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := SWITCHES.get(device.appliance_type):
for description in descriptions:
if description.entity_category == EntityCategory.CONFIG: if description.entity_category == EntityCategory.CONFIG:
if description.key not in device.available_settings: if description.key not in device.available_settings:
continue continue
@ -374,11 +368,11 @@ async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> Non
] ]
): ):
continue continue
appliances.extend( entity = HonSwitchEntity(hass, entry, device, description)
[HonSwitchEntity(hass, coordinator, entry, device, description)] await entity.coordinator.async_config_entry_first_refresh()
) entities.append(entity)
async_add_entities(appliances) async_add_entities(entities)
class HonSwitchEntity(HonEntity, SwitchEntity): class HonSwitchEntity(HonEntity, SwitchEntity):
@ -387,12 +381,11 @@ class HonSwitchEntity(HonEntity, SwitchEntity):
def __init__( def __init__(
self, self,
hass, hass,
coordinator,
entry, entry,
device: HonAppliance, device: HonAppliance,
description: HonSwitchEntityDescription, description: HonSwitchEntityDescription,
) -> None: ) -> None:
super().__init__(hass, entry, coordinator, device) super().__init__(hass, entry, device)
self.entity_description = description self.entity_description = description
self._attr_unique_id = f"{super().unique_id}{description.key}" self._attr_unique_id = f"{super().unique_id}{description.key}"