Skip to content

Commit

Permalink
💦 add mode_property option for humidifier (#1421)
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Dec 16, 2023
1 parent 91c5393 commit 9cf54b4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
5 changes: 5 additions & 0 deletions custom_components/xiaomi_miot/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,11 @@ def get_customize_options(hass, options={}, bool2selects=[], entity_id='', model
if domain == 'number':
bool2selects.extend(['restore_state'])

if domain == 'humidifier':
options.update({
'mode_property': cv.string,
})

if domain == 'device_tracker' or re.search(r'watch', model, re.I):
options.update({
'coord_type': cv.string,
Expand Down
35 changes: 19 additions & 16 deletions custom_components/xiaomi_miot/humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ def __init__(self, config: dict, miot_service: MiotService):
super().__init__(miot_service, config=config, logger=_LOGGER)

self._prop_power = miot_service.get_property('on')
self._prop_mode = miot_service.get_property('mode')
self._prop_fan_level = miot_service.get_property('fan_level')
self._prop_water_level = miot_service.get_property('water_level')
self._prop_temperature = miot_service.get_property('temperature')
self._prop_target_humi = miot_service.get_property('target_humidity')
Expand All @@ -70,13 +68,18 @@ def __init__(self, config: dict, miot_service: MiotService):
self._prop_target_humi = self._environment.get_property('target_humidity') or self._prop_target_humi
self._prop_humidity = self._environment.get_property('relative_humidity', 'humidity') or self._prop_humidity

self._humidifier_mode = None
self._mode_props = [self._prop_mode, self._prop_fan_level]
self._mode_props = list(filter(lambda x: x, self._mode_props))
self._prop_mode = None
self._mode_props = list(filter(lambda x: x, [
miot_service.get_property('mode'),
miot_service.get_property('fan_level'),
]))
if self._mode_props:
self._humidifier_mode = self._mode_props.pop(0)
self._prop_mode = self._mode_props.pop(0)
if prop := self.custom_config('mode_property'):
if prop := self._miot_service.spec.get_property(prop):
self._prop_mode = prop
if self._prop_mode:
self._supported_features = HumidifierEntityFeature.MODES

async def async_added_to_hass(self):
await super().async_added_to_hass()
self._vars['target_humidity_ratio'] = self.custom_config_number('target_humidity_ratio')
Expand All @@ -103,7 +106,7 @@ async def async_update(self):
add_fans = self._add_entities.get('fan')
for p in self._mode_props:
pnm = p.full_name
if self._humidifier_mode and pnm == self._humidifier_mode.full_name:
if self._prop_mode and pnm == self._prop_mode.full_name:
continue
if pnm in self._subs:
self._subs[pnm].update_from_parent()
Expand Down Expand Up @@ -170,26 +173,26 @@ def max_humidity(self):
def mode(self):
if not self.is_on:
return MODE_OFF
if not self._humidifier_mode:
if not self._prop_mode:
return None
val = self._humidifier_mode.from_dict(self._state_attrs)
val = self._prop_mode.from_dict(self._state_attrs)
if val is None:
return None
return self._humidifier_mode.list_description(val)
return self._prop_mode.list_description(val)

@property
def available_modes(self):
mds = [MODE_OFF]
if self._humidifier_mode:
mds.extend(self._humidifier_mode.list_descriptions() or [])
if self._prop_mode:
mds.extend(self._prop_mode.list_descriptions() or [])
return mds

def set_mode(self, mode: str):
if mode == MODE_OFF:
return self.turn_off()
if not self._humidifier_mode:
if not self._prop_mode:
return False
val = self._humidifier_mode.list_value(mode)
val = self._prop_mode.list_value(mode)
if val is None:
return False
return self.set_property(self._humidifier_mode, val)
return self.set_property(self._prop_mode, val)

0 comments on commit 9cf54b4

Please sign in to comment.