Use climate enums in plugwise (#70729)

This commit is contained in:
epenet 2022-04-26 09:03:23 +02:00 committed by GitHub
parent ab1dd7887e
commit 896b616687
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 36 deletions

View file

@ -4,14 +4,11 @@ from __future__ import annotations
from collections.abc import Mapping from collections.abc import Mapping
from typing import Any from typing import Any
from homeassistant.components.climate import ClimateEntity, ClimateEntityFeature from homeassistant.components.climate import ClimateEntity
from homeassistant.components.climate.const import ( from homeassistant.components.climate.const import (
CURRENT_HVAC_COOL, ClimateEntityFeature,
CURRENT_HVAC_HEAT, HVACAction,
CURRENT_HVAC_IDLE, HVACMode,
HVAC_MODE_AUTO,
HVAC_MODE_COOL,
HVAC_MODE_HEAT,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS
@ -61,11 +58,11 @@ class PlugwiseClimateEntity(PlugwiseEntity, ClimateEntity):
self._attr_preset_modes = list(presets) self._attr_preset_modes = list(presets)
# Determine hvac modes and current hvac mode # Determine hvac modes and current hvac mode
self._attr_hvac_modes = [HVAC_MODE_HEAT] self._attr_hvac_modes = [HVACMode.HEAT]
if self.coordinator.data.gateway.get("cooling_present"): if self.coordinator.data.gateway.get("cooling_present"):
self._attr_hvac_modes.append(HVAC_MODE_COOL) self._attr_hvac_modes.append(HVACMode.COOL)
if self.device.get("available_schedules") != ["None"]: if self.device.get("available_schedules") != ["None"]:
self._attr_hvac_modes.append(HVAC_MODE_AUTO) self._attr_hvac_modes.append(HVACMode.AUTO)
self._attr_min_temp = self.device.get("lower_bound", DEFAULT_MIN_TEMP) self._attr_min_temp = self.device.get("lower_bound", DEFAULT_MIN_TEMP)
self._attr_max_temp = self.device.get("upper_bound", DEFAULT_MAX_TEMP) self._attr_max_temp = self.device.get("upper_bound", DEFAULT_MAX_TEMP)
@ -84,31 +81,31 @@ class PlugwiseClimateEntity(PlugwiseEntity, ClimateEntity):
return self.device["sensors"].get("setpoint") return self.device["sensors"].get("setpoint")
@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."""
if (mode := self.device.get("mode")) is None or mode not in self.hvac_modes: if (mode := self.device.get("mode")) is None or mode not in self.hvac_modes:
return HVAC_MODE_HEAT return HVACMode.HEAT
return mode return HVACMode(mode)
@property @property
def hvac_action(self) -> str: def hvac_action(self) -> HVACAction:
"""Return the current running hvac operation if supported.""" """Return the current running hvac operation if supported."""
# When control_state is present, prefer this data # When control_state is present, prefer this data
if "control_state" in self.device: if "control_state" in self.device:
if self.device.get("control_state") == "cooling": if self.device.get("control_state") == "cooling":
return CURRENT_HVAC_COOL return HVACAction.COOLING
# Support preheating state as heating, until preheating is added as a separate state # Support preheating state as heating, until preheating is added as a separate state
if self.device.get("control_state") in ["heating", "preheating"]: if self.device.get("control_state") in ["heating", "preheating"]:
return CURRENT_HVAC_HEAT return HVACAction.HEATING
else: else:
heater_central_data = self.coordinator.data.devices[ heater_central_data = self.coordinator.data.devices[
self.coordinator.data.gateway["heater_id"] self.coordinator.data.gateway["heater_id"]
] ]
if heater_central_data["binary_sensors"].get("heating_state"): if heater_central_data["binary_sensors"].get("heating_state"):
return CURRENT_HVAC_HEAT return HVACAction.HEATING
if heater_central_data["binary_sensors"].get("cooling_state"): if heater_central_data["binary_sensors"].get("cooling_state"):
return CURRENT_HVAC_COOL return HVACAction.COOLING
return CURRENT_HVAC_IDLE return HVACAction.IDLE
@property @property
def preset_mode(self) -> str | None: def preset_mode(self) -> str | None:
@ -133,15 +130,15 @@ class PlugwiseClimateEntity(PlugwiseEntity, ClimateEntity):
await self.coordinator.api.set_temperature(self.device["location"], temperature) await self.coordinator.api.set_temperature(self.device["location"], temperature)
@plugwise_command @plugwise_command
async def async_set_hvac_mode(self, hvac_mode: str) -> None: async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set the hvac mode.""" """Set the hvac mode."""
if hvac_mode == HVAC_MODE_AUTO and not self.device.get("schedule_temperature"): if hvac_mode == HVACMode.AUTO and not self.device.get("schedule_temperature"):
raise ValueError("Cannot set HVAC mode to Auto: No schedule available") raise ValueError("Cannot set HVAC mode to Auto: No schedule available")
await self.coordinator.api.set_schedule_state( await self.coordinator.api.set_schedule_state(
self.device["location"], self.device["location"],
self.device.get("last_used"), self.device.get("last_used"),
"on" if hvac_mode == HVAC_MODE_AUTO else "off", "on" if hvac_mode == HVACMode.AUTO else "off",
) )
@plugwise_command @plugwise_command

View file

@ -1,15 +1,10 @@
"""Tests for the Plugwise Climate integration.""" """Tests for the Plugwise Climate integration."""
from unittest.mock import MagicMock from unittest.mock import MagicMock
from plugwise.exceptions import PlugwiseException from plugwise.exceptions import PlugwiseException
import pytest import pytest
from homeassistant.components.climate.const import ( from homeassistant.components.climate.const import HVACMode
HVAC_MODE_AUTO,
HVAC_MODE_COOL,
HVAC_MODE_HEAT,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
@ -24,8 +19,8 @@ async def test_adam_climate_entity_attributes(
assert state assert state
assert state.attributes["hvac_modes"] == [ assert state.attributes["hvac_modes"] == [
HVAC_MODE_HEAT, HVACMode.HEAT,
HVAC_MODE_AUTO, HVACMode.AUTO,
] ]
assert "preset_modes" in state.attributes assert "preset_modes" in state.attributes
@ -44,8 +39,8 @@ async def test_adam_climate_entity_attributes(
assert state assert state
assert state.attributes["hvac_modes"] == [ assert state.attributes["hvac_modes"] == [
HVAC_MODE_HEAT, HVACMode.HEAT,
HVAC_MODE_AUTO, HVACMode.AUTO,
] ]
assert "preset_modes" in state.attributes assert "preset_modes" in state.attributes
@ -90,7 +85,7 @@ async def test_adam_climate_adjust_negative_testing(
"set_hvac_mode", "set_hvac_mode",
{ {
"entity_id": "climate.zone_thermostat_jessie", "entity_id": "climate.zone_thermostat_jessie",
"hvac_mode": HVAC_MODE_AUTO, "hvac_mode": HVACMode.AUTO,
}, },
blocking=True, blocking=True,
) )
@ -155,10 +150,10 @@ async def test_anna_climate_entity_attributes(
"""Test creation of anna climate device environment.""" """Test creation of anna climate device environment."""
state = hass.states.get("climate.anna") state = hass.states.get("climate.anna")
assert state assert state
assert state.state == HVAC_MODE_HEAT assert state.state == HVACMode.HEAT
assert state.attributes["hvac_modes"] == [ assert state.attributes["hvac_modes"] == [
HVAC_MODE_HEAT, HVACMode.HEAT,
HVAC_MODE_COOL, HVACMode.COOL,
] ]
assert "no_frost" in state.attributes["preset_modes"] assert "no_frost" in state.attributes["preset_modes"]
assert "home" in state.attributes["preset_modes"] assert "home" in state.attributes["preset_modes"]