Use shorthand attrs in iaqualink (#100281)

* Use shorthand attrs in iaqualink

* Use super

* Update homeassistant/components/iaqualink/light.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

* Remove self

* More follow ups

* Remove cast and type check

* Update homeassistant/components/iaqualink/__init__.py

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

---------

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
Jan Bouwhuis 2023-09-13 16:34:14 +02:00 committed by GitHub
parent c3a7aee48e
commit f2f45380a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 117 deletions

View file

@ -6,7 +6,7 @@ from collections.abc import Awaitable, Callable, Coroutine
from datetime import datetime from datetime import datetime
from functools import wraps from functools import wraps
import logging import logging
from typing import Any, Concatenate, ParamSpec, TypeVar, cast from typing import Any, Concatenate, ParamSpec, TypeVar
import httpx import httpx
from iaqualink.client import AqualinkClient from iaqualink.client import AqualinkClient
@ -215,6 +215,14 @@ class AqualinkEntity(Entity):
def __init__(self, dev: AqualinkDevice) -> None: def __init__(self, dev: AqualinkDevice) -> None:
"""Initialize the entity.""" """Initialize the entity."""
self.dev = dev self.dev = dev
self._attr_unique_id = f"{dev.system.serial}_{dev.name}"
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self._attr_unique_id)},
manufacturer=dev.manufacturer,
model=dev.model,
name=dev.label,
via_device=(DOMAIN, dev.system.serial),
)
async def async_added_to_hass(self) -> None: async def async_added_to_hass(self) -> None:
"""Set up a listener when this entity is added to HA.""" """Set up a listener when this entity is added to HA."""
@ -222,11 +230,6 @@ class AqualinkEntity(Entity):
async_dispatcher_connect(self.hass, DOMAIN, self.async_write_ha_state) async_dispatcher_connect(self.hass, DOMAIN, self.async_write_ha_state)
) )
@property
def unique_id(self) -> str:
"""Return a unique identifier for this entity."""
return f"{self.dev.system.serial}_{self.dev.name}"
@property @property
def assumed_state(self) -> bool: def assumed_state(self) -> bool:
"""Return whether the state is based on actual reading from the device.""" """Return whether the state is based on actual reading from the device."""
@ -236,16 +239,3 @@ class AqualinkEntity(Entity):
def available(self) -> bool: def available(self) -> bool:
"""Return whether the device is available or not.""" """Return whether the device is available or not."""
return self.dev.system.online is True return self.dev.system.online is True
@property
def device_info(self) -> DeviceInfo:
"""Return the device info."""
return DeviceInfo(
identifiers={(DOMAIN, self.unique_id)},
manufacturer=self.dev.manufacturer,
model=self.dev.model,
# Instead of setting the device name to the entity name, iaqualink
# should be updated to set has_entity_name = True
name=cast(str | None, self.name),
via_device=(DOMAIN, self.dev.system.serial),
)

View file

@ -1,6 +1,8 @@
"""Support for Aqualink temperature sensors.""" """Support for Aqualink temperature sensors."""
from __future__ import annotations from __future__ import annotations
from iaqualink.device import AqualinkBinarySensor
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
DOMAIN, DOMAIN,
BinarySensorDeviceClass, BinarySensorDeviceClass,
@ -31,19 +33,14 @@ async def async_setup_entry(
class HassAqualinkBinarySensor(AqualinkEntity, BinarySensorEntity): class HassAqualinkBinarySensor(AqualinkEntity, BinarySensorEntity):
"""Representation of a binary sensor.""" """Representation of a binary sensor."""
@property def __init__(self, dev: AqualinkBinarySensor) -> None:
def name(self) -> str: """Initialize AquaLink binary sensor."""
"""Return the name of the binary sensor.""" super().__init__(dev)
return self.dev.label self._attr_name = dev.label
if dev.label == "Freeze Protection":
self._attr_device_class = BinarySensorDeviceClass.COLD
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return whether the binary sensor is on or not.""" """Return whether the binary sensor is on or not."""
return self.dev.is_on return self.dev.is_on
@property
def device_class(self) -> BinarySensorDeviceClass | None:
"""Return the class of the binary sensor."""
if self.name == "Freeze Protection":
return BinarySensorDeviceClass.COLD
return None

View file

@ -4,6 +4,8 @@ from __future__ import annotations
import logging import logging
from typing import Any from typing import Any
from iaqualink.device import AqualinkThermostat
from homeassistant.components.climate import ( from homeassistant.components.climate import (
DOMAIN as CLIMATE_DOMAIN, DOMAIN as CLIMATE_DOMAIN,
ClimateEntity, ClimateEntity,
@ -42,10 +44,17 @@ class HassAqualinkThermostat(AqualinkEntity, ClimateEntity):
_attr_hvac_modes = [HVACMode.HEAT, HVACMode.OFF] _attr_hvac_modes = [HVACMode.HEAT, HVACMode.OFF]
_attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE _attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE
@property def __init__(self, dev: AqualinkThermostat) -> None:
def name(self) -> str: """Initialize AquaLink thermostat."""
"""Return the name of the thermostat.""" super().__init__(dev)
return self.dev.label.split(" ")[0] self._attr_name = dev.label.split(" ")[0]
self._attr_temperature_unit = (
UnitOfTemperature.FAHRENHEIT
if dev.unit == "F"
else UnitOfTemperature.CELSIUS
)
self._attr_min_temp = dev.min_temperature
self._attr_max_temp = dev.max_temperature
@property @property
def hvac_mode(self) -> HVACMode: def hvac_mode(self) -> HVACMode:
@ -64,23 +73,6 @@ class HassAqualinkThermostat(AqualinkEntity, ClimateEntity):
else: else:
_LOGGER.warning("Unknown operation mode: %s", hvac_mode) _LOGGER.warning("Unknown operation mode: %s", hvac_mode)
@property
def temperature_unit(self) -> str:
"""Return the unit of measurement."""
if self.dev.unit == "F":
return UnitOfTemperature.FAHRENHEIT
return UnitOfTemperature.CELSIUS
@property
def min_temp(self) -> int:
"""Return the minimum temperature supported by the thermostat."""
return self.dev.min_temperature
@property
def max_temp(self) -> int:
"""Return the minimum temperature supported by the thermostat."""
return self.dev.max_temperature
@property @property
def target_temperature(self) -> float: def target_temperature(self) -> float:
"""Return the current target temperature.""" """Return the current target temperature."""

View file

@ -3,6 +3,8 @@ from __future__ import annotations
from typing import Any from typing import Any
from iaqualink.device import AqualinkLight
from homeassistant.components.light import ( from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS,
ATTR_EFFECT, ATTR_EFFECT,
@ -37,10 +39,18 @@ async def async_setup_entry(
class HassAqualinkLight(AqualinkEntity, LightEntity): class HassAqualinkLight(AqualinkEntity, LightEntity):
"""Representation of a light.""" """Representation of a light."""
@property def __init__(self, dev: AqualinkLight) -> None:
def name(self) -> str: """Initialize AquaLink light."""
"""Return the name of the light.""" super().__init__(dev)
return self.dev.label self._attr_name = dev.label
if dev.supports_effect:
self._attr_effect_list = list(dev.supported_effects)
self._attr_supported_features = LightEntityFeature.EFFECT
color_mode = ColorMode.ONOFF
if dev.supports_brightness:
color_mode = ColorMode.BRIGHTNESS
self._attr_color_mode = color_mode
self._attr_supported_color_modes = {color_mode}
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
@ -81,28 +91,3 @@ class HassAqualinkLight(AqualinkEntity, LightEntity):
def effect(self) -> str: def effect(self) -> str:
"""Return the current light effect if supported.""" """Return the current light effect if supported."""
return self.dev.effect return self.dev.effect
@property
def effect_list(self) -> list[str]:
"""Return supported light effects."""
return list(self.dev.supported_effects)
@property
def color_mode(self) -> ColorMode:
"""Return the color mode of the light."""
if self.dev.supports_brightness:
return ColorMode.BRIGHTNESS
return ColorMode.ONOFF
@property
def supported_color_modes(self) -> set[ColorMode]:
"""Flag supported color modes."""
return {self.color_mode}
@property
def supported_features(self) -> LightEntityFeature:
"""Return the list of features supported by the light."""
if self.dev.supports_effect:
return LightEntityFeature.EFFECT
return LightEntityFeature(0)

View file

@ -1,6 +1,8 @@
"""Support for Aqualink temperature sensors.""" """Support for Aqualink temperature sensors."""
from __future__ import annotations from __future__ import annotations
from iaqualink.device import AqualinkSensor
from homeassistant.components.sensor import DOMAIN, SensorDeviceClass, SensorEntity from homeassistant.components.sensor import DOMAIN, SensorDeviceClass, SensorEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfTemperature from homeassistant.const import UnitOfTemperature
@ -28,19 +30,17 @@ async def async_setup_entry(
class HassAqualinkSensor(AqualinkEntity, SensorEntity): class HassAqualinkSensor(AqualinkEntity, SensorEntity):
"""Representation of a sensor.""" """Representation of a sensor."""
@property def __init__(self, dev: AqualinkSensor) -> None:
def name(self) -> str: """Initialize AquaLink sensor."""
"""Return the name of the sensor.""" super().__init__(dev)
return self.dev.label self._attr_name = dev.label
if dev.name.endswith("_temp"):
@property self._attr_native_unit_of_measurement = (
def native_unit_of_measurement(self) -> str | None: UnitOfTemperature.FAHRENHEIT
"""Return the measurement unit for the sensor.""" if dev.system.temp_unit == "F"
if self.dev.name.endswith("_temp"): else UnitOfTemperature.CELSIUS
if self.dev.system.temp_unit == "F": )
return UnitOfTemperature.FAHRENHEIT self._attr_device_class = SensorDeviceClass.TEMPERATURE
return UnitOfTemperature.CELSIUS
return None
@property @property
def native_value(self) -> int | float | None: def native_value(self) -> int | float | None:
@ -52,10 +52,3 @@ class HassAqualinkSensor(AqualinkEntity, SensorEntity):
return int(self.dev.state) return int(self.dev.state)
except ValueError: except ValueError:
return float(self.dev.state) return float(self.dev.state)
@property
def device_class(self) -> SensorDeviceClass | None:
"""Return the class of the sensor."""
if self.dev.name.endswith("_temp"):
return SensorDeviceClass.TEMPERATURE
return None

View file

@ -3,6 +3,8 @@ from __future__ import annotations
from typing import Any from typing import Any
from iaqualink.device import AqualinkSwitch
from homeassistant.components.switch import DOMAIN, SwitchEntity from homeassistant.components.switch import DOMAIN, SwitchEntity
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -30,23 +32,18 @@ async def async_setup_entry(
class HassAqualinkSwitch(AqualinkEntity, SwitchEntity): class HassAqualinkSwitch(AqualinkEntity, SwitchEntity):
"""Representation of a switch.""" """Representation of a switch."""
@property def __init__(self, dev: AqualinkSwitch) -> None:
def name(self) -> str: """Initialize AquaLink switch."""
"""Return the name of the switch.""" super().__init__(dev)
return self.dev.label name = self._attr_name = dev.label
if name == "Cleaner":
@property self._attr_icon = "mdi:robot-vacuum"
def icon(self) -> str | None: elif name == "Waterfall" or name.endswith("Dscnt"):
"""Return an icon based on the switch type.""" self._attr_icon = "mdi:fountain"
if self.name == "Cleaner": elif name.endswith("Pump") or name.endswith("Blower"):
return "mdi:robot-vacuum" self._attr_icon = "mdi:fan"
if self.name == "Waterfall" or self.name.endswith("Dscnt"): if name.endswith("Heater"):
return "mdi:fountain" self._attr_icon = "mdi:radiator"
if self.name.endswith("Pump") or self.name.endswith("Blower"):
return "mdi:fan"
if self.name.endswith("Heater"):
return "mdi:radiator"
return None
@property @property
def is_on(self) -> bool: def is_on(self) -> bool: