Gracefully handle unknown HVAC mode in Tuya (#62984)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Franck Nijhof 2021-12-29 14:52:08 +01:00 committed by GitHub
parent 85f2e259da
commit bd98fc231d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 3 deletions

View File

@ -32,7 +32,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import HomeAssistantTuyaData from . import HomeAssistantTuyaData
from .base import EnumTypeData, IntegerTypeData, TuyaEntity from .base import EnumTypeData, IntegerTypeData, TuyaEntity
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode from .const import DOMAIN, LOGGER, TUYA_DISCOVERY_NEW, DPCode
TUYA_HVAC_TO_HA = { TUYA_HVAC_TO_HA = {
"auto": HVAC_MODE_HEAT_COOL, "auto": HVAC_MODE_HEAT_COOL,
@ -298,6 +298,21 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity):
if DPCode.SWITCH_VERTICAL in device.function: if DPCode.SWITCH_VERTICAL in device.function:
self._attr_swing_modes.append(SWING_VERTICAL) self._attr_swing_modes.append(SWING_VERTICAL)
async def async_added_to_hass(self) -> None:
"""Call when entity is added to hass."""
await super().async_added_to_hass()
# Log unknown modes
if DPCode.MODE in self.device.function:
data_type = EnumTypeData.from_json(self.device.function[DPCode.MODE].values)
for tuya_mode in data_type.range:
if tuya_mode not in TUYA_HVAC_TO_HA:
LOGGER.warning(
"Unknown HVAC mode '%s' for device %s; assuming it as off",
tuya_mode,
self.device.name,
)
def set_hvac_mode(self, hvac_mode: str) -> None: def set_hvac_mode(self, hvac_mode: str) -> 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 != HVAC_MODE_OFF}]
@ -436,8 +451,11 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity):
return self.entity_description.switch_only_hvac_mode return self.entity_description.switch_only_hvac_mode
return HVAC_MODE_OFF return HVAC_MODE_OFF
if self.device.status.get(DPCode.MODE) is not None: if (
return TUYA_HVAC_TO_HA[self.device.status[DPCode.MODE]] mode := self.device.status.get(DPCode.MODE)
) is not None and mode in TUYA_HVAC_TO_HA:
return TUYA_HVAC_TO_HA[mode]
return HVAC_MODE_OFF return HVAC_MODE_OFF
@property @property

View File

@ -4,6 +4,7 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from dataclasses import dataclass, field from dataclasses import dataclass, field
from enum import Enum from enum import Enum
import logging
from tuya_iot import TuyaCloudOpenAPIEndpoint from tuya_iot import TuyaCloudOpenAPIEndpoint
@ -39,6 +40,7 @@ from homeassistant.const import (
) )
DOMAIN = "tuya" DOMAIN = "tuya"
LOGGER = logging.getLogger(__package__)
CONF_AUTH_TYPE = "auth_type" CONF_AUTH_TYPE = "auth_type"
CONF_PROJECT_TYPE = "tuya_project_type" CONF_PROJECT_TYPE = "tuya_project_type"