From 16e8373fdd5a96723ca1d82d10abcfc0ac027611 Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Sat, 24 Jul 2021 07:00:41 -0400 Subject: [PATCH] Use entity class attributes for advantage_air (#52498) * Use entity class attributes for advantage_air * update * tweak * tweak * use update listeners --- .../components/advantage_air/binary_sensor.py | 48 ++++---- .../components/advantage_air/climate.py | 112 ++++++------------ .../components/advantage_air/cover.py | 27 ++--- .../components/advantage_air/entity.py | 18 ++- .../components/advantage_air/sensor.py | 48 +++----- .../components/advantage_air/switch.py | 21 ++-- 6 files changed, 99 insertions(+), 175 deletions(-) diff --git a/homeassistant/components/advantage_air/binary_sensor.py b/homeassistant/components/advantage_air/binary_sensor.py index fba90148788..6a050e4086a 100644 --- a/homeassistant/components/advantage_air/binary_sensor.py +++ b/homeassistant/components/advantage_air/binary_sensor.py @@ -35,15 +35,13 @@ class AdvantageAirZoneFilter(AdvantageAirEntity, BinarySensorEntity): _attr_device_class = DEVICE_CLASS_PROBLEM - @property - def name(self): - """Return the name.""" - return f'{self._ac["name"]} Filter' - - @property - def unique_id(self): - """Return a unique id.""" - return f'{self.coordinator.data["system"]["rid"]}-{self.ac_key}-filter' + def __init__(self, instance, ac_key): + """Initialize an Advantage Air Filter.""" + super().__init__(instance, ac_key) + self._attr_name = f'{self._ac["name"]} Filter' + self._attr_unique_id = ( + f'{self.coordinator.data["system"]["rid"]}-{ac_key}-filter' + ) @property def is_on(self): @@ -56,15 +54,13 @@ class AdvantageAirZoneMotion(AdvantageAirEntity, BinarySensorEntity): _attr_device_class = DEVICE_CLASS_MOTION - @property - def name(self): - """Return the name.""" - return f'{self._zone["name"]} Motion' - - @property - def unique_id(self): - """Return a unique id.""" - return f'{self.coordinator.data["system"]["rid"]}-{self.ac_key}-{self.zone_key}-motion' + def __init__(self, instance, ac_key, zone_key): + """Initialize an Advantage Air Zone Motion.""" + super().__init__(instance, ac_key, zone_key) + self._attr_name = f'{self._zone["name"]} Motion' + self._attr_unique_id = ( + f'{self.coordinator.data["system"]["rid"]}-{ac_key}-{zone_key}-motion' + ) @property def is_on(self): @@ -77,15 +73,13 @@ class AdvantageAirZoneMyZone(AdvantageAirEntity, BinarySensorEntity): _attr_entity_registry_enabled_default = False - @property - def name(self): - """Return the name.""" - return f'{self._zone["name"]} MyZone' - - @property - def unique_id(self): - """Return a unique id.""" - return f'{self.coordinator.data["system"]["rid"]}-{self.ac_key}-{self.zone_key}-myzone' + def __init__(self, instance, ac_key, zone_key): + """Initialize an Advantage Air Zone MyZone.""" + super().__init__(instance, ac_key, zone_key) + self._attr_name = f'{self._zone["name"]} MyZone' + self._attr_unique_id = ( + f'{self.coordinator.data["system"]["rid"]}-{ac_key}-{zone_key}-myzone' + ) @property def is_on(self): diff --git a/homeassistant/components/advantage_air/climate.py b/homeassistant/components/advantage_air/climate.py index d890fa43207..1d377abc065 100644 --- a/homeassistant/components/advantage_air/climate.py +++ b/homeassistant/components/advantage_air/climate.py @@ -1,5 +1,4 @@ """Climate platform for Advantage Air integration.""" - from homeassistant.components.climate import ClimateEntity from homeassistant.components.climate.const import ( FAN_AUTO, @@ -16,6 +15,7 @@ from homeassistant.components.climate.const import ( SUPPORT_TARGET_TEMPERATURE, ) from homeassistant.const import ATTR_TEMPERATURE, PRECISION_WHOLE, TEMP_CELSIUS +from homeassistant.core import callback from homeassistant.helpers import entity_platform from .const import ( @@ -84,39 +84,26 @@ async def async_setup_entry(hass, config_entry, async_add_entities): class AdvantageAirClimateEntity(AdvantageAirEntity, ClimateEntity): """AdvantageAir Climate class.""" - @property - def temperature_unit(self): - """Return the temperature unit.""" - return TEMP_CELSIUS - - @property - def target_temperature_step(self): - """Return the supported temperature step.""" - return PRECISION_WHOLE - - @property - def max_temp(self): - """Return the maximum supported temperature.""" - return 32 - - @property - def min_temp(self): - """Return the minimum supported temperature.""" - return 16 + _attr_temperature_unit = TEMP_CELSIUS + _attr_target_temperature_step = PRECISION_WHOLE + _attr_max_temp = 32 + _attr_min_temp = 16 class AdvantageAirAC(AdvantageAirClimateEntity): """AdvantageAir AC unit.""" - @property - def name(self): - """Return the name.""" - return self._ac["name"] + _attr_fan_modes = [FAN_AUTO, FAN_LOW, FAN_MEDIUM, FAN_HIGH] + _attr_hvac_modes = AC_HVAC_MODES + _attr_supported_features = SUPPORT_TARGET_TEMPERATURE | SUPPORT_FAN_MODE - @property - def unique_id(self): - """Return a unique id.""" - return f'{self.coordinator.data["system"]["rid"]}-{self.ac_key}' + def __init__(self, instance, ac_key): + """Initialize an AdvantageAir AC unit.""" + super().__init__(instance, ac_key) + self._attr_name = self._ac["name"] + self._attr_unique_id = f'{self.coordinator.data["system"]["rid"]}-{ac_key}' + if self._ac.get("myAutoModeEnabled"): + self._attr_hvac_modes = AC_HVAC_MODES + [HVAC_MODE_AUTO] @property def target_temperature(self): @@ -130,28 +117,11 @@ class AdvantageAirAC(AdvantageAirClimateEntity): return ADVANTAGE_AIR_HVAC_MODES.get(self._ac["mode"]) return HVAC_MODE_OFF - @property - def hvac_modes(self): - """Return the supported HVAC modes.""" - if self._ac.get("myAutoModeEnabled"): - return AC_HVAC_MODES + [HVAC_MODE_AUTO] - return AC_HVAC_MODES - @property def fan_mode(self): """Return the current fan modes.""" return ADVANTAGE_AIR_FAN_MODES.get(self._ac["fan"]) - @property - def fan_modes(self): - """Return the supported fan modes.""" - return [FAN_AUTO, FAN_LOW, FAN_MEDIUM, FAN_HIGH] - - @property - def supported_features(self): - """Return the supported features.""" - return SUPPORT_TARGET_TEMPERATURE | SUPPORT_FAN_MODE - async def async_set_hvac_mode(self, hvac_mode): """Set the HVAC Mode and State.""" if hvac_mode == HVAC_MODE_OFF: @@ -185,42 +155,30 @@ class AdvantageAirAC(AdvantageAirClimateEntity): class AdvantageAirZone(AdvantageAirClimateEntity): """AdvantageAir Zone control.""" - @property - def name(self): - """Return the name.""" - return self._zone["name"] + _attr_hvac_modes = ZONE_HVAC_MODES + _attr_supported_features = SUPPORT_TARGET_TEMPERATURE - @property - def unique_id(self): - """Return a unique id.""" - return f'{self.coordinator.data["system"]["rid"]}-{self.ac_key}-{self.zone_key}' + def __init__(self, instance, ac_key, zone_key): + """Initialize an AdvantageAir Zone control.""" + super().__init__(instance, ac_key, zone_key) + self._attr_name = self._zone["name"] + self._attr_unique_id = ( + f'{self.coordinator.data["system"]["rid"]}-{ac_key}-{zone_key}' + ) - @property - def current_temperature(self): - """Return the current temperature.""" - return self._zone["measuredTemp"] + async def async_added_to_hass(self): + """When entity is added to hass.""" + self.async_on_remove(self.coordinator.async_add_listener(self._update_callback)) - @property - def target_temperature(self): - """Return the target temperature.""" - return self._zone["setTemp"] - - @property - def hvac_mode(self): - """Return the current HVAC modes.""" + @callback + def _update_callback(self) -> None: + """Load data from integration.""" + self._attr_current_temperature = self._zone["measuredTemp"] + self._attr_target_temperature = self._zone["setTemp"] + self._attr_hvac_mode = HVAC_MODE_OFF if self._zone["state"] == ADVANTAGE_AIR_STATE_OPEN: - return HVAC_MODE_FAN_ONLY - return HVAC_MODE_OFF - - @property - def hvac_modes(self): - """Return supported HVAC modes.""" - return ZONE_HVAC_MODES - - @property - def supported_features(self): - """Return the supported features.""" - return SUPPORT_TARGET_TEMPERATURE + self._attr_hvac_mode = HVAC_MODE_FAN_ONLY + self.async_write_ha_state() async def async_set_hvac_mode(self, hvac_mode): """Set the HVAC Mode and State.""" diff --git a/homeassistant/components/advantage_air/cover.py b/homeassistant/components/advantage_air/cover.py index 69d66849cd6..04960dab002 100644 --- a/homeassistant/components/advantage_air/cover.py +++ b/homeassistant/components/advantage_air/cover.py @@ -36,25 +36,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities): class AdvantageAirZoneVent(AdvantageAirEntity, CoverEntity): """Advantage Air Cover Class.""" - @property - def name(self): - """Return the name.""" - return f'{self._zone["name"]}' + _attr_device_class = DEVICE_CLASS_DAMPER + _attr_supported_features = SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_SET_POSITION - @property - def unique_id(self): - """Return a unique id.""" - return f'{self.coordinator.data["system"]["rid"]}-{self.ac_key}-{self.zone_key}' - - @property - def device_class(self): - """Return the device class of the vent.""" - return DEVICE_CLASS_DAMPER - - @property - def supported_features(self): - """Return the supported features.""" - return SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_SET_POSITION + def __init__(self, instance, ac_key, zone_key): + """Initialize an Advantage Air Cover Class.""" + super().__init__(instance, ac_key, zone_key) + self._attr_name = f'{self._zone["name"]}' + self._attr_unique_id = ( + f'{self.coordinator.data["system"]["rid"]}-{ac_key}-{zone_key}' + ) @property def is_closed(self): diff --git a/homeassistant/components/advantage_air/entity.py b/homeassistant/components/advantage_air/entity.py index ea20368c10f..ffb75e78c01 100644 --- a/homeassistant/components/advantage_air/entity.py +++ b/homeassistant/components/advantage_air/entity.py @@ -14,6 +14,13 @@ class AdvantageAirEntity(CoordinatorEntity): self.async_change = instance["async_change"] self.ac_key = ac_key self.zone_key = zone_key + self._attr_device_info = { + "identifiers": {(DOMAIN, self.coordinator.data["system"]["rid"])}, + "name": self.coordinator.data["system"]["name"], + "manufacturer": "Advantage Air", + "model": self.coordinator.data["system"]["sysType"], + "sw_version": self.coordinator.data["system"]["myAppRev"], + } @property def _ac(self): @@ -22,14 +29,3 @@ class AdvantageAirEntity(CoordinatorEntity): @property def _zone(self): return self.coordinator.data["aircons"][self.ac_key]["zones"][self.zone_key] - - @property - def device_info(self): - """Return parent device information.""" - return { - "identifiers": {(DOMAIN, self.coordinator.data["system"]["rid"])}, - "name": self.coordinator.data["system"]["name"], - "manufacturer": "Advantage Air", - "model": self.coordinator.data["system"]["sysType"], - "sw_version": self.coordinator.data["system"]["myAppRev"], - } diff --git a/homeassistant/components/advantage_air/sensor.py b/homeassistant/components/advantage_air/sensor.py index e2bf90e73c3..eca7651d6eb 100644 --- a/homeassistant/components/advantage_air/sensor.py +++ b/homeassistant/components/advantage_air/sensor.py @@ -51,17 +51,11 @@ class AdvantageAirTimeTo(AdvantageAirEntity, SensorEntity): """Initialize the Advantage Air timer control.""" super().__init__(instance, ac_key) self.action = action - self._time_key = f"countDownTo{self.action}" - - @property - def name(self): - """Return the name.""" - return f'{self._ac["name"]} Time To {self.action}' - - @property - def unique_id(self): - """Return a unique id.""" - return f'{self.coordinator.data["system"]["rid"]}-{self.ac_key}-timeto{self.action}' + self._time_key = f"countDownTo{action}" + self._attr_name = f'{self._ac["name"]} Time To {action}' + self._attr_unique_id = ( + f'{self.coordinator.data["system"]["rid"]}-{self.ac_key}-timeto{action}' + ) @property def state(self): @@ -87,15 +81,13 @@ class AdvantageAirZoneVent(AdvantageAirEntity, SensorEntity): _attr_unit_of_measurement = PERCENTAGE _attr_state_class = STATE_CLASS_MEASUREMENT - @property - def name(self): - """Return the name.""" - return f'{self._zone["name"]} Vent' - - @property - def unique_id(self): - """Return a unique id.""" - return f'{self.coordinator.data["system"]["rid"]}-{self.ac_key}-{self.zone_key}-vent' + def __init__(self, instance, ac_key, zone_key): + """Initialize an Advantage Air Zone Vent Sensor.""" + super().__init__(instance, ac_key, zone_key=zone_key) + self._attr_name = f'{self._zone["name"]} Vent' + self._attr_unique_id = ( + f'{self.coordinator.data["system"]["rid"]}-{ac_key}-{zone_key}-vent' + ) @property def state(self): @@ -118,15 +110,13 @@ class AdvantageAirZoneSignal(AdvantageAirEntity, SensorEntity): _attr_unit_of_measurement = PERCENTAGE _attr_state_class = STATE_CLASS_MEASUREMENT - @property - def name(self): - """Return the name.""" - return f'{self._zone["name"]} Signal' - - @property - def unique_id(self): - """Return a unique id.""" - return f'{self.coordinator.data["system"]["rid"]}-{self.ac_key}-{self.zone_key}-signal' + def __init__(self, instance, ac_key, zone_key): + """Initialize an Advantage Air Zone wireless signal sensor.""" + super().__init__(instance, ac_key, zone_key=zone_key) + self._attr_name = f'{self._zone["name"]} Signal' + self._attr_unique_id = ( + f'{self.coordinator.data["system"]["rid"]}-{ac_key}-{zone_key}-signal' + ) @property def state(self): diff --git a/homeassistant/components/advantage_air/switch.py b/homeassistant/components/advantage_air/switch.py index 6c687c1427e..1a44973f8c1 100644 --- a/homeassistant/components/advantage_air/switch.py +++ b/homeassistant/components/advantage_air/switch.py @@ -25,26 +25,21 @@ async def async_setup_entry(hass, config_entry, async_add_entities): class AdvantageAirFreshAir(AdvantageAirEntity, ToggleEntity): """Representation of Advantage Air fresh air control.""" - @property - def name(self): - """Return the name.""" - return f'{self._ac["name"]} Fresh Air' + _attr_icon = "mdi:air-filter" - @property - def unique_id(self): - """Return a unique id.""" - return f'{self.coordinator.data["system"]["rid"]}-{self.ac_key}-freshair' + def __init__(self, instance, ac_key): + """Initialize an Advantage Air fresh air control.""" + super().__init__(instance, ac_key) + self._attr_name = f'{self._ac["name"]} Fresh Air' + self._attr_unique_id = ( + f'{self.coordinator.data["system"]["rid"]}-{ac_key}-freshair' + ) @property def is_on(self): """Return the fresh air status.""" return self._ac["freshAirStatus"] == ADVANTAGE_AIR_STATE_ON - @property - def icon(self): - """Return a representative icon of the fresh air switch.""" - return "mdi:air-filter" - async def async_turn_on(self, **kwargs): """Turn fresh air on.""" await self.async_change(