Add unknown tuya modes as presets (#82056)

Fixes https://github.com/home-assistant/core/issues/81681
fixes undefined
This commit is contained in:
Rogelio Orts 2022-11-24 16:28:43 +01:00 committed by GitHub
parent e386bab682
commit bb517c269e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -25,7 +25,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import HomeAssistantTuyaData
from .base import IntegerTypeData, TuyaEntity
from .const import DOMAIN, LOGGER, TUYA_DISCOVERY_NEW, DPCode, DPType
from .const import DOMAIN, TUYA_DISCOVERY_NEW, DPCode, DPType
TUYA_HVAC_TO_HA = {
"auto": HVACMode.HEAT_COOL,
@ -205,10 +205,19 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity):
DPCode.MODE, dptype=DPType.ENUM, prefer_function=True
):
self._attr_hvac_modes = [HVACMode.OFF]
for tuya_mode, ha_mode in TUYA_HVAC_TO_HA.items():
if tuya_mode in enum_type.range:
unknown_hvac_modes: list[str] = []
for tuya_mode in enum_type.range:
if tuya_mode in TUYA_HVAC_TO_HA:
ha_mode = TUYA_HVAC_TO_HA[tuya_mode]
self._hvac_to_tuya[ha_mode] = tuya_mode
self._attr_hvac_modes.append(ha_mode)
else:
unknown_hvac_modes.append(tuya_mode)
if unknown_hvac_modes: # Tuya modes are presets instead of hvac_modes
self._attr_hvac_modes.append(description.switch_only_hvac_mode)
self._attr_preset_modes = unknown_hvac_modes
self._attr_supported_features |= ClimateEntityFeature.PRESET_MODE
elif self.find_dpcode(DPCode.SWITCH, prefer_function=True):
self._attr_hvac_modes = [
HVACMode.OFF,
@ -263,18 +272,6 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity):
"""Call when entity is added to hass."""
await super().async_added_to_hass()
# Log unknown modes
if enum_type := self.find_dpcode(
DPCode.MODE, dptype=DPType.ENUM, prefer_function=True
):
for tuya_mode in enum_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: HVACMode) -> None:
"""Set new target hvac mode."""
commands = [{"code": DPCode.SWITCH, "value": hvac_mode != HVACMode.OFF}]
@ -284,6 +281,11 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity):
)
self._send_command(commands)
def set_preset_mode(self, preset_mode):
"""Set new target preset mode."""
commands = [{"code": DPCode.MODE, "value": preset_mode}]
self._send_command(commands)
def set_fan_mode(self, fan_mode: str) -> None:
"""Set new target fan mode."""
self._send_command([{"code": DPCode.FAN_SPEED_ENUM, "value": fan_mode}])
@ -420,8 +422,24 @@ class TuyaClimateEntity(TuyaEntity, ClimateEntity):
) is not None and mode in TUYA_HVAC_TO_HA:
return TUYA_HVAC_TO_HA[mode]
# If the switch is on, and the mode does not match any hvac mode.
if self.device.status.get(DPCode.SWITCH, False):
return self.entity_description.switch_only_hvac_mode
return HVACMode.OFF
@property
def preset_mode(self) -> str | None:
"""Return preset mode."""
if DPCode.MODE not in self.device.function:
return None
mode = self.device.status.get(DPCode.MODE)
if mode in TUYA_HVAC_TO_HA:
return None
return mode
@property
def fan_mode(self) -> str | None:
"""Return fan mode."""