Skip to content

Commit

Permalink
Fix humidifer mode and convert to object.
Browse files Browse the repository at this point in the history
* Similar to selects and climates humidifier now is dict object.
* Add Friendly names to the modes.
  • Loading branch information
xZetsubou committed Jan 13, 2024
1 parent bee12d1 commit c5218f8
Show file tree
Hide file tree
Showing 6 changed files with 767 additions and 748 deletions.
26 changes: 20 additions & 6 deletions custom_components/localtuya/humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from functools import partial
from .config_flow import _col_to_select
from homeassistant.helpers import selector

import voluptuous as vol
from homeassistant.const import CONF_DEVICE_CLASS
Expand Down Expand Up @@ -38,17 +39,19 @@ def flow_schema(dps):
vol.Optional(CONF_HUMIDIFIER_MODE_DP): _col_to_select(dps, is_dps=True),
vol.Required(ATTR_MIN_HUMIDITY, default=DEFAULT_MIN_HUMIDITY): int,
vol.Required(ATTR_MAX_HUMIDITY, default=DEFAULT_MAX_HUMIDITY): int,
vol.Optional(CONF_HUMIDIFIER_AVAILABLE_MODES): str,
vol.Optional(
CONF_HUMIDIFIER_AVAILABLE_MODES, default={}
): selector.ObjectSelector(),
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
}


class LocaltuyaHumidifier(LocalTuyaEntity, HumidifierEntity):
"""Representation of a Localtuya Humidifier."""

_available_modes = CONF_HUMIDIFIER_MODE_DP
_dp_current_humidity = CONF_HUMIDIFIER_CURRENT_HUMIDITY_DP
_dp_mode = CONF_HUMIDIFIER_MODE_DP
_available_modes = CONF_HUMIDIFIER_AVAILABLE_MODES
_dp_current_humidity = CONF_HUMIDIFIER_CURRENT_HUMIDITY_DP
_dp_set_humidity = CONF_HUMIDIFIER_SET_HUMIDITY_DP

def __init__(
Expand All @@ -61,6 +64,7 @@ def __init__(
"""Initialize the Tuya button."""
super().__init__(device, config_entry, humidifierID, _LOGGER, **kwargs)
self._state = None
self._current_mode = None

if self._config.get(self._dp_mode) and self._config.get(self._available_modes):
self._attr_supported_features |= HumidifierEntityFeature.MODES
Expand Down Expand Up @@ -115,9 +119,8 @@ async def async_set_humidity(self, humidity: int) -> None:
@property
def available_modes(self):
"""Return the list of presets that this device supports."""
if modes := self._config.get(self._available_modes, None):
modes = [v.lstrip() for v in modes.split(",")]

if modes := self._config.get(self._available_modes, {}).values():
modes = list(modes)
return modes

async def async_set_mode(self, mode):
Expand All @@ -128,5 +131,16 @@ async def async_set_mode(self, mode):

await self._device.set_dp(set_mode_dp, mode)

def status_updated(self):
"""Device status was updated."""
super().status_updated()
current_mode = self.dp_value(self._dp_mode)
for mode, mode_name in self._config.get(self._available_modes, {}).items():
if mode == current_mode:
self._current_mode = mode_name
break
else:
self._current_mode = "unknown"


async_setup_entry = partial(async_setup_entry, DOMAIN, LocaltuyaHumidifier, flow_schema)
3 changes: 2 additions & 1 deletion custom_components/localtuya/translations/ar.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
"step_size": "Minimum increment between numbers",
"is_passive_entity": "Passive entity? (requires integration to send initialisation value)",
"entity_category": "Show the entity in this category",
"humidifier_available_modes": "(Optional) Avaliable modes in device, separate entries by comma ','",
"humidifier_available_modes": "(Optional) Avaliable modes in device",
"humidifier_current_humidity_dp": "(Optional) Current Humidity DP",
"humidifier_mode_dp": "(Optional) Set mode DP",
"humidifier_set_humidity_dp": "(Optional) Set Humidity DP",
Expand All @@ -237,6 +237,7 @@
"preset_set":"Each line represents [ device_value: friendly name ]",
"scene_values":"Each line represents [ device_value: friendly name ]",
"select_options":"Each line represents [ device_value: friendly name ]",
"humidifier_available_modes":"Each line represents [ device_value: friendly name ]",
"device_class": "Find out more about [Device Classes](https://www.home-assistant.io/docs/configuration/customizing-devices/#device-class)",
"state_class": "Find out more about [State Classes](https://developers.home-assistant.io/docs/core/entity/sensor/#available-state-classes)"
}
Expand Down

0 comments on commit c5218f8

Please sign in to comment.