test_kogan_heater.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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_PRESET_MODE,
  7. SUPPORT_TARGET_TEMPERATURE,
  8. )
  9. from homeassistant.components.lock import STATE_LOCKED, STATE_UNLOCKED
  10. from homeassistant.const import STATE_UNAVAILABLE
  11. from custom_components.tuya_local.generic.climate import TuyaLocalClimate
  12. from custom_components.tuya_local.generic.lock import TuyaLocalLock
  13. from custom_components.tuya_local.helpers.device_config import TuyaDeviceConfig
  14. from ..const import KOGAN_HEATER_PAYLOAD
  15. from ..helpers import assert_device_properties_set
  16. TEMPERATURE_DPS = "2"
  17. CURRENTTEMP_DPS = "3"
  18. PRESET_DPS = "4"
  19. LOCK_DPS = "6"
  20. HVACMODE_DPS = "7"
  21. TIMER_DPS = "8"
  22. class TestGoldairKoganHeater(IsolatedAsyncioTestCase):
  23. def setUp(self):
  24. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  25. self.addCleanup(device_patcher.stop)
  26. self.mock_device = device_patcher.start()
  27. cfg = TuyaDeviceConfig("kogan_heater.yaml")
  28. climate = cfg.primary_entity
  29. lock = None
  30. for e in cfg.secondary_entities():
  31. if e.entity == "lock":
  32. lock = e
  33. self.climate_name = climate.name
  34. self.lock_name = "missing" if lock is None else lock.name
  35. self.subject = TuyaLocalClimate(self.mock_device, climate)
  36. self.lock = None if lock is None else TuyaLocalLock(self.mock_device, lock)
  37. self.dps = KOGAN_HEATER_PAYLOAD.copy()
  38. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  39. def test_supported_features(self):
  40. self.assertEqual(
  41. self.subject.supported_features,
  42. SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE,
  43. )
  44. def test_should_poll(self):
  45. self.assertTrue(self.subject.should_poll)
  46. self.assertTrue(self.lock.should_poll)
  47. def test_name_returns_device_name(self):
  48. self.assertEqual(self.subject.name, self.subject._device.name)
  49. self.assertEqual(self.lock.name, self.subject._device.name)
  50. def test_friendly_name_returns_config_name(self):
  51. self.assertEqual(self.subject.friendly_name, self.climate_name)
  52. self.assertEqual(self.lock.friendly_name, self.lock_name)
  53. def test_unique_id_returns_device_unique_id(self):
  54. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  55. self.assertEqual(self.lock.unique_id, self.subject._device.unique_id)
  56. def test_device_info_returns_device_info_from_device(self):
  57. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  58. self.assertEqual(self.lock.device_info, self.subject._device.device_info)
  59. @skip("Icon customisation not supported yet")
  60. def test_icon(self):
  61. self.dps[HVACMODE_DPS] = True
  62. self.assertEqual(self.subject.icon, "mdi:radiator")
  63. self.dps[HVACMODE_DPS] = False
  64. self.assertEqual(self.subject.icon, "mdi:radiator-disabled")
  65. def test_temperature_unit_returns_device_temperature_unit(self):
  66. self.assertEqual(
  67. self.subject.temperature_unit, self.subject._device.temperature_unit
  68. )
  69. def test_target_temperature(self):
  70. self.dps[TEMPERATURE_DPS] = 25
  71. self.assertEqual(self.subject.target_temperature, 25)
  72. def test_target_temperature_step(self):
  73. self.assertEqual(self.subject.target_temperature_step, 1)
  74. def test_minimum_target_temperature(self):
  75. self.assertEqual(self.subject.min_temp, 15)
  76. def test_maximum_target_temperature(self):
  77. self.assertEqual(self.subject.max_temp, 35)
  78. async def test_legacy_set_temperature_with_temperature(self):
  79. async with assert_device_properties_set(
  80. self.subject._device, {TEMPERATURE_DPS: 24}
  81. ):
  82. await self.subject.async_set_temperature(temperature=24)
  83. async def test_legacy_set_temperature_with_preset_mode(self):
  84. async with assert_device_properties_set(
  85. self.subject._device, {PRESET_DPS: "Low"}
  86. ):
  87. await self.subject.async_set_temperature(preset_mode="Low")
  88. async def test_legacy_set_temperature_with_both_properties(self):
  89. async with assert_device_properties_set(
  90. self.subject._device, {TEMPERATURE_DPS: 26, PRESET_DPS: "High"}
  91. ):
  92. await self.subject.async_set_temperature(temperature=26, preset_mode="High")
  93. async def test_legacy_set_temperature_with_no_valid_properties(self):
  94. await self.subject.async_set_temperature(something="else")
  95. self.subject._device.async_set_property.assert_not_called
  96. async def test_set_target_temperature_succeeds_within_valid_range(self):
  97. async with assert_device_properties_set(
  98. self.subject._device,
  99. {TEMPERATURE_DPS: 25},
  100. ):
  101. await self.subject.async_set_target_temperature(25)
  102. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  103. async with assert_device_properties_set(
  104. self.subject._device, {TEMPERATURE_DPS: 23}
  105. ):
  106. await self.subject.async_set_target_temperature(22.6)
  107. async def test_set_target_temperature_fails_outside_valid_range(self):
  108. with self.assertRaisesRegex(
  109. ValueError, "temperature \\(14\\) must be between 15 and 35"
  110. ):
  111. await self.subject.async_set_target_temperature(14)
  112. with self.assertRaisesRegex(
  113. ValueError, "temperature \\(36\\) must be between 15 and 35"
  114. ):
  115. await self.subject.async_set_target_temperature(36)
  116. def test_current_temperature(self):
  117. self.dps[CURRENTTEMP_DPS] = 25
  118. self.assertEqual(self.subject.current_temperature, 25)
  119. def test_hvac_mode(self):
  120. self.dps[HVACMODE_DPS] = True
  121. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  122. self.dps[HVACMODE_DPS] = False
  123. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  124. self.dps[HVACMODE_DPS] = None
  125. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  126. def test_hvac_modes(self):
  127. self.assertCountEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_HEAT])
  128. async def test_turn_on(self):
  129. async with assert_device_properties_set(
  130. self.subject._device, {HVACMODE_DPS: True}
  131. ):
  132. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  133. async def test_turn_off(self):
  134. async with assert_device_properties_set(
  135. self.subject._device, {HVACMODE_DPS: False}
  136. ):
  137. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  138. def test_preset_mode(self):
  139. self.dps[PRESET_DPS] = "Low"
  140. self.assertEqual(self.subject.preset_mode, "Low")
  141. self.dps[PRESET_DPS] = "High"
  142. self.assertEqual(self.subject.preset_mode, "High")
  143. self.dps[PRESET_DPS] = None
  144. self.assertIs(self.subject.preset_mode, None)
  145. def test_preset_modes(self):
  146. self.assertCountEqual(self.subject.preset_modes, ["Low", "High"])
  147. async def test_set_preset_mode_to_low(self):
  148. async with assert_device_properties_set(
  149. self.subject._device,
  150. {PRESET_DPS: "Low"},
  151. ):
  152. await self.subject.async_set_preset_mode("Low")
  153. async def test_set_preset_mode_to_high(self):
  154. async with assert_device_properties_set(
  155. self.subject._device,
  156. {PRESET_DPS: "High"},
  157. ):
  158. await self.subject.async_set_preset_mode("High")
  159. def test_state_attributes(self):
  160. self.dps[TIMER_DPS] = 10
  161. self.assertCountEqual(self.subject.device_state_attributes, {"timer": 10})
  162. self.dps[TIMER_DPS] = 0
  163. self.assertCountEqual(self.subject.device_state_attributes, {"timer": 0})
  164. async def test_update(self):
  165. result = AsyncMock()
  166. self.subject._device.async_refresh.return_value = result()
  167. await self.subject.async_update()
  168. self.subject._device.async_refresh.assert_called_once()
  169. result.assert_awaited()
  170. def test_lock_was_created(self):
  171. self.assertIsInstance(self.lock, TuyaLocalLock)
  172. def test_lock_is_same_device(self):
  173. self.assertEqual(self.lock._device, self.subject._device)
  174. def test_lock_state(self):
  175. self.dps[LOCK_DPS] = True
  176. self.assertEqual(self.lock.state, STATE_LOCKED)
  177. self.dps[LOCK_DPS] = False
  178. self.assertEqual(self.lock.state, STATE_UNLOCKED)
  179. self.dps[LOCK_DPS] = None
  180. self.assertEqual(self.lock.state, STATE_UNAVAILABLE)
  181. def test_lock_is_locked(self):
  182. self.dps[LOCK_DPS] = True
  183. self.assertTrue(self.lock.is_locked)
  184. self.dps[LOCK_DPS] = False
  185. self.assertFalse(self.lock.is_locked)
  186. self.dps[LOCK_DPS] = None
  187. self.assertFalse(self.lock.is_locked)
  188. async def async_test_lock_locks(self):
  189. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: True}):
  190. await self.subject.async_lock()
  191. async def async_test_lock_unlocks(self):
  192. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: False}):
  193. await self.subject.async_unlock()
  194. async def async_test_lock_update(self):
  195. result = AsyncMock()
  196. self.lock._device.async_refresh.return_value = result()
  197. await self.lock.async_update()
  198. self.lock._device.async_refresh.assert_called_once()
  199. result.assert_awaited()