Improve type hints in water_heater (#76910)

* Improve type hints in water_heater

* Adjust melcloud
This commit is contained in:
epenet 2022-08-17 16:58:17 +02:00 committed by GitHub
parent 8619df5294
commit 673a72503d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 23 deletions

View File

@ -9,6 +9,8 @@ from pymelcloud.atw_device import (
from pymelcloud.device import PROPERTY_POWER from pymelcloud.device import PROPERTY_POWER
from homeassistant.components.water_heater import ( from homeassistant.components.water_heater import (
DEFAULT_MAX_TEMP,
DEFAULT_MIN_TEMP,
WaterHeaterEntity, WaterHeaterEntity,
WaterHeaterEntityFeature, WaterHeaterEntityFeature,
) )
@ -122,11 +124,11 @@ class AtwWaterHeater(WaterHeaterEntity):
await self._device.set({PROPERTY_OPERATION_MODE: operation_mode}) await self._device.set({PROPERTY_OPERATION_MODE: operation_mode})
@property @property
def min_temp(self) -> float | None: def min_temp(self) -> float:
"""Return the minimum temperature.""" """Return the minimum temperature."""
return self._device.target_tank_temperature_min return self._device.target_tank_temperature_min or DEFAULT_MIN_TEMP
@property @property
def max_temp(self) -> float | None: def max_temp(self) -> float:
"""Return the maximum temperature.""" """Return the maximum temperature."""
return self._device.target_tank_temperature_max return self._device.target_tank_temperature_max or DEFAULT_MAX_TEMP

View File

@ -1,12 +1,13 @@
"""Support for water heater devices.""" """Support for water heater devices."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Mapping
from dataclasses import dataclass from dataclasses import dataclass
from datetime import timedelta from datetime import timedelta
from enum import IntEnum from enum import IntEnum
import functools as ft import functools as ft
import logging import logging
from typing import final from typing import Any, final
import voluptuous as vol import voluptuous as vol
@ -23,7 +24,7 @@ from homeassistant.const import (
TEMP_CELSIUS, TEMP_CELSIUS,
TEMP_FAHRENHEIT, TEMP_FAHRENHEIT,
) )
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant, ServiceCall
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.config_validation import ( # noqa: F401 from homeassistant.helpers.config_validation import ( # noqa: F401
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
@ -188,11 +189,11 @@ class WaterHeaterEntity(Entity):
return PRECISION_WHOLE return PRECISION_WHOLE
@property @property
def capability_attributes(self): def capability_attributes(self) -> Mapping[str, Any]:
"""Return capability attributes.""" """Return capability attributes."""
supported_features = self.supported_features or 0 supported_features = self.supported_features or 0
data = { data: dict[str, Any] = {
ATTR_MIN_TEMP: show_temp( ATTR_MIN_TEMP: show_temp(
self.hass, self.min_temp, self.temperature_unit, self.precision self.hass, self.min_temp, self.temperature_unit, self.precision
), ),
@ -208,9 +209,9 @@ class WaterHeaterEntity(Entity):
@final @final
@property @property
def state_attributes(self): def state_attributes(self) -> dict[str, Any]:
"""Return the optional state attributes.""" """Return the optional state attributes."""
data = { data: dict[str, Any] = {
ATTR_CURRENT_TEMPERATURE: show_temp( ATTR_CURRENT_TEMPERATURE: show_temp(
self.hass, self.hass,
self.current_temperature, self.current_temperature,
@ -237,7 +238,7 @@ class WaterHeaterEntity(Entity):
), ),
} }
supported_features = self.supported_features supported_features = self.supported_features or 0
if supported_features & WaterHeaterEntityFeature.OPERATION_MODE: if supported_features & WaterHeaterEntityFeature.OPERATION_MODE:
data[ATTR_OPERATION_MODE] = self.current_operation data[ATTR_OPERATION_MODE] = self.current_operation
@ -288,42 +289,42 @@ class WaterHeaterEntity(Entity):
"""Return true if away mode is on.""" """Return true if away mode is on."""
return self._attr_is_away_mode_on return self._attr_is_away_mode_on
def set_temperature(self, **kwargs): def set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperature.""" """Set new target temperature."""
raise NotImplementedError() raise NotImplementedError()
async def async_set_temperature(self, **kwargs): async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set new target temperature.""" """Set new target temperature."""
await self.hass.async_add_executor_job( await self.hass.async_add_executor_job(
ft.partial(self.set_temperature, **kwargs) ft.partial(self.set_temperature, **kwargs)
) )
def set_operation_mode(self, operation_mode): def set_operation_mode(self, operation_mode: str) -> None:
"""Set new target operation mode.""" """Set new target operation mode."""
raise NotImplementedError() raise NotImplementedError()
async def async_set_operation_mode(self, operation_mode): async def async_set_operation_mode(self, operation_mode: str) -> None:
"""Set new target operation mode.""" """Set new target operation mode."""
await self.hass.async_add_executor_job(self.set_operation_mode, operation_mode) await self.hass.async_add_executor_job(self.set_operation_mode, operation_mode)
def turn_away_mode_on(self): def turn_away_mode_on(self) -> None:
"""Turn away mode on.""" """Turn away mode on."""
raise NotImplementedError() raise NotImplementedError()
async def async_turn_away_mode_on(self): async def async_turn_away_mode_on(self) -> None:
"""Turn away mode on.""" """Turn away mode on."""
await self.hass.async_add_executor_job(self.turn_away_mode_on) await self.hass.async_add_executor_job(self.turn_away_mode_on)
def turn_away_mode_off(self): def turn_away_mode_off(self) -> None:
"""Turn away mode off.""" """Turn away mode off."""
raise NotImplementedError() raise NotImplementedError()
async def async_turn_away_mode_off(self): async def async_turn_away_mode_off(self) -> None:
"""Turn away mode off.""" """Turn away mode off."""
await self.hass.async_add_executor_job(self.turn_away_mode_off) await self.hass.async_add_executor_job(self.turn_away_mode_off)
@property @property
def min_temp(self): def min_temp(self) -> float:
"""Return the minimum temperature.""" """Return the minimum temperature."""
if hasattr(self, "_attr_min_temp"): if hasattr(self, "_attr_min_temp"):
return self._attr_min_temp return self._attr_min_temp
@ -332,7 +333,7 @@ class WaterHeaterEntity(Entity):
) )
@property @property
def max_temp(self): def max_temp(self) -> float:
"""Return the maximum temperature.""" """Return the maximum temperature."""
if hasattr(self, "_attr_max_temp"): if hasattr(self, "_attr_max_temp"):
return self._attr_max_temp return self._attr_max_temp
@ -341,7 +342,9 @@ class WaterHeaterEntity(Entity):
) )
async def async_service_away_mode(entity, service): async def async_service_away_mode(
entity: WaterHeaterEntity, service: ServiceCall
) -> None:
"""Handle away mode service.""" """Handle away mode service."""
if service.data[ATTR_AWAY_MODE]: if service.data[ATTR_AWAY_MODE]:
await entity.async_turn_away_mode_on() await entity.async_turn_away_mode_on()
@ -349,7 +352,9 @@ async def async_service_away_mode(entity, service):
await entity.async_turn_away_mode_off() await entity.async_turn_away_mode_off()
async def async_service_temperature_set(entity, service): async def async_service_temperature_set(
entity: WaterHeaterEntity, service: ServiceCall
) -> None:
"""Handle set temperature service.""" """Handle set temperature service."""
hass = entity.hass hass = entity.hass
kwargs = {} kwargs = {}