test_kogan_heater.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. 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_preset_mode(self):
  83. async with assert_device_properties_set(
  84. self.subject._device, {PRESET_DPS: "Low"}
  85. ):
  86. await self.subject.async_set_temperature(preset_mode="Low")
  87. async def test_legacy_set_temperature_with_both_properties(self):
  88. async with assert_device_properties_set(
  89. self.subject._device, {TEMPERATURE_DPS: 26, PRESET_DPS: "High"}
  90. ):
  91. await self.subject.async_set_temperature(temperature=26, preset_mode="High")
  92. async def test_legacy_set_temperature_with_no_valid_properties(self):
  93. await self.subject.async_set_temperature(something="else")
  94. self.subject._device.async_set_property.assert_not_called
  95. async def test_set_target_temperature_succeeds_within_valid_range(self):
  96. async with assert_device_properties_set(
  97. self.subject._device,
  98. {TEMPERATURE_DPS: 25},
  99. ):
  100. await self.subject.async_set_target_temperature(25)
  101. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  102. async with assert_device_properties_set(
  103. self.subject._device, {TEMPERATURE_DPS: 23}
  104. ):
  105. await self.subject.async_set_target_temperature(22.6)
  106. async def test_set_target_temperature_fails_outside_valid_range(self):
  107. with self.assertRaisesRegex(
  108. ValueError, "temperature \\(14\\) must be between 15 and 35"
  109. ):
  110. await self.subject.async_set_target_temperature(14)
  111. with self.assertRaisesRegex(
  112. ValueError, "temperature \\(36\\) must be between 15 and 35"
  113. ):
  114. await self.subject.async_set_target_temperature(36)
  115. def test_current_temperature(self):
  116. self.dps[CURRENTTEMP_DPS] = 25
  117. self.assertEqual(self.subject.current_temperature, 25)
  118. def test_hvac_mode(self):
  119. self.dps[HVACMODE_DPS] = True
  120. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  121. self.dps[HVACMODE_DPS] = False
  122. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  123. self.dps[HVACMODE_DPS] = None
  124. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  125. def test_hvac_modes(self):
  126. self.assertCountEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_HEAT])
  127. async def test_turn_on(self):
  128. async with assert_device_properties_set(
  129. self.subject._device, {HVACMODE_DPS: True}
  130. ):
  131. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  132. async def test_turn_off(self):
  133. async with assert_device_properties_set(
  134. self.subject._device, {HVACMODE_DPS: False}
  135. ):
  136. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  137. def test_preset_mode(self):
  138. self.dps[PRESET_DPS] = "Low"
  139. self.assertEqual(self.subject.preset_mode, "Low")
  140. self.dps[PRESET_DPS] = "High"
  141. self.assertEqual(self.subject.preset_mode, "High")
  142. self.dps[PRESET_DPS] = None
  143. self.assertIs(self.subject.preset_mode, None)
  144. def test_preset_modes(self):
  145. self.assertCountEqual(self.subject.preset_modes, ["Low", "High"])
  146. async def test_set_preset_mode_to_low(self):
  147. async with assert_device_properties_set(
  148. self.subject._device,
  149. {PRESET_DPS: "Low"},
  150. ):
  151. await self.subject.async_set_preset_mode("Low")
  152. async def test_set_preset_mode_to_high(self):
  153. async with assert_device_properties_set(
  154. self.subject._device,
  155. {PRESET_DPS: "High"},
  156. ):
  157. await self.subject.async_set_preset_mode("High")
  158. def test_state_attributes(self):
  159. self.dps[TIMER_DPS] = 10
  160. self.assertCountEqual(self.subject.device_state_attributes, {"timer": 10})
  161. self.dps[TIMER_DPS] = 0
  162. self.assertCountEqual(self.subject.device_state_attributes, {"timer": 0})
  163. async def test_update(self):
  164. result = AsyncMock()
  165. self.subject._device.async_refresh.return_value = result()
  166. await self.subject.async_update()
  167. self.subject._device.async_refresh.assert_called_once()
  168. result.assert_awaited()
  169. def test_lock_was_created(self):
  170. self.assertIsInstance(self.lock, TuyaLocalLock)
  171. def test_lock_is_same_device(self):
  172. self.assertEqual(self.lock._device, self.subject._device)
  173. def test_lock_state(self):
  174. self.dps[LOCK_DPS] = True
  175. self.assertEqual(self.lock.state, STATE_LOCKED)
  176. self.dps[LOCK_DPS] = False
  177. self.assertEqual(self.lock.state, STATE_UNLOCKED)
  178. self.dps[LOCK_DPS] = None
  179. self.assertEqual(self.lock.state, STATE_UNAVAILABLE)
  180. def test_lock_is_locked(self):
  181. self.dps[LOCK_DPS] = True
  182. self.assertTrue(self.lock.is_locked)
  183. self.dps[LOCK_DPS] = False
  184. self.assertFalse(self.lock.is_locked)
  185. self.dps[LOCK_DPS] = None
  186. self.assertFalse(self.lock.is_locked)
  187. async def test_lock_locks(self):
  188. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: True}):
  189. await self.lock.async_lock()
  190. async def test_lock_unlocks(self):
  191. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: False}):
  192. await self.lock.async_unlock()
  193. async def test_lock_update(self):
  194. result = AsyncMock()
  195. self.lock._device.async_refresh.return_value = result()
  196. await self.lock.async_update()
  197. self.lock._device.async_refresh.assert_called_once()
  198. result.assert_awaited()