Skip to content

Commit

Permalink
Add a data model to Advantage Air (home-assistant#91519)
Browse files Browse the repository at this point in the history
Co-authored-by: J. Nick Koston <[email protected]>
  • Loading branch information
Bre77 and bdraco authored Apr 17, 2023
1 parent 8fe9008 commit 58ea657
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 53 deletions.
3 changes: 2 additions & 1 deletion homeassistant/components/advantage_air/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import ADVANTAGE_AIR_RETRY, DOMAIN
from .models import AdvantageAirData

ADVANTAGE_AIR_SYNC_INTERVAL = 15
PLATFORMS = [
Expand Down Expand Up @@ -55,7 +56,7 @@ async def async_get():
await coordinator.async_config_entry_first_refresh()

hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = {"coordinator": coordinator, "api": api}
hass.data[DOMAIN][entry.entry_id] = AdvantageAirData(coordinator, api)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

Expand Down
13 changes: 6 additions & 7 deletions homeassistant/components/advantage_air/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""Binary Sensor platform for Advantage Air integration."""
from __future__ import annotations

from typing import Any

from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
Expand All @@ -14,6 +12,7 @@

from .const import DOMAIN as ADVANTAGE_AIR_DOMAIN
from .entity import AdvantageAirAcEntity, AdvantageAirZoneEntity
from .models import AdvantageAirData

PARALLEL_UPDATES = 0

Expand All @@ -25,10 +24,10 @@ async def async_setup_entry(
) -> None:
"""Set up AdvantageAir Binary Sensor platform."""

instance = hass.data[ADVANTAGE_AIR_DOMAIN][config_entry.entry_id]
instance: AdvantageAirData = hass.data[ADVANTAGE_AIR_DOMAIN][config_entry.entry_id]

entities: list[BinarySensorEntity] = []
if aircons := instance["coordinator"].data.get("aircons"):
if aircons := instance.coordinator.data.get("aircons"):
for ac_key, ac_device in aircons.items():
entities.append(AdvantageAirFilter(instance, ac_key))
for zone_key, zone in ac_device["zones"].items():
Expand All @@ -48,7 +47,7 @@ class AdvantageAirFilter(AdvantageAirAcEntity, BinarySensorEntity):
_attr_entity_category = EntityCategory.DIAGNOSTIC
_attr_name = "Filter"

def __init__(self, instance: dict[str, Any], ac_key: str) -> None:
def __init__(self, instance: AdvantageAirData, ac_key: str) -> None:
"""Initialize an Advantage Air Filter sensor."""
super().__init__(instance, ac_key)
self._attr_unique_id += "-filter"
Expand All @@ -64,7 +63,7 @@ class AdvantageAirZoneMotion(AdvantageAirZoneEntity, BinarySensorEntity):

_attr_device_class = BinarySensorDeviceClass.MOTION

def __init__(self, instance: dict[str, Any], ac_key: str, zone_key: str) -> None:
def __init__(self, instance: AdvantageAirData, ac_key: str, zone_key: str) -> None:
"""Initialize an Advantage Air Zone Motion sensor."""
super().__init__(instance, ac_key, zone_key)
self._attr_name = f'{self._zone["name"]} motion'
Expand All @@ -82,7 +81,7 @@ class AdvantageAirZoneMyZone(AdvantageAirZoneEntity, BinarySensorEntity):
_attr_entity_registry_enabled_default = False
_attr_entity_category = EntityCategory.DIAGNOSTIC

def __init__(self, instance: dict[str, Any], ac_key: str, zone_key: str) -> None:
def __init__(self, instance: AdvantageAirData, ac_key: str, zone_key: str) -> None:
"""Initialize an Advantage Air Zone MyZone sensor."""
super().__init__(instance, ac_key, zone_key)
self._attr_name = f'{self._zone["name"]} myZone'
Expand Down
9 changes: 5 additions & 4 deletions homeassistant/components/advantage_air/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
DOMAIN as ADVANTAGE_AIR_DOMAIN,
)
from .entity import AdvantageAirAcEntity, AdvantageAirZoneEntity
from .models import AdvantageAirData

ADVANTAGE_AIR_HVAC_MODES = {
"heat": HVACMode.HEAT,
Expand Down Expand Up @@ -68,10 +69,10 @@ async def async_setup_entry(
) -> None:
"""Set up AdvantageAir climate platform."""

instance = hass.data[ADVANTAGE_AIR_DOMAIN][config_entry.entry_id]
instance: AdvantageAirData = hass.data[ADVANTAGE_AIR_DOMAIN][config_entry.entry_id]

entities: list[ClimateEntity] = []
if aircons := instance["coordinator"].data.get("aircons"):
if aircons := instance.coordinator.data.get("aircons"):
for ac_key, ac_device in aircons.items():
entities.append(AdvantageAirAC(instance, ac_key))
for zone_key, zone in ac_device["zones"].items():
Expand All @@ -90,7 +91,7 @@ class AdvantageAirAC(AdvantageAirAcEntity, ClimateEntity):
_attr_max_temp = 32
_attr_min_temp = 16

def __init__(self, instance: dict[str, Any], ac_key: str) -> None:
def __init__(self, instance: AdvantageAirData, ac_key: str) -> None:
"""Initialize an AdvantageAir AC unit."""
super().__init__(instance, ac_key)

Expand Down Expand Up @@ -210,7 +211,7 @@ class AdvantageAirZone(AdvantageAirZoneEntity, ClimateEntity):
_attr_max_temp = 32
_attr_min_temp = 16

def __init__(self, instance: dict[str, Any], ac_key: str, zone_key: str) -> None:
def __init__(self, instance: AdvantageAirData, ac_key: str, zone_key: str) -> None:
"""Initialize an AdvantageAir Zone control."""
super().__init__(instance, ac_key, zone_key)
self._attr_name = self._zone["name"]
Expand Down
11 changes: 6 additions & 5 deletions homeassistant/components/advantage_air/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
DOMAIN as ADVANTAGE_AIR_DOMAIN,
)
from .entity import AdvantageAirThingEntity, AdvantageAirZoneEntity
from .models import AdvantageAirData

PARALLEL_UPDATES = 0

Expand All @@ -28,16 +29,16 @@ async def async_setup_entry(
) -> None:
"""Set up AdvantageAir cover platform."""

instance = hass.data[ADVANTAGE_AIR_DOMAIN][config_entry.entry_id]
instance: AdvantageAirData = hass.data[ADVANTAGE_AIR_DOMAIN][config_entry.entry_id]

entities: list[CoverEntity] = []
if aircons := instance["coordinator"].data.get("aircons"):
if aircons := instance.coordinator.data.get("aircons"):
for ac_key, ac_device in aircons.items():
for zone_key, zone in ac_device["zones"].items():
# Only add zone vent controls when zone in vent control mode.
if zone["type"] == 0:
entities.append(AdvantageAirZoneVent(instance, ac_key, zone_key))
if things := instance["coordinator"].data.get("myThings"):
if things := instance.coordinator.data.get("myThings"):
for thing in things["things"].values():
if thing["channelDipState"] in [1, 2]: # 1 = "Blind", 2 = "Blind 2"
entities.append(
Expand All @@ -60,7 +61,7 @@ class AdvantageAirZoneVent(AdvantageAirZoneEntity, CoverEntity):
| CoverEntityFeature.SET_POSITION
)

def __init__(self, instance: dict[str, Any], ac_key: str, zone_key: str) -> None:
def __init__(self, instance: AdvantageAirData, ac_key: str, zone_key: str) -> None:
"""Initialize an Advantage Air Zone Vent."""
super().__init__(instance, ac_key, zone_key)
self._attr_name = self._zone["name"]
Expand Down Expand Up @@ -108,7 +109,7 @@ class AdvantageAirThingCover(AdvantageAirThingEntity, CoverEntity):

def __init__(
self,
instance: dict[str, Any],
instance: AdvantageAirData,
thing: dict[str, Any],
device_class: CoverDeviceClass,
) -> None:
Expand Down
17 changes: 9 additions & 8 deletions homeassistant/components/advantage_air/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DOMAIN
from .models import AdvantageAirData


class AdvantageAirEntity(CoordinatorEntity):
"""Parent class for Advantage Air Entities."""

_attr_has_entity_name = True

def __init__(self, instance: dict[str, Any]) -> None:
def __init__(self, instance: AdvantageAirData) -> None:
"""Initialize common aspects of an Advantage Air entity."""
super().__init__(instance["coordinator"])
super().__init__(instance.coordinator)
self._attr_unique_id: str = self.coordinator.data["system"]["rid"]

def update_handle_factory(self, func, *keys):
Expand All @@ -39,7 +40,7 @@ async def update_handle(*values):
class AdvantageAirAcEntity(AdvantageAirEntity):
"""Parent class for Advantage Air AC Entities."""

def __init__(self, instance: dict[str, Any], ac_key: str) -> None:
def __init__(self, instance: AdvantageAirData, ac_key: str) -> None:
"""Initialize common aspects of an Advantage Air ac entity."""
super().__init__(instance)

Expand All @@ -54,7 +55,7 @@ def __init__(self, instance: dict[str, Any], ac_key: str) -> None:
name=self.coordinator.data["aircons"][self.ac_key]["info"]["name"],
)
self.async_update_ac = self.update_handle_factory(
instance["api"].aircon.async_update_ac, self.ac_key
instance.api.aircon.async_update_ac, self.ac_key
)

@property
Expand All @@ -65,14 +66,14 @@ def _ac(self) -> dict[str, Any]:
class AdvantageAirZoneEntity(AdvantageAirAcEntity):
"""Parent class for Advantage Air Zone Entities."""

def __init__(self, instance: dict[str, Any], ac_key: str, zone_key: str) -> None:
def __init__(self, instance: AdvantageAirData, ac_key: str, zone_key: str) -> None:
"""Initialize common aspects of an Advantage Air zone entity."""
super().__init__(instance, ac_key)

self.zone_key: str = zone_key
self._attr_unique_id += f"-{zone_key}"
self.async_update_zone = self.update_handle_factory(
instance["api"].aircon.async_update_zone, self.ac_key, self.zone_key
instance.api.aircon.async_update_zone, self.ac_key, self.zone_key
)

@property
Expand All @@ -83,7 +84,7 @@ def _zone(self) -> dict[str, Any]:
class AdvantageAirThingEntity(AdvantageAirEntity):
"""Parent class for Advantage Air Things Entities."""

def __init__(self, instance: dict[str, Any], thing: dict[str, Any]) -> None:
def __init__(self, instance: AdvantageAirData, thing: dict[str, Any]) -> None:
"""Initialize common aspects of an Advantage Air Things entity."""
super().__init__(instance)

Expand All @@ -98,7 +99,7 @@ def __init__(self, instance: dict[str, Any], thing: dict[str, Any]) -> None:
name=thing["name"],
)
self.async_update_value = self.update_handle_factory(
instance["api"].things.async_update_value, self._id
instance.api.things.async_update_value, self._id
)

@property
Expand Down
15 changes: 8 additions & 7 deletions homeassistant/components/advantage_air/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from .const import ADVANTAGE_AIR_STATE_ON, DOMAIN as ADVANTAGE_AIR_DOMAIN
from .entity import AdvantageAirEntity, AdvantageAirThingEntity
from .models import AdvantageAirData


async def async_setup_entry(
Expand All @@ -18,16 +19,16 @@ async def async_setup_entry(
) -> None:
"""Set up AdvantageAir light platform."""

instance = hass.data[ADVANTAGE_AIR_DOMAIN][config_entry.entry_id]
instance: AdvantageAirData = hass.data[ADVANTAGE_AIR_DOMAIN][config_entry.entry_id]

entities: list[LightEntity] = []
if my_lights := instance["coordinator"].data.get("myLights"):
if my_lights := instance.coordinator.data.get("myLights"):
for light in my_lights["lights"].values():
if light.get("relay"):
entities.append(AdvantageAirLight(instance, light))
else:
entities.append(AdvantageAirLightDimmable(instance, light))
if things := instance["coordinator"].data.get("myThings"):
if things := instance.coordinator.data.get("myThings"):
for thing in things["things"].values():
if thing["channelDipState"] == 4: # 4 = "Light (on/off)""
entities.append(AdvantageAirThingLight(instance, thing))
Expand All @@ -41,7 +42,7 @@ class AdvantageAirLight(AdvantageAirEntity, LightEntity):

_attr_supported_color_modes = {ColorMode.ONOFF}

def __init__(self, instance: dict[str, Any], light: dict[str, Any]) -> None:
def __init__(self, instance: AdvantageAirData, light: dict[str, Any]) -> None:
"""Initialize an Advantage Air Light."""
super().__init__(instance)

Expand All @@ -55,7 +56,7 @@ def __init__(self, instance: dict[str, Any], light: dict[str, Any]) -> None:
name=light["name"],
)
self.async_update_state = self.update_handle_factory(
instance["api"].lights.async_update_state, self._id
instance.api.lights.async_update_state, self._id
)

@property
Expand All @@ -82,11 +83,11 @@ class AdvantageAirLightDimmable(AdvantageAirLight):

_attr_supported_color_modes = {ColorMode.ONOFF, ColorMode.BRIGHTNESS}

def __init__(self, instance: dict[str, Any], light: dict[str, Any]) -> None:
def __init__(self, instance: AdvantageAirData, light: dict[str, Any]) -> None:
"""Initialize an Advantage Air Dimmable Light."""
super().__init__(instance, light)
self.async_update_value = self.update_handle_factory(
instance["api"].lights.async_update_value, self._id
instance.api.lights.async_update_value, self._id
)

@property
Expand Down
16 changes: 16 additions & 0 deletions homeassistant/components/advantage_air/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""The Advantage Air integration models."""
from __future__ import annotations

from dataclasses import dataclass

from advantage_air import advantage_air

from homeassistant.helpers.update_coordinator import DataUpdateCoordinator


@dataclass
class AdvantageAirData:
"""Data for the Advantage Air integration."""

coordinator: DataUpdateCoordinator
api: advantage_air
14 changes: 6 additions & 8 deletions homeassistant/components/advantage_air/select.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Select platform for Advantage Air integration."""
from typing import Any

from homeassistant.components.select import SelectEntity
from homeassistant.config_entries import ConfigEntry
Expand All @@ -8,6 +7,7 @@

from .const import DOMAIN as ADVANTAGE_AIR_DOMAIN
from .entity import AdvantageAirAcEntity
from .models import AdvantageAirData

ADVANTAGE_AIR_INACTIVE = "Inactive"

Expand All @@ -19,10 +19,10 @@ async def async_setup_entry(
) -> None:
"""Set up AdvantageAir select platform."""

instance = hass.data[ADVANTAGE_AIR_DOMAIN][config_entry.entry_id]
instance: AdvantageAirData = hass.data[ADVANTAGE_AIR_DOMAIN][config_entry.entry_id]

entities: list[SelectEntity] = []
if aircons := instance["coordinator"].data.get("aircons"):
if aircons := instance.coordinator.data.get("aircons"):
for ac_key in aircons:
entities.append(AdvantageAirMyZone(instance, ac_key))
async_add_entities(entities)
Expand All @@ -34,18 +34,16 @@ class AdvantageAirMyZone(AdvantageAirAcEntity, SelectEntity):
_attr_icon = "mdi:home-thermometer"
_attr_name = "MyZone"

def __init__(self, instance: dict[str, Any], ac_key: str) -> None:
def __init__(self, instance: AdvantageAirData, ac_key: str) -> None:
"""Initialize an Advantage Air MyZone control."""
super().__init__(instance, ac_key)
self._attr_unique_id += "-myzone"
self._attr_options = [ADVANTAGE_AIR_INACTIVE]
self._number_to_name = {0: ADVANTAGE_AIR_INACTIVE}
self._name_to_number = {ADVANTAGE_AIR_INACTIVE: 0}

if "aircons" in instance["coordinator"].data:
for zone in (
instance["coordinator"].data["aircons"][ac_key]["zones"].values()
):
if "aircons" in instance.coordinator.data:
for zone in instance.coordinator.data["aircons"][ac_key]["zones"].values():
if zone["type"] > 0:
self._name_to_number[zone["name"]] = zone["number"]
self._number_to_name[zone["number"]] = zone["name"]
Expand Down
Loading

0 comments on commit 58ea657

Please sign in to comment.