2023-04-26 21:57:44 +00:00
|
|
|
import logging
|
2023-05-28 05:50:59 +00:00
|
|
|
from dataclasses import dataclass
|
2023-04-26 21:57:44 +00:00
|
|
|
|
|
|
|
from homeassistant.components.climate import (
|
|
|
|
ClimateEntity,
|
|
|
|
ClimateEntityDescription,
|
|
|
|
)
|
|
|
|
from homeassistant.components.climate.const import (
|
|
|
|
FAN_OFF,
|
|
|
|
SWING_OFF,
|
|
|
|
SWING_BOTH,
|
|
|
|
SWING_VERTICAL,
|
|
|
|
SWING_HORIZONTAL,
|
|
|
|
ClimateEntityFeature,
|
|
|
|
HVACMode,
|
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_TEMPERATURE,
|
|
|
|
PRECISION_WHOLE,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
)
|
|
|
|
from homeassistant.core import callback
|
2023-05-28 05:50:59 +00:00
|
|
|
from pyhon import helper
|
2023-05-24 22:52:54 +00:00
|
|
|
from pyhon.appliance import HonAppliance
|
|
|
|
|
2023-05-06 22:52:54 +00:00
|
|
|
from .const import HON_HVAC_MODE, HON_FAN, HON_HVAC_PROGRAM, DOMAIN
|
2023-05-24 23:30:33 +00:00
|
|
|
from .hon import HonEntity
|
2023-04-26 21:57:44 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2023-05-28 05:50:59 +00:00
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class HonACClimateEntityDescription(ClimateEntityDescription):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class HonREFClimateEntityDescription(ClimateEntityDescription):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-04-26 21:57:44 +00:00
|
|
|
CLIMATES = {
|
2023-05-10 16:13:05 +00:00
|
|
|
"AC": (
|
2023-05-28 05:50:59 +00:00
|
|
|
HonACClimateEntityDescription(
|
2023-05-10 16:13:05 +00:00
|
|
|
key="settings",
|
|
|
|
name="Air Conditioner",
|
|
|
|
icon="mdi:air-conditioner",
|
|
|
|
translation_key="air_conditioner",
|
|
|
|
),
|
|
|
|
),
|
2023-05-28 05:50:59 +00:00
|
|
|
"REF": (
|
|
|
|
HonREFClimateEntityDescription(
|
|
|
|
key="settings.tempSelZ1",
|
|
|
|
name="Fridge",
|
|
|
|
icon="mdi:thermometer",
|
|
|
|
translation_key="fridge",
|
|
|
|
),
|
|
|
|
HonREFClimateEntityDescription(
|
|
|
|
key="settings.tempSelZ2",
|
|
|
|
name="Freezer",
|
|
|
|
icon="mdi:snowflake-thermometer",
|
|
|
|
translation_key="freezer",
|
|
|
|
),
|
|
|
|
),
|
2023-04-26 21:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, entry: ConfigEntry, async_add_entities) -> None:
|
2023-05-24 23:30:33 +00:00
|
|
|
entities = []
|
|
|
|
for device in hass.data[DOMAIN][entry.unique_id].appliances:
|
|
|
|
for description in CLIMATES.get(device.appliance_type, []):
|
2023-05-28 05:50:59 +00:00
|
|
|
if isinstance(description, HonACClimateEntityDescription):
|
|
|
|
if description.key not in list(device.commands):
|
|
|
|
continue
|
|
|
|
entity = HonACClimateEntity(hass, entry, device, description)
|
|
|
|
elif isinstance(description, HonREFClimateEntityDescription):
|
|
|
|
if description.key not in device.available_settings:
|
|
|
|
continue
|
|
|
|
entity = HonREFClimateEntity(hass, entry, device, description)
|
|
|
|
else:
|
2023-05-24 23:30:33 +00:00
|
|
|
continue
|
|
|
|
await entity.coordinator.async_config_entry_first_refresh()
|
|
|
|
entities.append(entity)
|
|
|
|
async_add_entities(entities)
|
2023-04-26 21:57:44 +00:00
|
|
|
|
|
|
|
|
2023-05-28 05:50:59 +00:00
|
|
|
class HonACClimateEntity(HonEntity, ClimateEntity):
|
2023-05-24 23:30:33 +00:00
|
|
|
def __init__(self, hass, entry, device: HonAppliance, description) -> None:
|
2023-05-27 22:30:08 +00:00
|
|
|
super().__init__(hass, entry, device, description)
|
2023-04-26 21:57:44 +00:00
|
|
|
|
|
|
|
self._attr_temperature_unit = TEMP_CELSIUS
|
|
|
|
self._attr_target_temperature_step = PRECISION_WHOLE
|
2023-05-08 00:05:04 +00:00
|
|
|
self._attr_max_temp = device.settings["settings.tempSel"].max
|
|
|
|
self._attr_min_temp = device.settings["settings.tempSel"].min
|
2023-04-26 21:57:44 +00:00
|
|
|
|
|
|
|
self._attr_hvac_modes = [HVACMode.OFF] + [
|
2023-05-08 00:05:04 +00:00
|
|
|
HON_HVAC_MODE[mode] for mode in device.settings["settings.machMode"].values
|
2023-04-26 21:57:44 +00:00
|
|
|
]
|
|
|
|
self._attr_fan_modes = [FAN_OFF] + [
|
2023-05-08 00:05:04 +00:00
|
|
|
HON_FAN[mode] for mode in device.settings["settings.windSpeed"].values
|
2023-04-26 21:57:44 +00:00
|
|
|
]
|
|
|
|
self._attr_swing_modes = [
|
|
|
|
SWING_OFF,
|
|
|
|
SWING_VERTICAL,
|
|
|
|
SWING_HORIZONTAL,
|
|
|
|
SWING_BOTH,
|
|
|
|
]
|
|
|
|
self._attr_supported_features = (
|
|
|
|
ClimateEntityFeature.TARGET_TEMPERATURE
|
|
|
|
| ClimateEntityFeature.FAN_MODE
|
|
|
|
| ClimateEntityFeature.SWING_MODE
|
|
|
|
)
|
|
|
|
|
2023-05-18 23:27:44 +00:00
|
|
|
self._handle_coordinator_update(update=False)
|
2023-05-08 00:05:04 +00:00
|
|
|
|
2023-04-26 21:57:44 +00:00
|
|
|
async def async_set_hvac_mode(self, hvac_mode):
|
2023-05-28 05:50:59 +00:00
|
|
|
if self._device.get("onOffStatus") == "0":
|
|
|
|
self._attr_hvac_mode = HVACMode.OFF
|
|
|
|
else:
|
|
|
|
self._attr_hvac_mode = HON_HVAC_MODE[self._device.get("machMode")]
|
2023-04-26 21:57:44 +00:00
|
|
|
if hvac_mode == HVACMode.OFF:
|
2023-05-08 00:05:04 +00:00
|
|
|
await self._device.commands["stopProgram"].send()
|
2023-04-26 21:57:44 +00:00
|
|
|
else:
|
2023-05-08 00:05:04 +00:00
|
|
|
self._device.settings["startProgram.program"].value = HON_HVAC_PROGRAM[
|
|
|
|
hvac_mode
|
|
|
|
]
|
|
|
|
await self._device.commands["startProgram"].send()
|
2023-04-26 21:57:44 +00:00
|
|
|
self._attr_hvac_mode = hvac_mode
|
2023-05-16 22:01:33 +00:00
|
|
|
self.async_write_ha_state()
|
2023-04-26 21:57:44 +00:00
|
|
|
|
|
|
|
async def async_set_fan_mode(self, fan_mode):
|
|
|
|
mode_number = list(HON_FAN.values()).index(fan_mode)
|
2023-05-08 00:05:04 +00:00
|
|
|
self._device.settings["settings.windSpeed"].value = list(HON_FAN.keys())[
|
|
|
|
mode_number
|
|
|
|
]
|
|
|
|
await self._device.commands["settings"].send()
|
2023-05-16 22:01:33 +00:00
|
|
|
self.async_write_ha_state()
|
2023-04-26 21:57:44 +00:00
|
|
|
|
|
|
|
async def async_set_swing_mode(self, swing_mode):
|
2023-05-08 00:05:04 +00:00
|
|
|
horizontal = self._device.settings["settings.windDirectionHorizontal"]
|
|
|
|
vertical = self._device.settings["settings.windDirectionVertical"]
|
2023-04-26 21:57:44 +00:00
|
|
|
if swing_mode in [SWING_BOTH, SWING_HORIZONTAL]:
|
|
|
|
horizontal.value = "7"
|
|
|
|
if swing_mode in [SWING_BOTH, SWING_VERTICAL]:
|
|
|
|
vertical.value = "8"
|
|
|
|
if swing_mode in [SWING_OFF, SWING_HORIZONTAL] and vertical.value == "8":
|
|
|
|
vertical.value = "5"
|
|
|
|
if swing_mode in [SWING_OFF, SWING_VERTICAL] and horizontal.value == "7":
|
|
|
|
horizontal.value = "0"
|
|
|
|
self._attr_swing_mode = swing_mode
|
2023-05-08 00:05:04 +00:00
|
|
|
await self._device.commands["settings"].send()
|
2023-05-16 22:01:33 +00:00
|
|
|
self.async_write_ha_state()
|
2023-04-26 21:57:44 +00:00
|
|
|
|
|
|
|
async def async_set_temperature(self, **kwargs):
|
|
|
|
if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
|
|
|
|
return False
|
2023-05-16 22:01:33 +00:00
|
|
|
self._device.settings["settings.tempSel"].value = str(int(temperature))
|
2023-05-08 00:05:04 +00:00
|
|
|
await self._device.commands["settings"].send()
|
2023-05-16 22:01:33 +00:00
|
|
|
self.async_write_ha_state()
|
2023-04-26 21:57:44 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self, update=True) -> None:
|
2023-05-08 17:17:08 +00:00
|
|
|
self._attr_target_temperature = int(float(self._device.get("tempSel")))
|
|
|
|
self._attr_current_temperature = float(self._device.get("tempIndoor"))
|
2023-04-26 21:57:44 +00:00
|
|
|
|
|
|
|
if self._device.get("onOffStatus") == "0":
|
|
|
|
self._attr_hvac_mode = HVACMode.OFF
|
|
|
|
else:
|
2023-05-28 05:50:59 +00:00
|
|
|
self._attr_hvac_mode = HON_HVAC_MODE[self._device.get("machMode")]
|
2023-04-26 21:57:44 +00:00
|
|
|
|
2023-05-16 22:01:33 +00:00
|
|
|
self._attr_fan_mode = HON_FAN[self._device.get("windSpeed")]
|
2023-04-26 21:57:44 +00:00
|
|
|
|
2023-05-16 22:01:33 +00:00
|
|
|
horizontal = self._device.get("windDirectionHorizontal")
|
|
|
|
vertical = self._device.get("windDirectionVertical")
|
2023-04-26 21:57:44 +00:00
|
|
|
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
|
2023-05-18 23:27:44 +00:00
|
|
|
if update:
|
|
|
|
self.async_write_ha_state()
|
2023-05-28 05:50:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HonREFClimateEntity(HonEntity, ClimateEntity):
|
|
|
|
def __init__(self, hass, entry, device: HonAppliance, description) -> None:
|
|
|
|
super().__init__(hass, entry, device, description)
|
|
|
|
|
|
|
|
self._attr_temperature_unit = TEMP_CELSIUS
|
|
|
|
self._attr_target_temperature_step = PRECISION_WHOLE
|
|
|
|
self._attr_max_temp = device.settings[description.key].max
|
|
|
|
self._attr_min_temp = device.settings[description.key].min
|
|
|
|
|
|
|
|
self._attr_hvac_modes = [HVACMode.COOL]
|
|
|
|
self._attr_supported_features = (
|
|
|
|
ClimateEntityFeature.TARGET_TEMPERATURE | ClimateEntityFeature.PRESET_MODE
|
|
|
|
)
|
|
|
|
|
|
|
|
self._handle_coordinator_update(update=False)
|
|
|
|
|
|
|
|
modes = ["no_mode"]
|
|
|
|
for mode, data in device.commands["startProgram"].categories.items():
|
|
|
|
if zone := data.parameters.get("zone"):
|
|
|
|
if self.entity_description.name.lower() in zone.values:
|
|
|
|
modes.append(mode)
|
|
|
|
self._attr_preset_modes = modes
|
|
|
|
|
|
|
|
@property
|
|
|
|
def target_temperature(self) -> int | None:
|
|
|
|
"""Return the temperature we try to reach."""
|
|
|
|
return int(self._device.get(self.entity_description.key))
|
|
|
|
|
|
|
|
@property
|
|
|
|
def current_temperature(self) -> int | None:
|
|
|
|
"""Return the current temperature."""
|
|
|
|
temp_key = self.entity_description.key.split(".")[-1].replace("Sel", "")
|
|
|
|
return int(self._device.get(temp_key))
|
|
|
|
|
|
|
|
async def async_set_temperature(self, **kwargs):
|
|
|
|
if (temperature := kwargs.get(ATTR_TEMPERATURE)) is None:
|
|
|
|
return False
|
|
|
|
self._device.settings[self.entity_description.key].value = str(int(temperature))
|
|
|
|
await self._device.commands["settings"].send()
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def preset_mode(self) -> str | None:
|
|
|
|
"""Return the current Preset for this channel."""
|
|
|
|
return self._device.get(f"mode{self.entity_description.key[-2:]}", "no_mode")
|
|
|
|
|
|
|
|
async def async_set_preset_mode(self, preset_mode: str) -> None:
|
|
|
|
"""Set the new preset mode."""
|
|
|
|
if preset_mode == "no_mode":
|
|
|
|
self._device.sync_command("stopProgram", "settings")
|
|
|
|
await self.coordinator.async_refresh()
|
|
|
|
await self._device.commands["stopProgram"].send()
|
|
|
|
else:
|
|
|
|
self._device.settings["startProgram.program"].value = preset_mode
|
|
|
|
self._device.settings[
|
|
|
|
"startProgram.zone"
|
|
|
|
].value = self.entity_description.name.lower()
|
|
|
|
self._device.sync_command("startProgram", "settings")
|
|
|
|
await self.coordinator.async_refresh()
|
|
|
|
await self._device.commands["startProgram"].send()
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self, update=True) -> None:
|
|
|
|
self._attr_target_temperature = int(
|
|
|
|
float(self._device.get(self.entity_description.key))
|
|
|
|
)
|
|
|
|
temp_key = self.entity_description.key.split(".")[-1].replace("Sel", "")
|
|
|
|
self._attr_current_temperature = int(self._device.get(temp_key))
|
|
|
|
|
|
|
|
self._attr_hvac_mode = HVACMode.COOL
|
|
|
|
if update:
|
|
|
|
self.async_write_ha_state()
|