Refactor and update for lagging climate

This commit is contained in:
Andre Basche 2023-06-08 19:59:43 +02:00
parent 228cf3cf73
commit c433714a94

View File

@ -1,8 +1,6 @@
import logging
from dataclasses import dataclass
from pyhon.appliance import HonAppliance
from homeassistant.components.climate import (
ClimateEntity,
ClimateEntityDescription,
@ -22,6 +20,8 @@ from homeassistant.const import (
TEMP_CELSIUS,
)
from homeassistant.core import callback
from pyhon.appliance import HonAppliance
from .const import HON_HVAC_MODE, HON_FAN, HON_HVAC_PROGRAM, DOMAIN
from .hon import HonEntity
@ -103,12 +103,12 @@ class HonACClimateEntity(HonEntity, ClimateEntity):
self._attr_max_temp = device.settings["settings.tempSel"].max
self._attr_min_temp = device.settings["settings.tempSel"].min
self._attr_hvac_modes = [HVACMode.OFF] + [
HON_HVAC_MODE[mode] for mode in device.settings["settings.machMode"].values
]
self._attr_fan_modes = [FAN_OFF] + [
HON_FAN[mode] for mode in device.settings["settings.windSpeed"].values
]
self._attr_hvac_modes = [HVACMode.OFF]
for mode in device.settings["settings.machMode"].values:
self._attr_hvac_modes.append(HON_HVAC_MODE[mode])
self._attr_fan_modes = [FAN_OFF]
for mode in device.settings["settings.windSpeed"].values:
self._attr_fan_modes.append(HON_FAN[mode])
self._attr_swing_modes = [
SWING_OFF,
SWING_VERTICAL,
@ -123,6 +123,23 @@ class HonACClimateEntity(HonEntity, ClimateEntity):
self._handle_coordinator_update(update=False)
@property
def target_temperature(self) -> int | None:
"""Return the temperature we try to reach."""
return int(float(self._device.get("tempSel")))
@property
def current_temperature(self) -> float | None:
"""Return the current temperature."""
return float(self._device.get("tempIndoor"))
async def async_set_temperature(self, **kwargs):
if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
return False
self._device.settings["settings.tempSel"].value = str(int(temperature))
await self._device.commands["settings"].send()
self.async_write_ha_state()
@property
def hvac_mode(self) -> HVACMode | str | None:
if self._device.get("onOffStatus") == "0":
@ -131,24 +148,42 @@ class HonACClimateEntity(HonEntity, ClimateEntity):
return HON_HVAC_MODE[self._device.get("machMode")]
async def async_set_hvac_mode(self, hvac_mode):
self._attr_hvac_mode = hvac_mode
if hvac_mode == HVACMode.OFF:
await self._device.commands["stopProgram"].send()
else:
self._device.settings["startProgram.program"].value = HON_HVAC_PROGRAM[
hvac_mode
]
mode = HON_HVAC_PROGRAM[hvac_mode]
self._device.settings["startProgram.program"].value = mode
await self._device.commands["startProgram"].send()
self._attr_hvac_mode = hvac_mode
self.async_write_ha_state()
@property
def fan_mode(self) -> str | None:
"""Return the fan setting."""
return HON_FAN[self._device.get("windSpeed")]
async def async_set_fan_mode(self, fan_mode):
mode_number = list(HON_FAN.values()).index(fan_mode)
self._device.settings["settings.windSpeed"].value = list(HON_FAN.keys())[
mode_number
]
mode = list(HON_FAN.keys())[mode_number]
self._device.settings["settings.windSpeed"].value = mode
self._attr_fan_mode = fan_mode
await self._device.commands["settings"].send()
self.async_write_ha_state()
@property
def swing_mode(self) -> str | None:
"""Return the swing setting."""
horizontal = self._device.get("windDirectionHorizontal")
vertical = self._device.get("windDirectionVertical")
if horizontal == "7" and vertical == "8":
return SWING_BOTH
elif horizontal == "7":
return SWING_HORIZONTAL
elif vertical == "8":
return SWING_VERTICAL
else:
return SWING_OFF
async def async_set_swing_mode(self, swing_mode):
horizontal = self._device.settings["settings.windDirectionHorizontal"]
vertical = self._device.settings["settings.windDirectionVertical"]
@ -164,35 +199,13 @@ class HonACClimateEntity(HonEntity, ClimateEntity):
await self._device.commands["settings"].send()
self.async_write_ha_state()
async def async_set_temperature(self, **kwargs):
if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
return False
self._device.settings["settings.tempSel"].value = str(int(temperature))
await self._device.commands["settings"].send()
self.async_write_ha_state()
@callback
def _handle_coordinator_update(self, update=True) -> None:
self._attr_target_temperature = int(float(self._device.get("tempSel")))
self._attr_current_temperature = float(self._device.get("tempIndoor"))
if self._device.get("onOffStatus") == "0":
self._attr_hvac_mode = HVACMode.OFF
else:
self._attr_hvac_mode = HON_HVAC_MODE[self._device.get("machMode")]
self._attr_fan_mode = HON_FAN[self._device.get("windSpeed")]
horizontal = self._device.get("windDirectionHorizontal")
vertical = self._device.get("windDirectionVertical")
if horizontal == "7" and vertical == "8":
self._attr_swing_mode = SWING_BOTH
elif horizontal == "7":
self._attr_swing_mode = SWING_HORIZONTAL
elif vertical == "8":
self._attr_swing_mode = SWING_VERTICAL
else:
self._attr_swing_mode = SWING_OFF
self._attr_target_temperature = self.target_temperature
self._attr_current_temperature = self.current_temperature
self._attr_hvac_mode = self.hvac_mode
self._attr_fan_mode = self.fan_mode
self._attr_swing_mode = self.swing_mode
if update:
self.async_write_ha_state()