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>
pull/100305/head
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 functools import wraps
import logging
from typing import Any, Concatenate, ParamSpec, TypeVar, cast
from typing import Any, Concatenate, ParamSpec, TypeVar
import httpx
from iaqualink.client import AqualinkClient
@ -215,6 +215,14 @@ class AqualinkEntity(Entity):
def __init__(self, dev: AqualinkDevice) -> None:
"""Initialize the entity."""
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:
"""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)
)
@property
def unique_id(self) -> str:
"""Return a unique identifier for this entity."""
return f"{self.dev.system.serial}_{self.dev.name}"
@property
def assumed_state(self) -> bool:
"""Return whether the state is based on actual reading from the device."""
@ -236,16 +239,3 @@ class AqualinkEntity(Entity):
def available(self) -> bool:
"""Return whether the device is available or not."""
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."""
from __future__ import annotations
from iaqualink.device import AqualinkBinarySensor
from homeassistant.components.binary_sensor import (
DOMAIN,
BinarySensorDeviceClass,
@ -31,19 +33,14 @@ async def async_setup_entry(
class HassAqualinkBinarySensor(AqualinkEntity, BinarySensorEntity):
"""Representation of a binary sensor."""
@property
def name(self) -> str:
"""Return the name of the binary sensor."""
return self.dev.label
def __init__(self, dev: AqualinkBinarySensor) -> None:
"""Initialize AquaLink binary sensor."""
super().__init__(dev)
self._attr_name = dev.label
if dev.label == "Freeze Protection":
self._attr_device_class = BinarySensorDeviceClass.COLD
@property
def is_on(self) -> bool:
"""Return whether the binary sensor is on or not."""
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
from typing import Any
from iaqualink.device import AqualinkThermostat
from homeassistant.components.climate import (
DOMAIN as CLIMATE_DOMAIN,
ClimateEntity,
@ -42,10 +44,17 @@ class HassAqualinkThermostat(AqualinkEntity, ClimateEntity):
_attr_hvac_modes = [HVACMode.HEAT, HVACMode.OFF]
_attr_supported_features = ClimateEntityFeature.TARGET_TEMPERATURE
@property
def name(self) -> str:
"""Return the name of the thermostat."""
return self.dev.label.split(" ")[0]
def __init__(self, dev: AqualinkThermostat) -> None:
"""Initialize AquaLink thermostat."""
super().__init__(dev)
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
def hvac_mode(self) -> HVACMode:
@ -64,23 +73,6 @@ class HassAqualinkThermostat(AqualinkEntity, ClimateEntity):
else:
_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
def target_temperature(self) -> float:
"""Return the current target temperature."""

View File

@ -3,6 +3,8 @@ from __future__ import annotations
from typing import Any
from iaqualink.device import AqualinkLight
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_EFFECT,
@ -37,10 +39,18 @@ async def async_setup_entry(
class HassAqualinkLight(AqualinkEntity, LightEntity):
"""Representation of a light."""
@property
def name(self) -> str:
"""Return the name of the light."""
return self.dev.label
def __init__(self, dev: AqualinkLight) -> None:
"""Initialize AquaLink light."""
super().__init__(dev)
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
def is_on(self) -> bool:
@ -81,28 +91,3 @@ class HassAqualinkLight(AqualinkEntity, LightEntity):
def effect(self) -> str:
"""Return the current light effect if supported."""
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."""
from __future__ import annotations
from iaqualink.device import AqualinkSensor
from homeassistant.components.sensor import DOMAIN, SensorDeviceClass, SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfTemperature
@ -28,19 +30,17 @@ async def async_setup_entry(
class HassAqualinkSensor(AqualinkEntity, SensorEntity):
"""Representation of a sensor."""
@property
def name(self) -> str:
"""Return the name of the sensor."""
return self.dev.label
@property
def native_unit_of_measurement(self) -> str | None:
"""Return the measurement unit for the sensor."""
if self.dev.name.endswith("_temp"):
if self.dev.system.temp_unit == "F":
return UnitOfTemperature.FAHRENHEIT
return UnitOfTemperature.CELSIUS
return None
def __init__(self, dev: AqualinkSensor) -> None:
"""Initialize AquaLink sensor."""
super().__init__(dev)
self._attr_name = dev.label
if dev.name.endswith("_temp"):
self._attr_native_unit_of_measurement = (
UnitOfTemperature.FAHRENHEIT
if dev.system.temp_unit == "F"
else UnitOfTemperature.CELSIUS
)
self._attr_device_class = SensorDeviceClass.TEMPERATURE
@property
def native_value(self) -> int | float | None:
@ -52,10 +52,3 @@ class HassAqualinkSensor(AqualinkEntity, SensorEntity):
return int(self.dev.state)
except ValueError:
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 iaqualink.device import AqualinkSwitch
from homeassistant.components.switch import DOMAIN, SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
@ -30,23 +32,18 @@ async def async_setup_entry(
class HassAqualinkSwitch(AqualinkEntity, SwitchEntity):
"""Representation of a switch."""
@property
def name(self) -> str:
"""Return the name of the switch."""
return self.dev.label
@property
def icon(self) -> str | None:
"""Return an icon based on the switch type."""
if self.name == "Cleaner":
return "mdi:robot-vacuum"
if self.name == "Waterfall" or self.name.endswith("Dscnt"):
return "mdi:fountain"
if self.name.endswith("Pump") or self.name.endswith("Blower"):
return "mdi:fan"
if self.name.endswith("Heater"):
return "mdi:radiator"
return None
def __init__(self, dev: AqualinkSwitch) -> None:
"""Initialize AquaLink switch."""
super().__init__(dev)
name = self._attr_name = dev.label
if name == "Cleaner":
self._attr_icon = "mdi:robot-vacuum"
elif name == "Waterfall" or name.endswith("Dscnt"):
self._attr_icon = "mdi:fountain"
elif name.endswith("Pump") or name.endswith("Blower"):
self._attr_icon = "mdi:fan"
if name.endswith("Heater"):
self._attr_icon = "mdi:radiator"
@property
def is_on(self) -> bool: