1
mirror of https://github.com/home-assistant/core synced 2024-09-03 08:14:07 +02:00

Use climate enums in knx (#70676)

This commit is contained in:
epenet 2022-04-25 14:57:31 +02:00 committed by GitHub
parent 23d9e135bc
commit 069dd567f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 47 deletions

View File

@ -8,12 +8,12 @@ from xknx.devices import Climate as XknxClimate, ClimateMode as XknxClimateMode
from xknx.dpt.dpt_hvac_mode import HVACControllerMode, HVACOperationMode
from homeassistant import config_entries
from homeassistant.components.climate import ClimateEntity, ClimateEntityFeature
from homeassistant.components.climate import ClimateEntity
from homeassistant.components.climate.const import (
CURRENT_HVAC_IDLE,
CURRENT_HVAC_OFF,
HVAC_MODE_OFF,
PRESET_AWAY,
ClimateEntityFeature,
HVACAction,
HVACMode,
)
from homeassistant.const import (
ATTR_TEMPERATURE,
@ -149,7 +149,9 @@ class KNXClimate(KnxEntity, ClimateEntity):
f"{self._device.target_temperature.group_address}_"
f"{self._device._setpoint_shift.group_address}"
)
self.default_hvac_mode: str = config[ClimateSchema.CONF_DEFAULT_CONTROLLER_MODE]
self.default_hvac_mode: HVACMode = config[
ClimateSchema.CONF_DEFAULT_CONTROLLER_MODE
]
@property
def current_temperature(self) -> float | None:
@ -181,10 +183,10 @@ class KNXClimate(KnxEntity, ClimateEntity):
self.async_write_ha_state()
@property
def hvac_mode(self) -> str:
def hvac_mode(self) -> HVACMode:
"""Return current operation ie. heat, cool, idle."""
if self._device.supports_on_off and not self._device.is_on:
return HVAC_MODE_OFF
return HVACMode.OFF
if self._device.mode is not None and self._device.mode.supports_controller_mode:
return CONTROLLER_MODES.get(
self._device.mode.controller_mode.value, self.default_hvac_mode
@ -192,9 +194,9 @@ class KNXClimate(KnxEntity, ClimateEntity):
return self.default_hvac_mode
@property
def hvac_modes(self) -> list[str]:
def hvac_modes(self) -> list[HVACMode]:
"""Return the list of available operation/controller modes."""
ha_controller_modes: list[str | None] = []
ha_controller_modes: list[HVACMode | None] = []
if self._device.mode is not None:
for knx_controller_mode in self._device.mode.controller_modes:
ha_controller_modes.append(
@ -204,30 +206,30 @@ class KNXClimate(KnxEntity, ClimateEntity):
if self._device.supports_on_off:
if not ha_controller_modes:
ha_controller_modes.append(self.default_hvac_mode)
ha_controller_modes.append(HVAC_MODE_OFF)
ha_controller_modes.append(HVACMode.OFF)
hvac_modes = list(set(filter(None, ha_controller_modes)))
return hvac_modes if hvac_modes else [self.default_hvac_mode]
@property
def hvac_action(self) -> str | None:
def hvac_action(self) -> HVACAction | None:
"""Return the current running hvac operation if supported.
Need to be one of CURRENT_HVAC_*.
"""
if self._device.supports_on_off and not self._device.is_on:
return CURRENT_HVAC_OFF
return HVACAction.OFF
if self._device.is_active is False:
return CURRENT_HVAC_IDLE
return HVACAction.IDLE
if (
self._device.mode is not None and self._device.mode.supports_controller_mode
) or self._device.is_active:
return CURRENT_HVAC_ACTIONS.get(self.hvac_mode, CURRENT_HVAC_IDLE)
return CURRENT_HVAC_ACTIONS.get(self.hvac_mode, HVACAction.IDLE)
return None
async def async_set_hvac_mode(self, hvac_mode: str) -> None:
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set operation mode."""
if self._device.supports_on_off and hvac_mode == HVAC_MODE_OFF:
if self._device.supports_on_off and hvac_mode == HVACMode.OFF:
await self._device.turn_off()
else:
if self._device.supports_on_off and not self._device.is_on:

View File

@ -5,22 +5,13 @@ from enum import Enum
from typing import Final, TypedDict
from homeassistant.components.climate.const import (
CURRENT_HVAC_COOL,
CURRENT_HVAC_DRY,
CURRENT_HVAC_FAN,
CURRENT_HVAC_HEAT,
CURRENT_HVAC_OFF,
HVAC_MODE_AUTO,
HVAC_MODE_COOL,
HVAC_MODE_DRY,
HVAC_MODE_FAN_ONLY,
HVAC_MODE_HEAT,
HVAC_MODE_OFF,
PRESET_AWAY,
PRESET_COMFORT,
PRESET_ECO,
PRESET_NONE,
PRESET_SLEEP,
HVACAction,
HVACMode,
)
from homeassistant.const import Platform
@ -127,20 +118,20 @@ SUPPORTED_PLATFORMS: Final = [
# Map KNX controller modes to HA modes. This list might not be complete.
CONTROLLER_MODES: Final = {
# Map DPT 20.105 HVAC control modes
"Auto": HVAC_MODE_AUTO,
"Heat": HVAC_MODE_HEAT,
"Cool": HVAC_MODE_COOL,
"Off": HVAC_MODE_OFF,
"Fan only": HVAC_MODE_FAN_ONLY,
"Dry": HVAC_MODE_DRY,
"Auto": HVACMode.AUTO,
"Heat": HVACMode.HEAT,
"Cool": HVACMode.COOL,
"Off": HVACMode.OFF,
"Fan only": HVACMode.FAN_ONLY,
"Dry": HVACMode.DRY,
}
CURRENT_HVAC_ACTIONS: Final = {
HVAC_MODE_HEAT: CURRENT_HVAC_HEAT,
HVAC_MODE_COOL: CURRENT_HVAC_COOL,
HVAC_MODE_OFF: CURRENT_HVAC_OFF,
HVAC_MODE_FAN_ONLY: CURRENT_HVAC_FAN,
HVAC_MODE_DRY: CURRENT_HVAC_DRY,
HVACMode.HEAT: HVACAction.HEATING,
HVACMode.COOL: HVACAction.COOLING,
HVACMode.OFF: HVACAction.OFF,
HVACMode.FAN_ONLY: HVACAction.FAN,
HVACMode.DRY: HVACAction.DRYING,
}
PRESET_MODES: Final = {

View File

@ -16,7 +16,7 @@ from xknx.telegram.address import IndividualAddress, parse_device_group_address
from homeassistant.components.binary_sensor import (
DEVICE_CLASSES_SCHEMA as BINARY_SENSOR_DEVICE_CLASSES_SCHEMA,
)
from homeassistant.components.climate.const import HVAC_MODE_HEAT, HVAC_MODES
from homeassistant.components.climate.const import HVACMode
from homeassistant.components.cover import (
DEVICE_CLASSES_SCHEMA as COVER_DEVICE_CLASSES_SCHEMA,
)
@ -465,8 +465,8 @@ class ClimateSchema(KNXPlatformSchema):
cv.ensure_list, [vol.In(CONTROLLER_MODES)]
),
vol.Optional(
CONF_DEFAULT_CONTROLLER_MODE, default=HVAC_MODE_HEAT
): vol.In(HVAC_MODES),
CONF_DEFAULT_CONTROLLER_MODE, default=HVACMode.HEAT
): vol.Coerce(HVACMode),
vol.Optional(CONF_MIN_TEMP): vol.Coerce(float),
vol.Optional(CONF_MAX_TEMP): vol.Coerce(float),
vol.Optional(CONF_ENTITY_CATEGORY): ENTITY_CATEGORIES_SCHEMA,

View File

@ -1,6 +1,5 @@
"""Test KNX climate."""
from homeassistant.components.climate import HVAC_MODE_HEAT, HVAC_MODE_OFF
from homeassistant.components.climate.const import PRESET_ECO, PRESET_SLEEP
from homeassistant.components.climate.const import PRESET_ECO, PRESET_SLEEP, HVACMode
from homeassistant.components.knx.schema import ClimateSchema
from homeassistant.const import CONF_NAME, STATE_IDLE
from homeassistant.core import HomeAssistant
@ -82,7 +81,7 @@ async def test_climate_hvac_mode(hass: HomeAssistant, knx: KNXTestKit):
await hass.services.async_call(
"climate",
"set_hvac_mode",
{"entity_id": "climate.test", "hvac_mode": HVAC_MODE_OFF},
{"entity_id": "climate.test", "hvac_mode": HVACMode.OFF},
blocking=True,
)
await knx.assert_write("1/2/8", False)
@ -91,7 +90,7 @@ async def test_climate_hvac_mode(hass: HomeAssistant, knx: KNXTestKit):
await hass.services.async_call(
"climate",
"set_hvac_mode",
{"entity_id": "climate.test", "hvac_mode": HVAC_MODE_HEAT},
{"entity_id": "climate.test", "hvac_mode": HVACMode.HEAT},
blocking=True,
)
await knx.assert_write("1/2/8", True)
@ -235,10 +234,10 @@ async def test_command_value_idle_mode(hass: HomeAssistant, knx: KNXTestKit):
assert len(events) == 2
events.pop()
knx.assert_state("climate.test", HVAC_MODE_HEAT, command_value=20)
knx.assert_state("climate.test", HVACMode.HEAT, command_value=20)
await knx.receive_write("1/2/6", (0x00,))
knx.assert_state(
"climate.test", HVAC_MODE_HEAT, command_value=0, hvac_action=STATE_IDLE
"climate.test", HVACMode.HEAT, command_value=0, hvac_action=STATE_IDLE
)