Fix humidifier mode for Vesync (#135746)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>pull/136432/head
parent
72d1ac9f92
commit
50cf94ca9b
|
@ -6,7 +6,6 @@ from typing import Any
|
||||||
from pyvesync.vesyncbasedevice import VeSyncBaseDevice
|
from pyvesync.vesyncbasedevice import VeSyncBaseDevice
|
||||||
|
|
||||||
from homeassistant.components.humidifier import (
|
from homeassistant.components.humidifier import (
|
||||||
ATTR_HUMIDITY,
|
|
||||||
MODE_AUTO,
|
MODE_AUTO,
|
||||||
MODE_NORMAL,
|
MODE_NORMAL,
|
||||||
MODE_SLEEP,
|
MODE_SLEEP,
|
||||||
|
@ -40,8 +39,6 @@ _LOGGER = logging.getLogger(__name__)
|
||||||
MIN_HUMIDITY = 30
|
MIN_HUMIDITY = 30
|
||||||
MAX_HUMIDITY = 80
|
MAX_HUMIDITY = 80
|
||||||
|
|
||||||
VS_TO_HA_ATTRIBUTES = {ATTR_HUMIDITY: "current_humidity"}
|
|
||||||
|
|
||||||
VS_TO_HA_MODE_MAP = {
|
VS_TO_HA_MODE_MAP = {
|
||||||
VS_HUMIDIFIER_MODE_AUTO: MODE_AUTO,
|
VS_HUMIDIFIER_MODE_AUTO: MODE_AUTO,
|
||||||
VS_HUMIDIFIER_MODE_HUMIDITY: MODE_AUTO,
|
VS_HUMIDIFIER_MODE_HUMIDITY: MODE_AUTO,
|
||||||
|
@ -49,8 +46,6 @@ VS_TO_HA_MODE_MAP = {
|
||||||
VS_HUMIDIFIER_MODE_SLEEP: MODE_SLEEP,
|
VS_HUMIDIFIER_MODE_SLEEP: MODE_SLEEP,
|
||||||
}
|
}
|
||||||
|
|
||||||
HA_TO_VS_MODE_MAP = {v: k for k, v in VS_TO_HA_MODE_MAP.items()}
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
@ -92,10 +87,6 @@ def _get_ha_mode(vs_mode: str) -> str | None:
|
||||||
return ha_mode
|
return ha_mode
|
||||||
|
|
||||||
|
|
||||||
def _get_vs_mode(ha_mode: str) -> str | None:
|
|
||||||
return HA_TO_VS_MODE_MAP.get(ha_mode)
|
|
||||||
|
|
||||||
|
|
||||||
class VeSyncHumidifierHA(VeSyncBaseEntity, HumidifierEntity):
|
class VeSyncHumidifierHA(VeSyncBaseEntity, HumidifierEntity):
|
||||||
"""Representation of a VeSync humidifier."""
|
"""Representation of a VeSync humidifier."""
|
||||||
|
|
||||||
|
@ -108,14 +99,35 @@ class VeSyncHumidifierHA(VeSyncBaseEntity, HumidifierEntity):
|
||||||
|
|
||||||
device: VeSyncHumidifierDevice
|
device: VeSyncHumidifierDevice
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
device: VeSyncBaseDevice,
|
||||||
|
coordinator: VeSyncDataCoordinator,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize the VeSyncHumidifierHA device."""
|
||||||
|
super().__init__(device, coordinator)
|
||||||
|
|
||||||
|
# 2 Vesync humidifier modes (humidity and auto) maps to the HA mode auto.
|
||||||
|
# They are on different devices though. We need to map HA mode to the
|
||||||
|
# device specific mode when setting it.
|
||||||
|
|
||||||
|
self._ha_to_vs_mode_map: dict[str, str] = {}
|
||||||
|
self._available_modes: list[str] = []
|
||||||
|
|
||||||
|
# Populate maps once.
|
||||||
|
for vs_mode in self.device.mist_modes:
|
||||||
|
ha_mode = _get_ha_mode(vs_mode)
|
||||||
|
if ha_mode:
|
||||||
|
self._available_modes.append(ha_mode)
|
||||||
|
self._ha_to_vs_mode_map[ha_mode] = vs_mode
|
||||||
|
|
||||||
|
def _get_vs_mode(self, ha_mode: str) -> str | None:
|
||||||
|
return self._ha_to_vs_mode_map.get(ha_mode)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available_modes(self) -> list[str]:
|
def available_modes(self) -> list[str]:
|
||||||
"""Return the available mist modes."""
|
"""Return the available mist modes."""
|
||||||
return [
|
return self._available_modes
|
||||||
ha_mode
|
|
||||||
for ha_mode in (_get_ha_mode(vs_mode) for vs_mode in self.device.mist_modes)
|
|
||||||
if ha_mode
|
|
||||||
]
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def target_humidity(self) -> int:
|
def target_humidity(self) -> int:
|
||||||
|
@ -140,9 +152,15 @@ class VeSyncHumidifierHA(VeSyncBaseEntity, HumidifierEntity):
|
||||||
raise HomeAssistantError(
|
raise HomeAssistantError(
|
||||||
"{mode} is not one of the valid available modes: {self.available_modes}"
|
"{mode} is not one of the valid available modes: {self.available_modes}"
|
||||||
)
|
)
|
||||||
if not self.device.set_humidity_mode(_get_vs_mode(mode)):
|
if not self.device.set_humidity_mode(self._get_vs_mode(mode)):
|
||||||
raise HomeAssistantError(f"An error occurred while setting mode {mode}.")
|
raise HomeAssistantError(f"An error occurred while setting mode {mode}.")
|
||||||
|
|
||||||
|
# Changing mode while humidifier is off actually turns it on, as per the app. But
|
||||||
|
# the library does not seem to update the device_status. It is also possible that
|
||||||
|
# other attributes get updated. Scheduling a forced refresh to get device status.
|
||||||
|
# updated.
|
||||||
|
self.schedule_update_ha_state(force_refresh=True)
|
||||||
|
|
||||||
def turn_on(self, **kwargs: Any) -> None:
|
def turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
success = self.device.turn_on()
|
success = self.device.turn_on()
|
||||||
|
|
Loading…
Reference in New Issue