Use climate enums in plugwise (#70729)

pull/69301/head
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 typing import Any
from homeassistant.components.climate import ClimateEntity, ClimateEntityFeature
from homeassistant.components.climate import ClimateEntity
from homeassistant.components.climate.const import (
CURRENT_HVAC_COOL,
CURRENT_HVAC_HEAT,
CURRENT_HVAC_IDLE,
HVAC_MODE_AUTO,
HVAC_MODE_COOL,
HVAC_MODE_HEAT,
ClimateEntityFeature,
HVACAction,
HVACMode,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS
@ -61,11 +58,11 @@ class PlugwiseClimateEntity(PlugwiseEntity, ClimateEntity):
self._attr_preset_modes = list(presets)
# 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"):
self._attr_hvac_modes.append(HVAC_MODE_COOL)
self._attr_hvac_modes.append(HVACMode.COOL)
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_max_temp = self.device.get("upper_bound", DEFAULT_MAX_TEMP)
@ -84,31 +81,31 @@ class PlugwiseClimateEntity(PlugwiseEntity, ClimateEntity):
return self.device["sensors"].get("setpoint")
@property
def hvac_mode(self) -> str:
def hvac_mode(self) -> HVACMode:
"""Return HVAC operation ie. heat, cool mode."""
if (mode := self.device.get("mode")) is None or mode not in self.hvac_modes:
return HVAC_MODE_HEAT
return mode
return HVACMode.HEAT
return HVACMode(mode)
@property
def hvac_action(self) -> str:
def hvac_action(self) -> HVACAction:
"""Return the current running hvac operation if supported."""
# When control_state is present, prefer this data
if "control_state" in self.device:
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
if self.device.get("control_state") in ["heating", "preheating"]:
return CURRENT_HVAC_HEAT
return HVACAction.HEATING
else:
heater_central_data = self.coordinator.data.devices[
self.coordinator.data.gateway["heater_id"]
]
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"):
return CURRENT_HVAC_COOL
return CURRENT_HVAC_IDLE
return HVACAction.COOLING
return HVACAction.IDLE
@property
def preset_mode(self) -> str | None:
@ -133,15 +130,15 @@ class PlugwiseClimateEntity(PlugwiseEntity, ClimateEntity):
await self.coordinator.api.set_temperature(self.device["location"], temperature)
@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."""
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")
await self.coordinator.api.set_schedule_state(
self.device["location"],
self.device.get("last_used"),
"on" if hvac_mode == HVAC_MODE_AUTO else "off",
"on" if hvac_mode == HVACMode.AUTO else "off",
)
@plugwise_command

View File

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