test_goldair_geco_heater.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. @skip("Icon customisation not supported yet")
  59. def test_icon(self):
  60. self.dps[HVACMODE_DPS] = True
  61. self.assertEqual(self.subject.icon, "mdi:radiator")
  62. self.dps[HVACMODE_DPS] = False
  63. self.assertEqual(self.subject.icon, "mdi:radiator-disabled")
  64. def test_temperature_unit_returns_device_temperature_unit(self):
  65. self.assertEqual(
  66. self.subject.temperature_unit, self.subject._device.temperature_unit
  67. )
  68. def test_target_temperature(self):
  69. self.dps[TEMPERATURE_DPS] = 25
  70. self.assertEqual(self.subject.target_temperature, 25)
  71. def test_target_temperature_step(self):
  72. self.assertEqual(self.subject.target_temperature_step, 1)
  73. def test_minimum_target_temperature(self):
  74. self.assertEqual(self.subject.min_temp, 15)
  75. def test_maximum_target_temperature(self):
  76. self.assertEqual(self.subject.max_temp, 35)
  77. async def test_legacy_set_temperature_with_temperature(self):
  78. async with assert_device_properties_set(
  79. self.subject._device, {TEMPERATURE_DPS: 24}
  80. ):
  81. await self.subject.async_set_temperature(temperature=24)
  82. async def test_legacy_set_temperature_with_no_valid_properties(self):
  83. await self.subject.async_set_temperature(something="else")
  84. self.subject._device.async_set_property.assert_not_called
  85. async def test_set_target_temperature_succeeds_within_valid_range(self):
  86. async with assert_device_properties_set(
  87. self.subject._device,
  88. {TEMPERATURE_DPS: 25},
  89. ):
  90. await self.subject.async_set_target_temperature(25)
  91. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  92. async with assert_device_properties_set(
  93. self.subject._device, {TEMPERATURE_DPS: 23}
  94. ):
  95. await self.subject.async_set_target_temperature(22.6)
  96. async def test_set_target_temperature_fails_outside_valid_range(self):
  97. with self.assertRaisesRegex(
  98. ValueError, "Target temperature \\(14\\) must be between 15 and 35"
  99. ):
  100. await self.subject.async_set_target_temperature(14)
  101. with self.assertRaisesRegex(
  102. ValueError, "Target temperature \\(36\\) must be between 15 and 35"
  103. ):
  104. await self.subject.async_set_target_temperature(36)
  105. def test_current_temperature(self):
  106. self.dps[CURRENTTEMP_DPS] = 25
  107. self.assertEqual(self.subject.current_temperature, 25)
  108. def test_hvac_mode(self):
  109. self.dps[HVACMODE_DPS] = True
  110. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  111. self.dps[HVACMODE_DPS] = False
  112. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  113. self.dps[HVACMODE_DPS] = None
  114. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  115. def test_hvac_modes(self):
  116. self.assertCountEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_HEAT])
  117. async def test_turn_on(self):
  118. async with assert_device_properties_set(
  119. self.subject._device, {HVACMODE_DPS: True}
  120. ):
  121. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  122. async def test_turn_off(self):
  123. async with assert_device_properties_set(
  124. self.subject._device, {HVACMODE_DPS: False}
  125. ):
  126. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  127. def test_state_attributes(self):
  128. # There are currently no known error states; update this as
  129. # they are discovered
  130. self.dps[ERROR_DPS] = "something"
  131. self.dps[TIMER_DPS] = 10
  132. self.assertCountEqual(
  133. self.subject.device_state_attributes, {"error": "something", "timer": 10}
  134. )
  135. self.dps[ERROR_DPS] = "0"
  136. self.dps[TIMER_DPS] = 0
  137. self.assertCountEqual(
  138. self.subject.device_state_attributes, {"error": "OK", "timer": 0}
  139. )
  140. async def test_update(self):
  141. result = AsyncMock()
  142. self.subject._device.async_refresh.return_value = result()
  143. await self.subject.async_update()
  144. self.subject._device.async_refresh.assert_called_once()
  145. result.assert_awaited()
  146. def test_lock_was_created(self):
  147. self.assertIsInstance(self.lock, TuyaLocalLock)
  148. def test_lock_is_same_device(self):
  149. self.assertEqual(self.lock._device, self.subject._device)
  150. def test_lock_state(self):
  151. self.dps[LOCK_DPS] = True
  152. self.assertEqual(self.lock.state, STATE_LOCKED)
  153. self.dps[LOCK_DPS] = False
  154. self.assertEqual(self.lock.state, STATE_UNLOCKED)
  155. self.dps[LOCK_DPS] = None
  156. self.assertEqual(self.lock.state, STATE_UNAVAILABLE)
  157. def test_lock_is_locked(self):
  158. self.dps[LOCK_DPS] = True
  159. self.assertTrue(self.lock.is_locked)
  160. self.dps[LOCK_DPS] = False
  161. self.assertFalse(self.lock.is_locked)
  162. self.dps[LOCK_DPS] = None
  163. self.assertFalse(self.lock.is_locked)
  164. async def async_test_lock_locks(self):
  165. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: True}):
  166. await self.subject.async_lock()
  167. async def async_test_lock_unlocks(self):
  168. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: False}):
  169. await self.subject.async_unlock()
  170. async def async_test_lock_update(self):
  171. result = AsyncMock()
  172. self.lock._device.async_refresh.return_value = result()
  173. await self.lock.async_update()
  174. self.lock._device.async_refresh.assert_called_once()
  175. result.assert_awaited()