test_goldair_geco_heater.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. from unittest import IsolatedAsyncioTestCase, skip
  2. from unittest.mock import AsyncMock, patch
  3. from homeassistant.components.climate.const import (
  4. HVAC_MODE_HEAT,
  5. HVAC_MODE_OFF,
  6. SUPPORT_TARGET_TEMPERATURE,
  7. )
  8. from homeassistant.components.lock import STATE_LOCKED, STATE_UNLOCKED
  9. from homeassistant.const import STATE_UNAVAILABLE
  10. from custom_components.tuya_local.generic.climate import TuyaLocalClimate
  11. from custom_components.tuya_local.generic.lock import TuyaLocalLock
  12. from custom_components.tuya_local.helpers.device_config import TuyaDeviceConfig
  13. from ..const import GECO_HEATER_PAYLOAD
  14. from ..helpers import assert_device_properties_set
  15. HVACMODE_DPS = "1"
  16. LOCK_DPS = "2"
  17. TEMPERATURE_DPS = "3"
  18. CURRENTTEMP_DPS = "4"
  19. TIMER_DPS = "5"
  20. ERROR_DPS = "6"
  21. class TestGoldairGECOHeater(IsolatedAsyncioTestCase):
  22. def setUp(self):
  23. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  24. self.addCleanup(device_patcher.stop)
  25. self.mock_device = device_patcher.start()
  26. cfg = TuyaDeviceConfig("goldair_geco_heater.yaml")
  27. climate = cfg.primary_entity
  28. lock = None
  29. for e in cfg.secondary_entities():
  30. if e.entity == "lock":
  31. lock = e
  32. self.climate_name = climate.name
  33. self.lock_name = "missing" if lock is None else lock.name
  34. self.subject = TuyaLocalClimate(self.mock_device, climate)
  35. self.lock = None if lock is None else TuyaLocalLock(self.mock_device, lock)
  36. self.dps = GECO_HEATER_PAYLOAD.copy()
  37. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  38. def test_supported_features(self):
  39. self.assertEqual(
  40. self.subject.supported_features,
  41. SUPPORT_TARGET_TEMPERATURE,
  42. )
  43. def test_should_poll(self):
  44. self.assertTrue(self.subject.should_poll)
  45. self.assertTrue(self.lock.should_poll)
  46. def test_name_returns_device_name(self):
  47. self.assertEqual(self.subject.name, self.subject._device.name)
  48. self.assertEqual(self.lock.name, self.subject._device.name)
  49. def test_friendly_name_returns_config_name(self):
  50. self.assertEqual(self.subject.friendly_name, self.climate_name)
  51. self.assertEqual(self.lock.friendly_name, self.lock_name)
  52. def test_unique_id_returns_device_unique_id(self):
  53. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  54. self.assertEqual(self.lock.unique_id, self.subject._device.unique_id)
  55. def test_device_info_returns_device_info_from_device(self):
  56. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  57. self.assertEqual(self.lock.device_info, self.subject._device.device_info)
  58. def test_icon(self):
  59. self.dps[HVACMODE_DPS] = True
  60. self.assertEqual(self.subject.icon, "mdi:radiator")
  61. self.dps[HVACMODE_DPS] = False
  62. self.assertEqual(self.subject.icon, "mdi:radiator-disabled")
  63. def test_temperature_unit_returns_device_temperature_unit(self):
  64. self.assertEqual(
  65. self.subject.temperature_unit, self.subject._device.temperature_unit
  66. )
  67. def test_target_temperature(self):
  68. self.dps[TEMPERATURE_DPS] = 25
  69. self.assertEqual(self.subject.target_temperature, 25)
  70. def test_target_temperature_step(self):
  71. self.assertEqual(self.subject.target_temperature_step, 1)
  72. def test_minimum_target_temperature(self):
  73. self.assertEqual(self.subject.min_temp, 15)
  74. def test_maximum_target_temperature(self):
  75. self.assertEqual(self.subject.max_temp, 35)
  76. async def test_legacy_set_temperature_with_temperature(self):
  77. async with assert_device_properties_set(
  78. self.subject._device, {TEMPERATURE_DPS: 24}
  79. ):
  80. await self.subject.async_set_temperature(temperature=24)
  81. async def test_legacy_set_temperature_with_no_valid_properties(self):
  82. await self.subject.async_set_temperature(something="else")
  83. self.subject._device.async_set_property.assert_not_called
  84. async def test_set_target_temperature_succeeds_within_valid_range(self):
  85. async with assert_device_properties_set(
  86. self.subject._device,
  87. {TEMPERATURE_DPS: 25},
  88. ):
  89. await self.subject.async_set_target_temperature(25)
  90. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  91. async with assert_device_properties_set(
  92. self.subject._device, {TEMPERATURE_DPS: 23}
  93. ):
  94. await self.subject.async_set_target_temperature(22.6)
  95. async def test_set_target_temperature_fails_outside_valid_range(self):
  96. with self.assertRaisesRegex(
  97. ValueError, "temperature \\(14\\) must be between 15 and 35"
  98. ):
  99. await self.subject.async_set_target_temperature(14)
  100. with self.assertRaisesRegex(
  101. ValueError, "temperature \\(36\\) must be between 15 and 35"
  102. ):
  103. await self.subject.async_set_target_temperature(36)
  104. def test_current_temperature(self):
  105. self.dps[CURRENTTEMP_DPS] = 25
  106. self.assertEqual(self.subject.current_temperature, 25)
  107. def test_hvac_mode(self):
  108. self.dps[HVACMODE_DPS] = True
  109. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  110. self.dps[HVACMODE_DPS] = False
  111. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  112. self.dps[HVACMODE_DPS] = None
  113. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  114. def test_hvac_modes(self):
  115. self.assertCountEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_HEAT])
  116. async def test_turn_on(self):
  117. async with assert_device_properties_set(
  118. self.subject._device, {HVACMODE_DPS: True}
  119. ):
  120. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  121. async def test_turn_off(self):
  122. async with assert_device_properties_set(
  123. self.subject._device, {HVACMODE_DPS: False}
  124. ):
  125. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  126. def test_state_attributes(self):
  127. # There are currently no known error states; update this as
  128. # they are discovered
  129. self.dps[ERROR_DPS] = "something"
  130. self.dps[TIMER_DPS] = 10
  131. self.assertCountEqual(
  132. self.subject.device_state_attributes, {"error": "something", "timer": 10}
  133. )
  134. self.dps[ERROR_DPS] = "0"
  135. self.dps[TIMER_DPS] = 0
  136. self.assertCountEqual(
  137. self.subject.device_state_attributes, {"error": "OK", "timer": 0}
  138. )
  139. async def test_update(self):
  140. result = AsyncMock()
  141. self.subject._device.async_refresh.return_value = result()
  142. await self.subject.async_update()
  143. self.subject._device.async_refresh.assert_called_once()
  144. result.assert_awaited()
  145. def test_lock_was_created(self):
  146. self.assertIsInstance(self.lock, TuyaLocalLock)
  147. def test_lock_is_same_device(self):
  148. self.assertEqual(self.lock._device, self.subject._device)
  149. def test_lock_state(self):
  150. self.dps[LOCK_DPS] = True
  151. self.assertEqual(self.lock.state, STATE_LOCKED)
  152. self.dps[LOCK_DPS] = False
  153. self.assertEqual(self.lock.state, STATE_UNLOCKED)
  154. self.dps[LOCK_DPS] = None
  155. self.assertEqual(self.lock.state, STATE_UNAVAILABLE)
  156. def test_lock_is_locked(self):
  157. self.dps[LOCK_DPS] = True
  158. self.assertTrue(self.lock.is_locked)
  159. self.dps[LOCK_DPS] = False
  160. self.assertFalse(self.lock.is_locked)
  161. self.dps[LOCK_DPS] = None
  162. self.assertFalse(self.lock.is_locked)
  163. async def test_lock_locks(self):
  164. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: True}):
  165. await self.lock.async_lock()
  166. async def test_lock_unlocks(self):
  167. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: False}):
  168. await self.lock.async_unlock()
  169. async def test_lock_update(self):
  170. result = AsyncMock()
  171. self.lock._device.async_refresh.return_value = result()
  172. await self.lock.async_update()
  173. self.lock._device.async_refresh.assert_called_once()
  174. result.assert_awaited()