More cleanup in Plugwise climate (#66257)

This commit is contained in:
Franck Nijhof 2022-02-10 17:13:22 +01:00 committed by GitHub
parent deb8185911
commit f5829173db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,4 +1,7 @@
"""Plugwise Climate component for Home Assistant."""
from __future__ import annotations
from collections.abc import Mapping
from typing import Any
from homeassistant.components.climate import ClimateEntity
@ -15,7 +18,7 @@ from homeassistant.components.climate.const import (
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DEFAULT_MAX_TEMP, DEFAULT_MIN_TEMP, DOMAIN, SCHEDULE_OFF, SCHEDULE_ON
@ -45,15 +48,12 @@ async def async_setup_entry(
class PlugwiseClimateEntity(PlugwiseEntity, ClimateEntity):
"""Representation of an Plugwise thermostat."""
_attr_hvac_mode = HVAC_MODE_HEAT
_attr_max_temp = DEFAULT_MAX_TEMP
_attr_min_temp = DEFAULT_MIN_TEMP
_attr_preset_mode = None
_attr_preset_modes = None
_attr_supported_features = SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE
_attr_temperature_unit = TEMP_CELSIUS
_attr_hvac_modes = HVAC_MODES_HEAT_ONLY
_attr_hvac_mode = HVAC_MODE_HEAT
def __init__(
self,
@ -64,9 +64,59 @@ class PlugwiseClimateEntity(PlugwiseEntity, ClimateEntity):
super().__init__(coordinator, device_id)
self._attr_extra_state_attributes = {}
self._attr_unique_id = f"{device_id}-climate"
self._attr_name = coordinator.data.devices[device_id].get("name")
self._attr_name = self.device.get("name")
self._loc_id = coordinator.data.devices[device_id]["location"]
# Determine preset modes
if presets := self.device.get("presets"):
self._attr_preset_modes = list(presets)
# Determine hvac modes and current hvac mode
if self.coordinator.data.gateway.get("cooling_present"):
self._attr_hvac_modes = HVAC_MODES_HEAT_COOL
@property
def current_temperature(self) -> float | None:
"""Return the current temperature."""
return self.device["sensors"].get("temperature")
@property
def target_temperature(self) -> float | None:
"""Return the temperature we try to reach."""
return self.device["sensors"].get("setpoint")
@property
def hvac_mode(self) -> str:
"""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_OFF
return mode
@property
def hvac_action(self) -> str:
"""Return the current running hvac operation if supported."""
heater_central_data = self.coordinator.data.devices[
self.coordinator.data.gateway["heater_id"]
]
if heater_central_data.get("heating_state"):
return CURRENT_HVAC_HEAT
if heater_central_data.get("cooling_state"):
return CURRENT_HVAC_COOL
return CURRENT_HVAC_IDLE
@property
def preset_mode(self) -> str | None:
"""Return the current preset mode."""
return self.device.get("active_preset")
@property
def extra_state_attributes(self) -> Mapping[str, Any] | None:
"""Return entity specific state attributes."""
return {
"available_schemas": self.device.get("available_schedules"),
"selected_schema": self.device.get("selected_schedule"),
}
@plugwise_command
async def async_set_temperature(self, **kwargs: Any) -> None:
@ -75,72 +125,24 @@ class PlugwiseClimateEntity(PlugwiseEntity, ClimateEntity):
self._attr_max_temp < temperature < self._attr_min_temp
):
raise ValueError("Invalid temperature requested")
await self.coordinator.api.set_temperature(self._loc_id, temperature)
await self.coordinator.api.set_temperature(self.device["location"], temperature)
@plugwise_command
async def async_set_hvac_mode(self, hvac_mode: str) -> None:
"""Set the hvac mode."""
state = SCHEDULE_OFF
climate_data = self.coordinator.data.devices[self._dev_id]
if hvac_mode == HVAC_MODE_AUTO:
state = SCHEDULE_ON
await self.coordinator.api.set_temperature(
self._loc_id, climate_data.get("schedule_temperature")
self.device["location"], self.device.get("schedule_temperature")
)
self._attr_target_temperature = climate_data.get("schedule_temperature")
self._attr_target_temperature = self.device.get("schedule_temperature")
await self.coordinator.api.set_schedule_state(
self._loc_id, climate_data.get("last_used"), state
self.device["location"], self.device.get("last_used"), state
)
@plugwise_command
async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set the preset mode."""
if not self.coordinator.data.devices[self._dev_id].get("presets"):
raise ValueError("No presets available")
await self.coordinator.api.set_preset(self._loc_id, preset_mode)
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
data = self.coordinator.data.devices[self._dev_id]
heater_central_data = self.coordinator.data.devices[
self.coordinator.data.gateway["heater_id"]
]
# Current & set temperatures
if setpoint := data["sensors"].get("setpoint"):
self._attr_target_temperature = setpoint
if temperature := data["sensors"].get("temperature"):
self._attr_current_temperature = temperature
# Presets handling
self._attr_preset_mode = data.get("active_preset")
if presets := data.get("presets"):
self._attr_preset_modes = list(presets)
else:
self._attr_preset_mode = None
# Determine current hvac action
self._attr_hvac_action = CURRENT_HVAC_IDLE
if heater_central_data.get("heating_state"):
self._attr_hvac_action = CURRENT_HVAC_HEAT
elif heater_central_data.get("cooling_state"):
self._attr_hvac_action = CURRENT_HVAC_COOL
# Determine hvac modes and current hvac mode
self._attr_hvac_modes = HVAC_MODES_HEAT_ONLY
if self.coordinator.data.gateway.get("cooling_present"):
self._attr_hvac_modes = HVAC_MODES_HEAT_COOL
if data.get("mode") in self._attr_hvac_modes:
self._attr_hvac_mode = data["mode"]
# Extra attributes
self._attr_extra_state_attributes = {
"available_schemas": data.get("available_schedules"),
"selected_schema": data.get("selected_schedule"),
}
super()._handle_coordinator_update()
await self.coordinator.api.set_preset(self.device["location"], preset_mode)