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) if not device.get(description.key):
await coordinator.async_config_entry_first_refresh() continue
entity = HonBinarySensorEntity(hass, entry, device, description)
if descriptions := BINARY_SENSORS.get(device.appliance_type): await entity.coordinator.async_config_entry_first_refresh()
for description in descriptions: entities.append(entity)
if not device.get(description.key): async_add_entities(entities)
continue
appliances.append(
HonBinarySensorEntity(hass, coordinator, entry, device, description)
)
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) if not device.commands.get(description.key):
await coordinator.async_config_entry_first_refresh() continue
entity = HonButtonEntity(hass, entry, device, description)
if descriptions := BUTTONS.get(device.appliance_type): await entity.coordinator.async_config_entry_first_refresh()
for description in descriptions: entities.append(entity)
if not device.commands.get(description.key): entities.append(HonFeatureRequestButton(hass, entry, device))
continue await entities[-1].coordinator.async_config_entry_first_refresh()
appliances.extend( async_add_entities(entities)
[HonButtonEntity(hass, coordinator, entry, device, description)]
)
appliances.extend([HonFeatureRequestButton(hass, coordinator, entry, device)])
async_add_entities(appliances)
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) if description.key not in list(device.commands):
await coordinator.async_config_entry_first_refresh() continue
entity = HonClimateEntity(hass, entry, device, description)
if descriptions := CLIMATES.get(device.appliance_type): await entity.coordinator.async_config_entry_first_refresh()
for description in descriptions: entities.append(entity)
if description.key not in list(device.commands): async_add_entities(entities)
continue
appliances.append(
HonClimateEntity(hass, coordinator, entry, device, description)
)
async_add_entities(appliances)
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) if description.key not in device.available_settings:
await coordinator.async_config_entry_first_refresh() continue
entity = HonNumberEntity(hass, entry, device, description)
if descriptions := NUMBERS.get(device.appliance_type): await entity.coordinator.async_config_entry_first_refresh()
for description in descriptions: entities.append(entity)
if description.key not in device.available_settings: async_add_entities(entities)
continue
appliances.extend(
[HonNumberEntity(hass, coordinator, entry, device, description)]
)
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) if description.key not in device.available_settings:
await coordinator.async_config_entry_first_refresh() continue
entity = HonSelectEntity(hass, entry, device, description)
if descriptions := SELECTS.get(device.appliance_type): await entity.coordinator.async_config_entry_first_refresh()
for description in descriptions: entities.append(entity)
if description.key not in device.available_settings: async_add_entities(entities)
continue
appliances.extend(
[HonSelectEntity(hass, coordinator, entry, device, description)]
)
async_add_entities(appliances)
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) if not device.get(description.key) and not device.settings.get(
await coordinator.async_config_entry_first_refresh() description.key
):
continue
entity = HonSensorEntity(hass, entry, device, description)
await entity.coordinator.async_config_entry_first_refresh()
entities.append(entity)
if descriptions := SENSORS.get(device.appliance_type): async_add_entities(entities)
for description in descriptions:
if not device.get(description.key) and not device.settings.get(
description.key
):
continue
appliances.extend(
[HonSensorEntity(hass, coordinator, entry, device, description)]
)
async_add_entities(appliances)
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,31 +353,26 @@ 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) if description.entity_category == EntityCategory.CONFIG:
await coordinator.async_config_entry_first_refresh() if description.key not in device.available_settings:
continue
else:
if not any(
[
device.get(description.key) is not None,
description.turn_on_key in list(device.commands),
description.turn_off_key in list(device.commands),
]
):
continue
entity = HonSwitchEntity(hass, entry, device, description)
await entity.coordinator.async_config_entry_first_refresh()
entities.append(entity)
if descriptions := SWITCHES.get(device.appliance_type): async_add_entities(entities)
for description in descriptions:
if description.entity_category == EntityCategory.CONFIG:
if description.key not in device.available_settings:
continue
else:
if not any(
[
device.get(description.key) is not None,
description.turn_on_key in list(device.commands),
description.turn_off_key in list(device.commands),
]
):
continue
appliances.extend(
[HonSwitchEntity(hass, coordinator, entry, device, description)]
)
async_add_entities(appliances)
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}"