From 02a7a2464a60c2d44dc6a8e66ca36579e8ddb93f Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Wed, 21 Jul 2021 05:33:44 -0400 Subject: [PATCH] Use entity class attributes for atag (#52686) --- homeassistant/components/atag/__init__.py | 19 ++-------- homeassistant/components/atag/climate.py | 27 ++++--------- homeassistant/components/atag/sensor.py | 38 +++++++------------ homeassistant/components/atag/water_heater.py | 17 ++------- 4 files changed, 29 insertions(+), 72 deletions(-) diff --git a/homeassistant/components/atag/__init__.py b/homeassistant/components/atag/__init__.py index af5eff67f57..de785a3a317 100644 --- a/homeassistant/components/atag/__init__.py +++ b/homeassistant/components/atag/__init__.py @@ -75,27 +75,16 @@ class AtagEntity(CoordinatorEntity): super().__init__(coordinator) self._id = atag_id - self._name = DOMAIN.title() + self._attr_name = DOMAIN.title() + self._attr_unique_id = f"{coordinator.data.id}-{atag_id}" @property def device_info(self) -> DeviceInfo: """Return info for device registry.""" - device = self.coordinator.data.id - version = self.coordinator.data.apiversion return { - "identifiers": {(DOMAIN, device)}, + "identifiers": {(DOMAIN, self.coordinator.data.id)}, "name": "Atag Thermostat", "model": "Atag One", - "sw_version": version, + "sw_version": self.coordinator.data.apiversion, "manufacturer": "Atag", } - - @property - def name(self) -> str: - """Return the name of the entity.""" - return self._name - - @property - def unique_id(self): - """Return a unique ID to use for this entity.""" - return f"{self.coordinator.data.id}-{self._id}" diff --git a/homeassistant/components/atag/climate.py b/homeassistant/components/atag/climate.py index da7e6a14a73..6bafd59ab82 100644 --- a/homeassistant/components/atag/climate.py +++ b/homeassistant/components/atag/climate.py @@ -37,10 +37,14 @@ async def async_setup_entry(hass, entry, async_add_entities): class AtagThermostat(AtagEntity, ClimateEntity): """Atag climate device.""" - @property - def supported_features(self): - """Return the list of supported features.""" - return SUPPORT_FLAGS + _attr_hvac_modes = HVAC_MODES + _attr_preset_modes = list(PRESET_MAP.keys()) + _attr_supported_features = SUPPORT_FLAGS + + def __init__(self, coordinator, atag_id): + """Initialize an Atag climate device.""" + super().__init__(coordinator, atag_id) + self._attr_temperature_unit = coordinator.data.climate.temp_unit @property def hvac_mode(self) -> str | None: @@ -49,22 +53,12 @@ class AtagThermostat(AtagEntity, ClimateEntity): return self.coordinator.data.climate.hvac_mode return None - @property - def hvac_modes(self) -> list[str]: - """Return the list of available hvac operation modes.""" - return HVAC_MODES - @property def hvac_action(self) -> str | None: """Return the current running hvac operation.""" is_active = self.coordinator.data.climate.status return CURRENT_HVAC_HEAT if is_active else CURRENT_HVAC_IDLE - @property - def temperature_unit(self) -> str | None: - """Return the unit of measurement.""" - return self.coordinator.data.climate.temp_unit - @property def current_temperature(self) -> float | None: """Return the current temperature.""" @@ -81,11 +75,6 @@ class AtagThermostat(AtagEntity, ClimateEntity): preset = self.coordinator.data.climate.preset_mode return PRESET_INVERTED.get(preset) - @property - def preset_modes(self) -> list[str] | None: - """Return a list of available preset modes.""" - return list(PRESET_MAP.keys()) - async def async_set_temperature(self, **kwargs) -> None: """Set new target temperature.""" await self.coordinator.data.climate.set_temp(kwargs.get(ATTR_TEMPERATURE)) diff --git a/homeassistant/components/atag/sensor.py b/homeassistant/components/atag/sensor.py index 88ccbdc899f..93164bd14bf 100644 --- a/homeassistant/components/atag/sensor.py +++ b/homeassistant/components/atag/sensor.py @@ -36,7 +36,20 @@ class AtagSensor(AtagEntity, SensorEntity): def __init__(self, coordinator, sensor): """Initialize Atag sensor.""" super().__init__(coordinator, SENSORS[sensor]) - self._name = sensor + self._attr_name = sensor + if coordinator.data.report[self._id].sensorclass in [ + DEVICE_CLASS_PRESSURE, + DEVICE_CLASS_TEMPERATURE, + ]: + self._attr_device_class = coordinator.data.report[self._id].sensorclass + if coordinator.data.report[self._id].measure in [ + PRESSURE_BAR, + TEMP_CELSIUS, + TEMP_FAHRENHEIT, + PERCENTAGE, + TIME_HOURS, + ]: + self._attr_unit_of_measurement = coordinator.data.report[self._id].measure @property def state(self): @@ -47,26 +60,3 @@ class AtagSensor(AtagEntity, SensorEntity): def icon(self): """Return icon.""" return self.coordinator.data.report[self._id].icon - - @property - def device_class(self): - """Return deviceclass.""" - if self.coordinator.data.report[self._id].sensorclass in [ - DEVICE_CLASS_PRESSURE, - DEVICE_CLASS_TEMPERATURE, - ]: - return self.coordinator.data.report[self._id].sensorclass - return None - - @property - def unit_of_measurement(self): - """Return measure.""" - if self.coordinator.data.report[self._id].measure in [ - PRESSURE_BAR, - TEMP_CELSIUS, - TEMP_FAHRENHEIT, - PERCENTAGE, - TIME_HOURS, - ]: - return self.coordinator.data.report[self._id].measure - return None diff --git a/homeassistant/components/atag/water_heater.py b/homeassistant/components/atag/water_heater.py index dac56edf89d..5fce2abf63e 100644 --- a/homeassistant/components/atag/water_heater.py +++ b/homeassistant/components/atag/water_heater.py @@ -22,15 +22,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities): class AtagWaterHeater(AtagEntity, WaterHeaterEntity): """Representation of an ATAG water heater.""" - @property - def supported_features(self): - """Return the list of supported features.""" - return SUPPORT_FLAGS_HEATER - - @property - def temperature_unit(self): - """Return the unit of measurement.""" - return TEMP_CELSIUS + _attr_operation_list = OPERATION_LIST + _attr_supported_features = SUPPORT_FLAGS_HEATER + _attr_temperature_unit = TEMP_CELSIUS @property def current_temperature(self): @@ -43,11 +37,6 @@ class AtagWaterHeater(AtagEntity, WaterHeaterEntity): operation = self.coordinator.data.dhw.current_operation return operation if operation in self.operation_list else STATE_OFF - @property - def operation_list(self): - """List of available operation modes.""" - return OPERATION_LIST - async def async_set_temperature(self, **kwargs): """Set new target temperature.""" if await self.coordinator.data.dhw.set_temp(kwargs.get(ATTR_TEMPERATURE)):