Use climate enums in vera (#70750)

This commit is contained in:
epenet 2022-04-26 09:25:24 +02:00 committed by GitHub
parent ae5ec1a320
commit 10905f6074
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 45 deletions

View File

@ -5,18 +5,12 @@ from typing import Any
import pyvera as veraApi import pyvera as veraApi
from homeassistant.components.climate import ( from homeassistant.components.climate import ENTITY_ID_FORMAT, ClimateEntity
ENTITY_ID_FORMAT,
ClimateEntity,
ClimateEntityFeature,
)
from homeassistant.components.climate.const import ( from homeassistant.components.climate.const import (
FAN_AUTO, FAN_AUTO,
FAN_ON, FAN_ON,
HVAC_MODE_COOL, ClimateEntityFeature,
HVAC_MODE_HEAT, HVACMode,
HVAC_MODE_HEAT_COOL,
HVAC_MODE_OFF,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
@ -33,7 +27,7 @@ from .common import ControllerData, get_controller_data
FAN_OPERATION_LIST = [FAN_ON, FAN_AUTO] FAN_OPERATION_LIST = [FAN_ON, FAN_AUTO]
SUPPORT_HVAC = [HVAC_MODE_COOL, HVAC_MODE_HEAT, HVAC_MODE_HEAT_COOL, HVAC_MODE_OFF] SUPPORT_HVAC = [HVACMode.COOL, HVACMode.HEAT, HVACMode.HEAT_COOL, HVACMode.OFF]
async def async_setup_entry( async def async_setup_entry(
@ -55,6 +49,7 @@ async def async_setup_entry(
class VeraThermostat(VeraDevice[veraApi.VeraThermostat], ClimateEntity): class VeraThermostat(VeraDevice[veraApi.VeraThermostat], ClimateEntity):
"""Representation of a Vera Thermostat.""" """Representation of a Vera Thermostat."""
_attr_hvac_modes = SUPPORT_HVAC
_attr_supported_features = ( _attr_supported_features = (
ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.FAN_MODE ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.FAN_MODE
) )
@ -67,27 +62,19 @@ class VeraThermostat(VeraDevice[veraApi.VeraThermostat], ClimateEntity):
self.entity_id = ENTITY_ID_FORMAT.format(self.vera_id) self.entity_id = ENTITY_ID_FORMAT.format(self.vera_id)
@property @property
def hvac_mode(self) -> str: def hvac_mode(self) -> HVACMode:
"""Return hvac operation ie. heat, cool mode. """Return hvac operation ie. heat, cool mode.
Need to be one of HVAC_MODE_*. Need to be one of HVAC_MODE_*.
""" """
mode = self.vera_device.get_hvac_mode() mode = self.vera_device.get_hvac_mode()
if mode == "HeatOn": if mode == "HeatOn":
return HVAC_MODE_HEAT return HVACMode.HEAT
if mode == "CoolOn": if mode == "CoolOn":
return HVAC_MODE_COOL return HVACMode.COOL
if mode == "AutoChangeOver": if mode == "AutoChangeOver":
return HVAC_MODE_HEAT_COOL return HVACMode.HEAT_COOL
return HVAC_MODE_OFF return HVACMode.OFF
@property
def hvac_modes(self) -> list[str]:
"""Return the list of available hvac operation modes.
Need to be a subset of HVAC_MODES.
"""
return SUPPORT_HVAC
@property @property
def fan_mode(self) -> str | None: def fan_mode(self) -> str | None:
@ -142,15 +129,15 @@ class VeraThermostat(VeraDevice[veraApi.VeraThermostat], ClimateEntity):
self.schedule_update_ha_state() self.schedule_update_ha_state()
def set_hvac_mode(self, hvac_mode) -> None: def set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set new target hvac mode.""" """Set new target hvac mode."""
if hvac_mode == HVAC_MODE_OFF: if hvac_mode == HVACMode.OFF:
self.vera_device.turn_off() self.vera_device.turn_off()
elif hvac_mode == HVAC_MODE_HEAT_COOL: elif hvac_mode == HVACMode.HEAT_COOL:
self.vera_device.turn_auto_on() self.vera_device.turn_auto_on()
elif hvac_mode == HVAC_MODE_COOL: elif hvac_mode == HVACMode.COOL:
self.vera_device.turn_cool_on() self.vera_device.turn_cool_on()
elif hvac_mode == HVAC_MODE_HEAT: elif hvac_mode == HVACMode.HEAT:
self.vera_device.turn_heat_on() self.vera_device.turn_heat_on()
self.schedule_update_ha_state() self.schedule_update_ha_state()

View File

@ -3,14 +3,7 @@ from unittest.mock import MagicMock
import pyvera as pv import pyvera as pv
from homeassistant.components.climate.const import ( from homeassistant.components.climate.const import FAN_AUTO, FAN_ON, HVACMode
FAN_AUTO,
FAN_ON,
HVAC_MODE_COOL,
HVAC_MODE_HEAT,
HVAC_MODE_HEAT_COOL,
HVAC_MODE_OFF,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from .common import ComponentFactory, new_simple_controller_config from .common import ComponentFactory, new_simple_controller_config
@ -38,55 +31,55 @@ async def test_climate(
) )
update_callback = component_data.controller_data[0].update_callback update_callback = component_data.controller_data[0].update_callback
assert hass.states.get(entity_id).state == HVAC_MODE_OFF assert hass.states.get(entity_id).state == HVACMode.OFF
await hass.services.async_call( await hass.services.async_call(
"climate", "climate",
"set_hvac_mode", "set_hvac_mode",
{"entity_id": entity_id, "hvac_mode": HVAC_MODE_COOL}, {"entity_id": entity_id, "hvac_mode": HVACMode.COOL},
) )
await hass.async_block_till_done() await hass.async_block_till_done()
vera_device.turn_cool_on.assert_called() vera_device.turn_cool_on.assert_called()
vera_device.get_hvac_mode.return_value = "CoolOn" vera_device.get_hvac_mode.return_value = "CoolOn"
update_callback(vera_device) update_callback(vera_device)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(entity_id).state == HVAC_MODE_COOL assert hass.states.get(entity_id).state == HVACMode.COOL
await hass.services.async_call( await hass.services.async_call(
"climate", "climate",
"set_hvac_mode", "set_hvac_mode",
{"entity_id": entity_id, "hvac_mode": HVAC_MODE_HEAT}, {"entity_id": entity_id, "hvac_mode": HVACMode.HEAT},
) )
await hass.async_block_till_done() await hass.async_block_till_done()
vera_device.turn_heat_on.assert_called() vera_device.turn_heat_on.assert_called()
vera_device.get_hvac_mode.return_value = "HeatOn" vera_device.get_hvac_mode.return_value = "HeatOn"
update_callback(vera_device) update_callback(vera_device)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(entity_id).state == HVAC_MODE_HEAT assert hass.states.get(entity_id).state == HVACMode.HEAT
await hass.services.async_call( await hass.services.async_call(
"climate", "climate",
"set_hvac_mode", "set_hvac_mode",
{"entity_id": entity_id, "hvac_mode": HVAC_MODE_HEAT_COOL}, {"entity_id": entity_id, "hvac_mode": HVACMode.HEAT_COOL},
) )
await hass.async_block_till_done() await hass.async_block_till_done()
vera_device.turn_auto_on.assert_called() vera_device.turn_auto_on.assert_called()
vera_device.get_hvac_mode.return_value = "AutoChangeOver" vera_device.get_hvac_mode.return_value = "AutoChangeOver"
update_callback(vera_device) update_callback(vera_device)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(entity_id).state == HVAC_MODE_HEAT_COOL assert hass.states.get(entity_id).state == HVACMode.HEAT_COOL
await hass.services.async_call( await hass.services.async_call(
"climate", "climate",
"set_hvac_mode", "set_hvac_mode",
{"entity_id": entity_id, "hvac_mode": HVAC_MODE_OFF}, {"entity_id": entity_id, "hvac_mode": HVACMode.OFF},
) )
await hass.async_block_till_done() await hass.async_block_till_done()
vera_device.turn_auto_on.assert_called() vera_device.turn_auto_on.assert_called()
vera_device.get_hvac_mode.return_value = "Off" vera_device.get_hvac_mode.return_value = "Off"
update_callback(vera_device) update_callback(vera_device)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get(entity_id).state == HVAC_MODE_OFF assert hass.states.get(entity_id).state == HVACMode.OFF
await hass.services.async_call( await hass.services.async_call(
"climate", "climate",