Use climate enums in tuya (#70747)

This commit is contained in:
epenet 2022-04-26 08:39:41 +02:00 committed by GitHub
parent 7e85510912
commit e688f6b315
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,23 +6,15 @@ from typing import Any
from tuya_iot import TuyaDevice, TuyaDeviceManager from tuya_iot import TuyaDevice, TuyaDeviceManager
from homeassistant.components.climate import ( from homeassistant.components.climate import ClimateEntity, ClimateEntityDescription
ClimateEntity,
ClimateEntityDescription,
ClimateEntityFeature,
)
from homeassistant.components.climate.const import ( from homeassistant.components.climate.const import (
HVAC_MODE_COOL,
HVAC_MODE_DRY,
HVAC_MODE_FAN_ONLY,
HVAC_MODE_HEAT,
HVAC_MODE_HEAT_COOL,
HVAC_MODE_OFF,
SWING_BOTH, SWING_BOTH,
SWING_HORIZONTAL, SWING_HORIZONTAL,
SWING_OFF, SWING_OFF,
SWING_ON, SWING_ON,
SWING_VERTICAL, SWING_VERTICAL,
ClimateEntityFeature,
HVACMode,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT
@ -35,14 +27,14 @@ from .base import IntegerTypeData, TuyaEntity
from .const import DOMAIN, LOGGER, TUYA_DISCOVERY_NEW, DPCode, DPType from .const import DOMAIN, LOGGER, TUYA_DISCOVERY_NEW, DPCode, DPType
TUYA_HVAC_TO_HA = { TUYA_HVAC_TO_HA = {
"auto": HVAC_MODE_HEAT_COOL, "auto": HVACMode.HEAT_COOL,
"cold": HVAC_MODE_COOL, "cold": HVACMode.COOL,
"freeze": HVAC_MODE_COOL, "freeze": HVACMode.COOL,
"heat": HVAC_MODE_HEAT, "heat": HVACMode.HEAT,
"hot": HVAC_MODE_HEAT, "hot": HVACMode.HEAT,
"manual": HVAC_MODE_HEAT_COOL, "manual": HVACMode.HEAT_COOL,
"wet": HVAC_MODE_DRY, "wet": HVACMode.DRY,
"wind": HVAC_MODE_FAN_ONLY, "wind": HVACMode.FAN_ONLY,
} }
@ -50,7 +42,7 @@ TUYA_HVAC_TO_HA = {
class TuyaClimateSensorDescriptionMixin: class TuyaClimateSensorDescriptionMixin:
"""Define an entity description mixin for climate entities.""" """Define an entity description mixin for climate entities."""
switch_only_hvac_mode: str switch_only_hvac_mode: HVACMode
@dataclass @dataclass
@ -65,31 +57,31 @@ CLIMATE_DESCRIPTIONS: dict[str, TuyaClimateEntityDescription] = {
# https://developer.tuya.com/en/docs/iot/categorykt?id=Kaiuz0z71ov2n # https://developer.tuya.com/en/docs/iot/categorykt?id=Kaiuz0z71ov2n
"kt": TuyaClimateEntityDescription( "kt": TuyaClimateEntityDescription(
key="kt", key="kt",
switch_only_hvac_mode=HVAC_MODE_COOL, switch_only_hvac_mode=HVACMode.COOL,
), ),
# Heater # Heater
# https://developer.tuya.com/en/docs/iot/f?id=K9gf46epy4j82 # https://developer.tuya.com/en/docs/iot/f?id=K9gf46epy4j82
"qn": TuyaClimateEntityDescription( "qn": TuyaClimateEntityDescription(
key="qn", key="qn",
switch_only_hvac_mode=HVAC_MODE_HEAT, switch_only_hvac_mode=HVACMode.HEAT,
), ),
# Heater # Heater
# https://developer.tuya.com/en/docs/iot/categoryrs?id=Kaiuz0nfferyx # https://developer.tuya.com/en/docs/iot/categoryrs?id=Kaiuz0nfferyx
"rs": TuyaClimateEntityDescription( "rs": TuyaClimateEntityDescription(
key="rs", key="rs",
switch_only_hvac_mode=HVAC_MODE_HEAT, switch_only_hvac_mode=HVACMode.HEAT,
), ),
# Thermostat # Thermostat
# https://developer.tuya.com/en/docs/iot/f?id=K9gf45ld5l0t9 # https://developer.tuya.com/en/docs/iot/f?id=K9gf45ld5l0t9
"wk": TuyaClimateEntityDescription( "wk": TuyaClimateEntityDescription(
key="wk", key="wk",
switch_only_hvac_mode=HVAC_MODE_HEAT_COOL, switch_only_hvac_mode=HVACMode.HEAT_COOL,
), ),
# Thermostatic Radiator Valve # Thermostatic Radiator Valve
# Not documented # Not documented
"wkf": TuyaClimateEntityDescription( "wkf": TuyaClimateEntityDescription(
key="wkf", key="wkf",
switch_only_hvac_mode=HVAC_MODE_HEAT, switch_only_hvac_mode=HVACMode.HEAT,
), ),
} }
@ -212,14 +204,14 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity):
if enum_type := self.find_dpcode( if enum_type := self.find_dpcode(
DPCode.MODE, dptype=DPType.ENUM, prefer_function=True DPCode.MODE, dptype=DPType.ENUM, prefer_function=True
): ):
self._attr_hvac_modes = [HVAC_MODE_OFF] self._attr_hvac_modes = [HVACMode.OFF]
for tuya_mode, ha_mode in TUYA_HVAC_TO_HA.items(): for tuya_mode, ha_mode in TUYA_HVAC_TO_HA.items():
if tuya_mode in enum_type.range: if tuya_mode in enum_type.range:
self._hvac_to_tuya[ha_mode] = tuya_mode self._hvac_to_tuya[ha_mode] = tuya_mode
self._attr_hvac_modes.append(ha_mode) self._attr_hvac_modes.append(ha_mode)
elif self.find_dpcode(DPCode.SWITCH, prefer_function=True): elif self.find_dpcode(DPCode.SWITCH, prefer_function=True):
self._attr_hvac_modes = [ self._attr_hvac_modes = [
HVAC_MODE_OFF, HVACMode.OFF,
description.switch_only_hvac_mode, description.switch_only_hvac_mode,
] ]
@ -283,9 +275,9 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity):
self.device.name, self.device.name,
) )
def set_hvac_mode(self, hvac_mode: str) -> None: def set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set new target hvac mode.""" """Set new target hvac mode."""
commands = [{"code": DPCode.SWITCH, "value": hvac_mode != HVAC_MODE_OFF}] commands = [{"code": DPCode.SWITCH, "value": hvac_mode != HVACMode.OFF}]
if hvac_mode in self._hvac_to_tuya: if hvac_mode in self._hvac_to_tuya:
commands.append( commands.append(
{"code": DPCode.MODE, "value": self._hvac_to_tuya[hvac_mode]} {"code": DPCode.MODE, "value": self._hvac_to_tuya[hvac_mode]}
@ -411,24 +403,24 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity):
return round(self._set_humidity.scale_value(humidity)) return round(self._set_humidity.scale_value(humidity))
@property @property
def hvac_mode(self) -> str: def hvac_mode(self) -> HVACMode:
"""Return hvac mode.""" """Return hvac mode."""
# If the switch off, hvac mode is off as well. Unless the switch # If the switch off, hvac mode is off as well. Unless the switch
# the switch is on or doesn't exists of course... # the switch is on or doesn't exists of course...
if not self.device.status.get(DPCode.SWITCH, True): if not self.device.status.get(DPCode.SWITCH, True):
return HVAC_MODE_OFF return HVACMode.OFF
if DPCode.MODE not in self.device.function: if DPCode.MODE not in self.device.function:
if self.device.status.get(DPCode.SWITCH, False): if self.device.status.get(DPCode.SWITCH, False):
return self.entity_description.switch_only_hvac_mode return self.entity_description.switch_only_hvac_mode
return HVAC_MODE_OFF return HVACMode.OFF
if ( if (
mode := self.device.status.get(DPCode.MODE) mode := self.device.status.get(DPCode.MODE)
) is not None and mode in TUYA_HVAC_TO_HA: ) is not None and mode in TUYA_HVAC_TO_HA:
return TUYA_HVAC_TO_HA[mode] return TUYA_HVAC_TO_HA[mode]
return HVAC_MODE_OFF return HVACMode.OFF
@property @property
def fan_mode(self) -> str | None: def fan_mode(self) -> str | None:
@ -461,7 +453,7 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity):
return return
# Fake turn on # Fake turn on
for mode in (HVAC_MODE_HEAT_COOL, HVAC_MODE_HEAT, HVAC_MODE_COOL): for mode in (HVACMode.HEAT_COOL, HVACMode.HEAT, HVACMode.COOL):
if mode not in self.hvac_modes: if mode not in self.hvac_modes:
continue continue
self.set_hvac_mode(mode) self.set_hvac_mode(mode)
@ -474,5 +466,5 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity):
return return
# Fake turn off # Fake turn off
if HVAC_MODE_OFF in self.hvac_modes: if HVACMode.OFF in self.hvac_modes:
self.set_hvac_mode(HVAC_MODE_OFF) self.set_hvac_mode(HVACMode.OFF)