| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- from unittest import IsolatedAsyncioTestCase, skip
- from unittest.mock import AsyncMock, patch
- from homeassistant.components.climate.const import (
- FAN_HIGH,
- FAN_MEDIUM,
- FAN_LOW,
- HVAC_MODE_DRY,
- HVAC_MODE_OFF,
- SUPPORT_FAN_MODE,
- SUPPORT_PRESET_MODE,
- SUPPORT_TARGET_HUMIDITY,
- )
- from homeassistant.components.fan import (
- SUPPORT_SET_SPEED,
- )
- from homeassistant.components.humidifier.const import (
- MODE_NORMAL,
- MODE_AUTO,
- MODE_SLEEP,
- SUPPORT_MODES,
- )
- from homeassistant.components.switch import DEVICE_CLASS_SWITCH
- from homeassistant.const import STATE_UNAVAILABLE
- from custom_components.tuya_local.generic.climate import TuyaLocalClimate
- from custom_components.tuya_local.generic.fan import TuyaLocalFan
- from custom_components.tuya_local.generic.humidifier import TuyaLocalHumidifier
- from custom_components.tuya_local.generic.switch import TuyaLocalSwitch
- from custom_components.tuya_local.helpers.device_config import TuyaDeviceConfig
- from ..const import EANONS_HUMIDIFIER_PAYLOAD
- from ..helpers import assert_device_properties_set
- FANMODE_DPS = "2"
- TIMERHR_DPS = "3"
- TIMER_DPS = "4"
- ERROR_DPS = "9"
- HVACMODE_DPS = "10"
- PRESET_DPS = "12"
- HUMIDITY_DPS = "15"
- CURRENTHUMID_DPS = "16"
- SWITCH_DPS = "22"
- class TestEanonsHumidifier(IsolatedAsyncioTestCase):
- def setUp(self):
- device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
- self.addCleanup(device_patcher.stop)
- self.mock_device = device_patcher.start()
- cfg = TuyaDeviceConfig("eanons_humidifier.yaml")
- entities = {}
- entities[cfg.primary_entity.entity] = cfg.primary_entity
- for e in cfg.secondary_entities():
- entities[e.entity] = e
- self.climate_name = (
- "missing" if "climate" not in entities else entities["climate"].name
- )
- self.switch_name = (
- "missing" if "switch" not in entities else entities["switch"].name
- )
- self.humidifier_name = (
- "missing" if "humidifier" not in entities else entities["humidifier"].name
- )
- self.fan_name = "missing" if "fan" not in entities else entities["fan"].name
- self.subject = TuyaLocalHumidifier(
- self.mock_device(), entities.get("humidifier")
- )
- self.climate = TuyaLocalClimate(self.mock_device(), entities.get("climate"))
- self.switch = TuyaLocalSwitch(self.mock_device(), entities.get("switch"))
- self.fan = TuyaLocalFan(self.mock_device(), entities.get("fan"))
- self.dps = EANONS_HUMIDIFIER_PAYLOAD.copy()
- self.subject._device.get_property.side_effect = lambda id: self.dps[id]
- def test_supported_features(self):
- self.assertEqual(
- self.climate.supported_features,
- SUPPORT_TARGET_HUMIDITY | SUPPORT_PRESET_MODE | SUPPORT_FAN_MODE,
- )
- self.assertEqual(self.subject.supported_features, SUPPORT_MODES)
- self.assertEqual(self.fan.supported_features, SUPPORT_SET_SPEED)
- def test_shouldPoll(self):
- self.assertTrue(self.subject.should_poll)
- self.assertTrue(self.climate.should_poll)
- self.assertTrue(self.fan.should_poll)
- self.assertTrue(self.switch.should_poll)
- def test_name_returns_device_name(self):
- self.assertEqual(self.subject.name, self.subject._device.name)
- self.assertEqual(self.climate.name, self.subject._device.name)
- self.assertEqual(self.fan.name, self.subject._device.name)
- self.assertEqual(self.switch.name, self.subject._device.name)
- def test_friendly_name_returns_config_name(self):
- self.assertEqual(self.subject.friendly_name, self.humidifier_name)
- self.assertEqual(self.climate.friendly_name, self.climate_name)
- self.assertEqual(self.fan.friendly_name, self.fan_name)
- self.assertEqual(self.switch.friendly_name, self.switch_name)
- def test_unique_id_returns_device_unique_id(self):
- self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
- self.assertEqual(self.climate.unique_id, self.subject._device.unique_id)
- self.assertEqual(self.fan.unique_id, self.subject._device.unique_id)
- self.assertEqual(self.switch.unique_id, self.subject._device.unique_id)
- def test_device_info_returns_device_info_from_device(self):
- self.assertEqual(self.subject.device_info, self.subject._device.device_info)
- self.assertEqual(self.climate.device_info, self.subject._device.device_info)
- self.assertEqual(self.fan.device_info, self.subject._device.device_info)
- self.assertEqual(self.switch.device_info, self.subject._device.device_info)
- def test_climate_icon_is_humidifier(self):
- """Test that the icon is as expected."""
- self.dps[HVACMODE_DPS] = True
- self.assertEqual(self.climate.icon, "mdi:air-humidifier")
- self.dps[HVACMODE_DPS] = False
- self.assertEqual(self.climate.icon, "mdi:air-humidifier-off")
- def test_icon_is_humidifier(self):
- """Test that the icon is as expected."""
- self.dps[HVACMODE_DPS] = True
- self.assertEqual(self.subject.icon, "mdi:air-humidifier")
- self.dps[HVACMODE_DPS] = False
- self.assertEqual(self.subject.icon, "mdi:air-humidifier-off")
- def test_current_humidity(self):
- self.dps[CURRENTHUMID_DPS] = 47
- self.assertEqual(self.climate.current_humidity, 47)
- def test_min_target_humidity(self):
- self.assertEqual(self.climate.min_humidity, 40)
- self.assertEqual(self.subject.min_humidity, 40)
- def test_max_target_humidity(self):
- self.assertEqual(self.climate.max_humidity, 90)
- self.assertEqual(self.subject.max_humidity, 90)
- def test_target_humidity(self):
- self.dps[HUMIDITY_DPS] = 55
- self.assertEqual(self.climate.target_humidity, 55)
- self.assertEqual(self.subject.target_humidity, 55)
- def test_climate_hvac_mode(self):
- self.dps[HVACMODE_DPS] = True
- self.assertEqual(self.climate.hvac_mode, HVAC_MODE_DRY)
- self.dps[HVACMODE_DPS] = False
- self.assertEqual(self.climate.hvac_mode, HVAC_MODE_OFF)
- self.dps[HVACMODE_DPS] = None
- self.assertEqual(self.climate.hvac_mode, STATE_UNAVAILABLE)
- def test_climate_hvac_modes(self):
- self.assertCountEqual(self.climate.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_DRY])
- def test_is_on(self):
- self.dps[HVACMODE_DPS] = True
- self.assertTrue(self.subject.is_on)
- self.assertTrue(self.fan.is_on)
- self.dps[HVACMODE_DPS] = False
- self.assertFalse(self.subject.is_on)
- self.assertFalse(self.fan.is_on)
- self.dps[HVACMODE_DPS] = None
- self.assertEqual(self.subject.is_on, STATE_UNAVAILABLE)
- self.assertEqual(self.fan.is_on, STATE_UNAVAILABLE)
- async def test_climate_turn_on(self):
- async with assert_device_properties_set(
- self.climate._device, {HVACMODE_DPS: True}
- ):
- await self.climate.async_set_hvac_mode(HVAC_MODE_DRY)
- async def test_climate_turn_off(self):
- async with assert_device_properties_set(
- self.climate._device, {HVACMODE_DPS: False}
- ):
- await self.climate.async_set_hvac_mode(HVAC_MODE_OFF)
- async def test_turn_on(self):
- async with assert_device_properties_set(
- self.subject._device, {HVACMODE_DPS: True}
- ):
- await self.subject.async_turn_on()
- async def test_turn_off(self):
- async with assert_device_properties_set(
- self.subject._device, {HVACMODE_DPS: False}
- ):
- await self.subject.async_turn_off()
- async def test_fan_turn_on(self):
- async with assert_device_properties_set(
- self.subject._device, {HVACMODE_DPS: True}
- ):
- await self.fan.async_turn_on()
- async def test_fan_turn_off(self):
- async with assert_device_properties_set(
- self.subject._device, {HVACMODE_DPS: False}
- ):
- await self.fan.async_turn_off()
- def test_preset_mode(self):
- self.dps[PRESET_DPS] = "sleep"
- self.assertEqual(self.climate.preset_mode, MODE_SLEEP)
- self.assertEqual(self.subject.mode, MODE_SLEEP)
- self.dps[PRESET_DPS] = "humidity"
- self.assertEqual(self.climate.preset_mode, MODE_AUTO)
- self.assertEqual(self.subject.mode, MODE_AUTO)
- self.dps[PRESET_DPS] = "work"
- self.assertEqual(self.climate.preset_mode, MODE_NORMAL)
- self.assertEqual(self.subject.mode, MODE_NORMAL)
- self.dps[PRESET_DPS] = None
- self.assertEqual(self.climate.preset_mode, None)
- self.assertEqual(self.subject.mode, None)
- def test_preset_modes(self):
- self.assertCountEqual(
- self.climate.preset_modes,
- {MODE_NORMAL, MODE_SLEEP, MODE_AUTO},
- )
- self.assertCountEqual(
- self.subject.available_modes,
- {MODE_NORMAL, MODE_SLEEP, MODE_AUTO},
- )
- async def test_set_climate_preset_to_auto(self):
- async with assert_device_properties_set(
- self.climate._device,
- {
- PRESET_DPS: "humidity",
- },
- ):
- await self.climate.async_set_preset_mode(MODE_AUTO)
- self.climate._device.anticipate_property_value.assert_not_called()
- async def test_set_climate_preset_to_sleep(self):
- async with assert_device_properties_set(
- self.climate._device,
- {
- PRESET_DPS: "sleep",
- },
- ):
- await self.climate.async_set_preset_mode(MODE_SLEEP)
- self.climate._device.anticipate_property_value.assert_not_called()
- async def test_set_climate_preset_to_normal(self):
- async with assert_device_properties_set(
- self.climate._device,
- {
- PRESET_DPS: "work",
- },
- ):
- await self.climate.async_set_preset_mode(MODE_NORMAL)
- self.climate._device.anticipate_property_value.assert_not_called()
- async def test_set_mode_to_auto(self):
- async with assert_device_properties_set(
- self.subject._device,
- {
- PRESET_DPS: "humidity",
- },
- ):
- await self.subject.async_set_mode(MODE_AUTO)
- self.subject._device.anticipate_property_value.assert_not_called()
- async def test_set_mode_to_sleep(self):
- async with assert_device_properties_set(
- self.subject._device,
- {
- PRESET_DPS: "sleep",
- },
- ):
- await self.subject.async_set_mode(MODE_SLEEP)
- self.subject._device.anticipate_property_value.assert_not_called()
- async def test_set_mode_to_normal(self):
- async with assert_device_properties_set(
- self.subject._device,
- {
- PRESET_DPS: "work",
- },
- ):
- await self.subject.async_set_mode(MODE_NORMAL)
- self.subject._device.anticipate_property_value.assert_not_called()
- def test_climate_device_state_attributes(self):
- self.dps[ERROR_DPS] = 0
- self.dps[TIMERHR_DPS] = "cancel"
- self.dps[TIMER_DPS] = 0
- self.assertCountEqual(
- self.climate.device_state_attributes,
- {
- "error": "OK",
- "timer_hr": "cancel",
- "timer_min": 0,
- },
- )
- self.dps[ERROR_DPS] = 1
- self.dps[TIMERHR_DPS] = "1"
- self.dps[TIMER_DPS] = 60
- self.assertCountEqual(
- self.climate.device_state_attributes,
- {
- "error": 1,
- "timer_hr": "1",
- "timer_min": 60,
- },
- )
- def test_device_state_attributes(self):
- self.dps[ERROR_DPS] = 0
- self.dps[TIMERHR_DPS] = "cancel"
- self.dps[TIMER_DPS] = 0
- self.dps[CURRENTHUMID_DPS] = 50
- self.dps[FANMODE_DPS] = "middle"
- self.assertCountEqual(
- self.subject.device_state_attributes,
- {
- "error": "OK",
- "timer_hr": "cancel",
- "timer_min": 0,
- "current_humidity": 50,
- },
- )
- def test_fan_speed(self):
- self.dps[FANMODE_DPS] = "small"
- self.assertEqual(self.fan.percentage, 33)
- self.dps[FANMODE_DPS] = "middle"
- self.assertEqual(self.fan.percentage, 67)
- self.dps[FANMODE_DPS] = "large"
- self.assertEqual(self.fan.percentage, 100)
- def test_climate_fan_mode(self):
- self.dps[FANMODE_DPS] = "small"
- self.assertEqual(self.climate.fan_mode, FAN_LOW)
- self.dps[FANMODE_DPS] = "middle"
- self.assertEqual(self.climate.fan_mode, FAN_MEDIUM)
- self.dps[FANMODE_DPS] = "large"
- self.assertEqual(self.climate.fan_mode, FAN_HIGH)
- self.dps[FANMODE_DPS] = None
- self.assertEqual(self.climate.fan_mode, None)
- def test_fan_speed_count(self):
- self.assertEqual(self.fan.speed_count, 3)
- def test_fan_percentage_step(self):
- self.assertAlmostEqual(self.fan.percentage_step, 33, 0)
- def test_climate_fan_modes(self):
- self.assertCountEqual(
- self.climate.fan_modes,
- {FAN_LOW, FAN_MEDIUM, FAN_HIGH},
- )
- async def test_fan_set_speed(self):
- async with assert_device_properties_set(
- self.fan._device,
- {FANMODE_DPS: "small"},
- ):
- await self.fan.async_set_percentage(33)
- async def test_fan_set_speed_snaps(self):
- async with assert_device_properties_set(
- self.fan._device,
- {FANMODE_DPS: "middle"},
- ):
- await self.fan.async_set_percentage(60)
- async def test_climate_set_fan_mode(self):
- async with assert_device_properties_set(
- self.climate._device,
- {FANMODE_DPS: "small"},
- ):
- await self.climate.async_set_fan_mode(FAN_LOW)
- def test_switch_was_created(self):
- self.assertIsInstance(self.switch, TuyaLocalSwitch)
- def test_switch_is_same_device(self):
- self.assertEqual(self.switch._device, self.climate._device)
- def test_switch_class_is_switch(self):
- self.assertEqual(self.switch.device_class, DEVICE_CLASS_SWITCH)
- def test_switch_is_on(self):
- self.dps[SWITCH_DPS] = True
- self.assertTrue(self.switch.is_on)
- self.dps[SWITCH_DPS] = False
- self.assertFalse(self.switch.is_on)
- def test_switch_is_on_when_unavailable(self):
- self.dps[SWITCH_DPS] = None
- self.assertEqual(self.switch.is_on, STATE_UNAVAILABLE)
- async def test_switch_turn_on(self):
- async with assert_device_properties_set(
- self.switch._device, {SWITCH_DPS: True}
- ):
- await self.switch.async_turn_on()
- async def test_switch_turn_off(self):
- async with assert_device_properties_set(
- self.switch._device, {SWITCH_DPS: False}
- ):
- await self.switch.async_turn_off()
- async def test_toggle_turns_the_switch_on_when_it_was_off(self):
- self.dps[SWITCH_DPS] = False
- async with assert_device_properties_set(
- self.switch._device, {SWITCH_DPS: True}
- ):
- await self.switch.async_toggle()
- async def test_toggle_turns_the_switch_off_when_it_was_on(self):
- self.dps[SWITCH_DPS] = True
- async with assert_device_properties_set(
- self.switch._device, {SWITCH_DPS: False}
- ):
- await self.switch.async_toggle()
- def test_switch_returns_none_for_power(self):
- self.assertIsNone(self.switch.current_power_w)
- def test_switch_state_attributes_set(self):
- self.assertEqual(self.switch.device_state_attributes, {})
|