Skip to content

Commit

Permalink
Fix unsupported fan_mode for esphome climate device (close dext0r#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
dext0r committed Mar 31, 2023
1 parent 3ee03c2 commit 367100a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
21 changes: 18 additions & 3 deletions custom_components/yandex_smart_home/capability_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,15 @@ def get_ha_mode_by_yandex_mode(self, yandex_mode: str) -> str:

def get_value(self) -> str | None:
"""Return the state value of this capability for this entity."""
ha_mode = self.state.state
return self.get_yandex_mode_by_ha_mode(self._ha_value, False)

@property
def _ha_value(self):
"""Return the current unmapped capability value."""
if self.state_value_attribute:
ha_mode = self.state.attributes.get(self.state_value_attribute)
return self.get_yandex_mode_by_ha_mode(ha_mode, False)
return self.state.attributes.get(self.state_value_attribute)

return self.state.state


@register_capability
Expand Down Expand Up @@ -524,6 +529,16 @@ def modes_list_attribute(self) -> str | None:
"""Return HA attribute contains modes list for this entity."""
return climate.ATTR_FAN_MODES

@property
def supported_ha_modes(self) -> list[str]:
modes = super().supported_ha_modes

# esphome default state for some devices
if self._ha_value == climate.const.FAN_ON and climate.const.FAN_ON not in modes:
modes.append(climate.const.FAN_ON)

return modes

async def set_state(self, data: RequestData, state: dict[str, Any]):
"""Set device state."""
await self.hass.services.async_call(
Expand Down
13 changes: 12 additions & 1 deletion tests/test_capability_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ async def test_capability_mode_fan_speed_fan_via_preset(hass, features):
assert calls[0].data == {ATTR_ENTITY_ID: state.entity_id, fan.ATTR_PRESET_MODE: 'Level 4'}


async def test_capability_mode_fan_speed_climate(hass):
async def test_capability_mode_fan_speed_climate(hass, caplog):
state = State('climate.test', STATE_OFF)
assert_no_capabilities(hass, BASIC_CONFIG, state, CAPABILITIES_MODE, MODE_INSTANCE_FAN_SPEED)

Expand All @@ -595,6 +595,17 @@ async def test_capability_mode_fan_speed_climate(hass):
cap = get_exact_one_capability(hass, BASIC_CONFIG, state, CAPABILITIES_MODE, MODE_INSTANCE_FAN_SPEED)
assert cap.get_value() == 'medium'

state = State('climate.test', STATE_OFF, {
ATTR_SUPPORTED_FEATURES: climate.ClimateEntityFeature.FAN_MODE,
climate.ATTR_FAN_MODES: ['3', '2'],
climate.ATTR_FAN_MODE: 'on',
})

cap = get_exact_one_capability(hass, BASIC_CONFIG, state, CAPABILITIES_MODE, MODE_INSTANCE_FAN_SPEED)
caplog.clear()
assert cap.get_value() == 'auto'
assert len(caplog.records) == 0

calls = async_mock_service(hass, climate.DOMAIN, climate.SERVICE_SET_FAN_MODE)
await cap.set_state(BASIC_DATA, {'value': 'low'})
assert len(calls) == 1
Expand Down

0 comments on commit 367100a

Please sign in to comment.