Use climate enums in lookin (#70681)

This commit is contained in:
epenet 2022-04-26 06:29:36 +02:00 committed by GitHub
parent d288c569fa
commit 6df53d7c1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,21 +7,17 @@ from typing import Any, Final, cast
from aiolookin import Climate, MeteoSensor from aiolookin import Climate, MeteoSensor
from aiolookin.models import UDPCommandType, UDPEvent from aiolookin.models import UDPCommandType, UDPEvent
from homeassistant.components.climate import ClimateEntity, ClimateEntityFeature from homeassistant.components.climate import ClimateEntity
from homeassistant.components.climate.const import ( from homeassistant.components.climate.const import (
ATTR_HVAC_MODE, ATTR_HVAC_MODE,
FAN_AUTO, FAN_AUTO,
FAN_HIGH, FAN_HIGH,
FAN_LOW, FAN_LOW,
FAN_MIDDLE, FAN_MIDDLE,
HVAC_MODE_AUTO,
HVAC_MODE_COOL,
HVAC_MODE_DRY,
HVAC_MODE_FAN_ONLY,
HVAC_MODE_HEAT,
HVAC_MODE_OFF,
SWING_BOTH, SWING_BOTH,
SWING_OFF, SWING_OFF,
ClimateEntityFeature,
HVACMode,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
@ -41,12 +37,12 @@ from .models import LookinData
LOOKIN_FAN_MODE_IDX_TO_HASS: Final = [FAN_AUTO, FAN_LOW, FAN_MIDDLE, FAN_HIGH] LOOKIN_FAN_MODE_IDX_TO_HASS: Final = [FAN_AUTO, FAN_LOW, FAN_MIDDLE, FAN_HIGH]
LOOKIN_SWING_MODE_IDX_TO_HASS: Final = [SWING_OFF, SWING_BOTH] LOOKIN_SWING_MODE_IDX_TO_HASS: Final = [SWING_OFF, SWING_BOTH]
LOOKIN_HVAC_MODE_IDX_TO_HASS: Final = [ LOOKIN_HVAC_MODE_IDX_TO_HASS: Final = [
HVAC_MODE_OFF, HVACMode.OFF,
HVAC_MODE_AUTO, HVACMode.AUTO,
HVAC_MODE_COOL, HVACMode.COOL,
HVAC_MODE_HEAT, HVACMode.HEAT,
HVAC_MODE_DRY, HVACMode.DRY,
HVAC_MODE_FAN_ONLY, HVACMode.FAN_ONLY,
] ]
HASS_TO_LOOKIN_HVAC_MODE: dict[str, int] = { HASS_TO_LOOKIN_HVAC_MODE: dict[str, int] = {
@ -104,7 +100,7 @@ class ConditionerEntity(LookinCoordinatorEntity, ClimateEntity):
) )
_attr_fan_modes: list[str] = LOOKIN_FAN_MODE_IDX_TO_HASS _attr_fan_modes: list[str] = LOOKIN_FAN_MODE_IDX_TO_HASS
_attr_swing_modes: list[str] = LOOKIN_SWING_MODE_IDX_TO_HASS _attr_swing_modes: list[str] = LOOKIN_SWING_MODE_IDX_TO_HASS
_attr_hvac_modes: list[str] = LOOKIN_HVAC_MODE_IDX_TO_HASS _attr_hvac_modes: list[HVACMode] = LOOKIN_HVAC_MODE_IDX_TO_HASS
_attr_min_temp = MIN_TEMP _attr_min_temp = MIN_TEMP
_attr_max_temp = MAX_TEMP _attr_max_temp = MAX_TEMP
_attr_target_temperature_step = PRECISION_WHOLE _attr_target_temperature_step = PRECISION_WHOLE
@ -124,7 +120,7 @@ class ConditionerEntity(LookinCoordinatorEntity, ClimateEntity):
def _climate(self) -> Climate: def _climate(self) -> Climate:
return cast(Climate, self.coordinator.data) return cast(Climate, self.coordinator.data)
async def async_set_hvac_mode(self, hvac_mode: str) -> None: async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set the hvac mode of the device.""" """Set the hvac mode of the device."""
if (mode := HASS_TO_LOOKIN_HVAC_MODE.get(hvac_mode)) is None: if (mode := HASS_TO_LOOKIN_HVAC_MODE.get(hvac_mode)) is None:
return return
@ -139,7 +135,7 @@ class ConditionerEntity(LookinCoordinatorEntity, ClimateEntity):
lookin_index = LOOKIN_HVAC_MODE_IDX_TO_HASS lookin_index = LOOKIN_HVAC_MODE_IDX_TO_HASS
if hvac_mode := kwargs.get(ATTR_HVAC_MODE): if hvac_mode := kwargs.get(ATTR_HVAC_MODE):
self._climate.hvac_mode = HASS_TO_LOOKIN_HVAC_MODE[hvac_mode] self._climate.hvac_mode = HASS_TO_LOOKIN_HVAC_MODE[hvac_mode]
elif self._climate.hvac_mode == lookin_index.index(HVAC_MODE_OFF): elif self._climate.hvac_mode == lookin_index.index(HVACMode.OFF):
# #
# If the device is off, and the user didn't specify an HVAC mode # If the device is off, and the user didn't specify an HVAC mode
# (which is the default when using the HA UI), the device won't turn # (which is the default when using the HA UI), the device won't turn
@ -152,11 +148,11 @@ class ConditionerEntity(LookinCoordinatorEntity, ClimateEntity):
# #
meteo_data: MeteoSensor = self._meteo_coordinator.data meteo_data: MeteoSensor = self._meteo_coordinator.data
if not (current_temp := meteo_data.temperature): if not (current_temp := meteo_data.temperature):
self._climate.hvac_mode = lookin_index.index(HVAC_MODE_AUTO) self._climate.hvac_mode = lookin_index.index(HVACMode.AUTO)
elif current_temp >= self._climate.temp_celsius: elif current_temp >= self._climate.temp_celsius:
self._climate.hvac_mode = lookin_index.index(HVAC_MODE_COOL) self._climate.hvac_mode = lookin_index.index(HVACMode.COOL)
else: else:
self._climate.hvac_mode = lookin_index.index(HVAC_MODE_HEAT) self._climate.hvac_mode = lookin_index.index(HVACMode.HEAT)
await self._async_update_conditioner() await self._async_update_conditioner()
async def async_set_fan_mode(self, fan_mode: str) -> None: async def async_set_fan_mode(self, fan_mode: str) -> None: