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:
if not device.get(description.key):
continue
appliances.append(
HonBinarySensorEntity(hass, coordinator, entry, device, description)
)
async_add_entities(appliances)
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
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:
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)
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
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:
if description.key not in list(device.commands):
continue
appliances.append(
HonClimateEntity(hass, coordinator, entry, device, description)
)
async_add_entities(appliances)
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
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:
if description.key not in device.available_settings:
continue
appliances.extend(
[HonNumberEntity(hass, coordinator, entry, device, description)]
)
async_add_entities(appliances)
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
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:
if description.key not in device.available_settings:
continue
appliances.extend(
[HonSelectEntity(hass, coordinator, entry, device, description)]
)
async_add_entities(appliances)
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
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()
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
entity = HonSensorEntity(hass, entry, device, description)
await entity.coordinator.async_config_entry_first_refresh()
entities.append(entity)
if descriptions := SENSORS.get(device.appliance_type):
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)
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,31 +353,26 @@ 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()
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
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):
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)
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}"