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.core import callback
from pyhon import Hon
from .const import DOMAIN
from .hon import HonEntity, unique_entities, get_coordinator
from .hon import HonEntity, unique_entities
_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:
hon: Hon = hass.data[DOMAIN][entry.unique_id]
appliances = []
for device in hon.appliances:
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := BINARY_SENSORS.get(device.appliance_type):
for description in descriptions:
entities = []
for device in hass.data[DOMAIN][entry.unique_id].appliances:
for description in BINARY_SENSORS.get(device.appliance_type, []):
if not device.get(description.key):
continue
appliances.append(
HonBinarySensorEntity(hass, coordinator, entry, device, description)
)
async_add_entities(appliances)
entity = HonBinarySensorEntity(hass, entry, device, description)
await entity.coordinator.async_config_entry_first_refresh()
entities.append(entity)
async_add_entities(entities)
class HonBinarySensorEntity(HonEntity, BinarySensorEntity):
entity_description: HonBinarySensorEntityDescription
def __init__(self, hass, coordinator, entry, device, description) -> None:
super().__init__(hass, entry, coordinator, device)
def __init__(self, hass, entry, device, description) -> None:
super().__init__(hass, entry, device)
self.entity_description = description
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.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from pyhon import Hon
from pyhon.appliance import HonAppliance
from .const import DOMAIN
from .hon import HonEntity, get_coordinator
from .hon import HonEntity
_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:
hon: Hon = hass.data[DOMAIN][entry.unique_id]
appliances = []
for device in hon.appliances:
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := BUTTONS.get(device.appliance_type):
for description in descriptions:
entities = []
for device in hass.data[DOMAIN][entry.unique_id].appliances:
for description in BUTTONS.get(device.appliance_type, []):
if not device.commands.get(description.key):
continue
appliances.extend(
[HonButtonEntity(hass, coordinator, entry, device, description)]
)
appliances.extend([HonFeatureRequestButton(hass, coordinator, entry, device)])
async_add_entities(appliances)
entity = HonButtonEntity(hass, entry, device, description)
await entity.coordinator.async_config_entry_first_refresh()
entities.append(entity)
entities.append(HonFeatureRequestButton(hass, entry, device))
await entities[-1].coordinator.async_config_entry_first_refresh()
async_add_entities(entities)
class HonButtonEntity(HonEntity, ButtonEntity):
def __init__(
self, hass, coordinator, entry, device: HonAppliance, description
) -> None:
super().__init__(hass, entry, coordinator, device)
def __init__(self, hass, entry, device: HonAppliance, description) -> None:
super().__init__(hass, entry, device)
self.entity_description = description
self._attr_unique_id = f"{super().unique_id}{description.key}"
@ -81,8 +73,8 @@ class HonButtonEntity(HonEntity, ButtonEntity):
class HonFeatureRequestButton(HonEntity, ButtonEntity):
def __init__(self, hass, coordinator, entry, device: HonAppliance) -> None:
super().__init__(hass, entry, coordinator, device)
def __init__(self, hass, entry, device: HonAppliance) -> None:
super().__init__(hass, entry, device)
self._attr_unique_id = f"{super().unique_id}_log_device_info"
self._attr_icon = "mdi:information"

View file

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

View file

@ -14,7 +14,8 @@ _LOGGER = logging.getLogger(__name__)
class HonEntity(CoordinatorEntity):
_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)
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.core import callback
from homeassistant.helpers.entity import EntityCategory
from pyhon import Hon
from pyhon.parameter.base import HonParameter
from pyhon.parameter.fixed import HonParameterFixed
from pyhon.parameter.range import HonParameterRange
from .const import DOMAIN
from .hon import HonEntity, unique_entities, get_coordinator
from .hon import HonEntity, unique_entities
NUMBERS: dict[str, tuple[NumberEntityDescription, ...]] = {
"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:
hon: Hon = hass.data[DOMAIN][entry.unique_id]
appliances = []
for device in hon.appliances:
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := NUMBERS.get(device.appliance_type):
for description in descriptions:
entities = []
for device in hass.data[DOMAIN][entry.unique_id].appliances:
for description in NUMBERS.get(device.appliance_type, []):
if description.key not in device.available_settings:
continue
appliances.extend(
[HonNumberEntity(hass, coordinator, entry, device, description)]
)
async_add_entities(appliances)
entity = HonNumberEntity(hass, entry, device, description)
await entity.coordinator.async_config_entry_first_refresh()
entities.append(entity)
async_add_entities(entities)
class HonNumberEntity(HonEntity, NumberEntity):
def __init__(self, hass, coordinator, entry, device, description) -> None:
super().__init__(hass, entry, coordinator, device)
def __init__(self, hass, entry, device, description) -> None:
super().__init__(hass, entry, device)
self._data = device.settings[description.key]
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.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, unique_entities, get_coordinator
from .hon import HonEntity, unique_entities
_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:
hon: Hon = hass.data[DOMAIN][entry.unique_id]
appliances = []
for device in hon.appliances:
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := SELECTS.get(device.appliance_type):
for description in descriptions:
entities = []
for device in hass.data[DOMAIN][entry.unique_id].appliances:
for description in SELECTS.get(device.appliance_type, []):
if description.key not in device.available_settings:
continue
appliances.extend(
[HonSelectEntity(hass, coordinator, entry, device, description)]
)
async_add_entities(appliances)
entity = HonSelectEntity(hass, entry, device, description)
await entity.coordinator.async_config_entry_first_refresh()
entities.append(entity)
async_add_entities(entities)
class HonSelectEntity(HonEntity, SelectEntity):
def __init__(
self, hass, coordinator, entry, device: HonAppliance, description
) -> None:
super().__init__(hass, entry, coordinator, device)
def __init__(self, hass, entry, device: HonAppliance, description) -> None:
super().__init__(hass, entry, device)
self.entity_description = description
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.helpers.entity import EntityCategory
from homeassistant.helpers.typing import StateType
from pyhon import Hon
from . import const
from .const import DOMAIN
from .hon import HonEntity, unique_entities, get_coordinator
from .hon import HonEntity, unique_entities
_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:
hon: Hon = hass.data[DOMAIN][entry.unique_id]
appliances = []
for device in hon.appliances:
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := SENSORS.get(device.appliance_type):
for description in descriptions:
entities = []
for device in hass.data[DOMAIN][entry.unique_id].appliances:
for description in SENSORS.get(device.appliance_type, []):
if not device.get(description.key) and not device.settings.get(
description.key
):
continue
appliances.extend(
[HonSensorEntity(hass, coordinator, entry, device, description)]
)
entity = HonSensorEntity(hass, 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):
def __init__(self, hass, coordinator, entry, device, description) -> None:
super().__init__(hass, entry, coordinator, device)
def __init__(self, hass, entry, device, description) -> None:
super().__init__(hass, entry, device)
self.entity_description = description
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.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 HonEntity, unique_entities, get_coordinator
from .hon import HonEntity, unique_entities
_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:
hon: Hon = hass.data[DOMAIN][entry.unique_id]
appliances = []
for device in hon.appliances:
coordinator = get_coordinator(hass, device)
await coordinator.async_config_entry_first_refresh()
if descriptions := SWITCHES.get(device.appliance_type):
for description in descriptions:
entities = []
for device in hass.data[DOMAIN][entry.unique_id].appliances:
for description in SWITCHES.get(device.appliance_type, []):
if description.entity_category == EntityCategory.CONFIG:
if description.key not in device.available_settings:
continue
@ -374,11 +368,11 @@ async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> Non
]
):
continue
appliances.extend(
[HonSwitchEntity(hass, coordinator, entry, device, description)]
)
entity = HonSwitchEntity(hass, 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):
@ -387,12 +381,11 @@ class HonSwitchEntity(HonEntity, SwitchEntity):
def __init__(
self,
hass,
coordinator,
entry,
device: HonAppliance,
description: HonSwitchEntityDescription,
) -> None:
super().__init__(hass, entry, coordinator, device)
super().__init__(hass, entry, device)
self.entity_description = description
self._attr_unique_id = f"{super().unique_id}{description.key}"