Skip to content

Commit

Permalink
Humidifier turn display off for sleep mode (#137133)
Browse files Browse the repository at this point in the history
  • Loading branch information
iprak authored Feb 4, 2025
1 parent 7f69c68 commit 9a56588
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
6 changes: 5 additions & 1 deletion homeassistant/components/vesync/humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,15 @@ def set_mode(self, mode: str) -> None:
"""Set the mode of the device."""
if mode not in self.available_modes:
raise HomeAssistantError(
"{mode} is not one of the valid available modes: {self.available_modes}"
f"{mode} is not one of the valid available modes: {self.available_modes}"
)
if not self.device.set_humidity_mode(self._get_vs_mode(mode)):
raise HomeAssistantError(f"An error occurred while setting mode {mode}.")

if mode == MODE_SLEEP:
# We successfully changed the mode. Consider it a success even if display operation fails.
self.device.set_display(False)

# 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.
Expand Down
44 changes: 43 additions & 1 deletion tests/components/vesync/test_humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@
ATTR_HUMIDITY,
ATTR_MODE,
DOMAIN as HUMIDIFIER_DOMAIN,
MODE_AUTO,
MODE_SLEEP,
SERVICE_SET_HUMIDITY,
SERVICE_SET_MODE,
)
from homeassistant.components.vesync.const import (
VS_HUMIDIFIER_MODE_AUTO,
VS_HUMIDIFIER_MODE_MANUAL,
VS_HUMIDIFIER_MODE_SLEEP,
)
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
from homeassistant.const import (
ATTR_ENTITY_ID,
Expand Down Expand Up @@ -222,7 +229,7 @@ async def test_set_mode(
await hass.services.async_call(
HUMIDIFIER_DOMAIN,
SERVICE_SET_MODE,
{ATTR_ENTITY_ID: ENTITY_HUMIDIFIER, ATTR_MODE: "auto"},
{ATTR_ENTITY_ID: ENTITY_HUMIDIFIER, ATTR_MODE: MODE_AUTO},
blocking=True,
)
await hass.async_block_till_done()
Expand Down Expand Up @@ -285,3 +292,38 @@ async def test_valid_mist_modes(
await hass.async_block_till_done()
assert "Unknown mode 'auto'" not in caplog.text
assert "Unknown mode 'manual'" not in caplog.text


async def test_set_mode_sleep_turns_display_off(
hass: HomeAssistant,
config_entry: ConfigEntry,
humidifier,
manager,
) -> None:
"""Test update of display for sleep mode."""

# First define valid mist modes
humidifier.mist_modes = [
VS_HUMIDIFIER_MODE_AUTO,
VS_HUMIDIFIER_MODE_MANUAL,
VS_HUMIDIFIER_MODE_SLEEP,
]

with patch(
"homeassistant.components.vesync.async_generate_device_list",
return_value=[humidifier],
):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

with (
patch.object(humidifier, "set_humidity_mode", return_value=True),
patch.object(humidifier, "set_display") as display_mock,
):
await hass.services.async_call(
HUMIDIFIER_DOMAIN,
SERVICE_SET_MODE,
{ATTR_ENTITY_ID: ENTITY_HUMIDIFIER, ATTR_MODE: MODE_SLEEP},
blocking=True,
)
display_mock.assert_called_once_with(False)

0 comments on commit 9a56588

Please sign in to comment.