test_climate.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. from unittest import IsolatedAsyncioTestCase
  2. from unittest.mock import AsyncMock, patch
  3. from homeassistant.components.climate.const import (
  4. ATTR_HVAC_MODE,
  5. ATTR_PRESET_MODE,
  6. HVAC_MODE_HEAT,
  7. HVAC_MODE_OFF,
  8. SUPPORT_PRESET_MODE,
  9. SUPPORT_TARGET_TEMPERATURE,
  10. )
  11. from homeassistant.const import (
  12. ATTR_TEMPERATURE,
  13. STATE_UNAVAILABLE,
  14. TEMP_CELSIUS,
  15. TEMP_FAHRENHEIT,
  16. )
  17. from custom_components.tuya_local.gardenpac_heatpump.climate import (
  18. GardenPACPoolHeatpump,
  19. )
  20. from custom_components.tuya_local.gardenpac_heatpump.const import (
  21. ATTR_OPERATING_MODE,
  22. ATTR_POWER_LEVEL,
  23. ATTR_TARGET_TEMPERATURE,
  24. ATTR_TEMP_UNIT,
  25. HVAC_MODE_TO_DPS_MODE,
  26. PRESET_SILENT,
  27. PRESET_SMART,
  28. PRESET_MODE_TO_DPS_MODE,
  29. PROPERTY_TO_DPS_ID,
  30. )
  31. from ..const import GARDENPAC_HEATPUMP_PAYLOAD
  32. from ..helpers import assert_device_properties_set
  33. class TestGardenPACPoolHeatpump(IsolatedAsyncioTestCase):
  34. def setUp(self):
  35. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  36. self.addCleanup(device_patcher.stop)
  37. self.mock_device = device_patcher.start()
  38. self.subject = GardenPACPoolHeatpump(self.mock_device())
  39. self.dps = GARDENPAC_HEATPUMP_PAYLOAD.copy()
  40. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  41. def test_supported_features(self):
  42. self.assertEqual(
  43. self.subject.supported_features,
  44. SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE,
  45. )
  46. def test_should_poll(self):
  47. self.assertTrue(self.subject.should_poll)
  48. def test_name_returns_device_name(self):
  49. self.assertEqual(self.subject.name, self.subject._device.name)
  50. def test_unique_id_returns_device_unique_id(self):
  51. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  52. def test_device_info_returns_device_info_from_device(self):
  53. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  54. def test_icon(self):
  55. self.dps[PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]] = True
  56. self.assertEqual(self.subject.icon, "mdi:hot-tub")
  57. self.dps[PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]] = False
  58. self.assertEqual(self.subject.icon, "mdi:radiator-disabled")
  59. def test_temperature_unit(self):
  60. self.dps[PROPERTY_TO_DPS_ID[ATTR_TEMP_UNIT]] = False
  61. self.assertEqual(self.subject.temperature_unit, TEMP_FAHRENHEIT)
  62. self.dps[PROPERTY_TO_DPS_ID[ATTR_TEMP_UNIT]] = True
  63. self.assertEqual(self.subject.temperature_unit, TEMP_CELSIUS)
  64. def test_target_temperature(self):
  65. self.dps[PROPERTY_TO_DPS_ID[ATTR_TARGET_TEMPERATURE]] = 25
  66. self.assertEqual(self.subject.target_temperature, 25)
  67. def test_target_temperature_step(self):
  68. self.assertEqual(self.subject.target_temperature_step, 1)
  69. def test_minimum_target_temperature(self):
  70. self.assertEqual(self.subject.min_temp, 18)
  71. def test_maximum_target_temperature(self):
  72. self.assertEqual(self.subject.max_temp, 45)
  73. async def test_legacy_set_temperature_with_temperature(self):
  74. async with assert_device_properties_set(
  75. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_TARGET_TEMPERATURE]: 25}
  76. ):
  77. await self.subject.async_set_temperature(temperature=25)
  78. async def test_legacy_set_temperature_with_no_valid_properties(self):
  79. await self.subject.async_set_temperature(something="else")
  80. self.subject._device.async_set_property.assert_not_called
  81. async def test_set_target_temperature_succeeds_within_valid_range(self):
  82. async with assert_device_properties_set(
  83. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_TARGET_TEMPERATURE]: 25}
  84. ):
  85. await self.subject.async_set_target_temperature(25)
  86. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  87. async with assert_device_properties_set(
  88. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_TARGET_TEMPERATURE]: 25},
  89. ):
  90. await self.subject.async_set_target_temperature(24.6)
  91. async def test_set_target_temperature_fails_outside_valid_range(self):
  92. with self.assertRaisesRegex(
  93. ValueError, "Target temperature \\(14\\) must be between 18 and 45"
  94. ):
  95. await self.subject.async_set_target_temperature(14)
  96. with self.assertRaisesRegex(
  97. ValueError, "Target temperature \\(46\\) must be between 18 and 45"
  98. ):
  99. await self.subject.async_set_target_temperature(46)
  100. def test_current_temperature(self):
  101. self.dps[PROPERTY_TO_DPS_ID[ATTR_TEMPERATURE]] = 25
  102. self.assertEqual(self.subject.current_temperature, 25)
  103. def test_hvac_mode(self):
  104. self.dps[PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]] = True
  105. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  106. self.dps[PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]] = False
  107. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  108. self.dps[PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]] = None
  109. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  110. def test_hvac_modes(self):
  111. self.assertEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_HEAT])
  112. async def test_turn_on(self):
  113. async with assert_device_properties_set(
  114. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]: True}
  115. ):
  116. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  117. async def test_turn_off(self):
  118. async with assert_device_properties_set(
  119. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]: False}
  120. ):
  121. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  122. def test_preset_mode(self):
  123. self.dps[PROPERTY_TO_DPS_ID[ATTR_PRESET_MODE]] = PRESET_MODE_TO_DPS_MODE[
  124. PRESET_SILENT
  125. ]
  126. self.assertEqual(self.subject.preset_mode, PRESET_SILENT)
  127. self.dps[PROPERTY_TO_DPS_ID[ATTR_PRESET_MODE]] = PRESET_MODE_TO_DPS_MODE[
  128. PRESET_SMART
  129. ]
  130. self.assertEqual(self.subject.preset_mode, PRESET_SMART)
  131. self.dps[PROPERTY_TO_DPS_ID[ATTR_PRESET_MODE]] = None
  132. self.assertIs(self.subject.preset_mode, None)
  133. def test_preset_modes(self):
  134. self.assertEqual(self.subject.preset_modes, [PRESET_SILENT, PRESET_SMART])
  135. async def test_set_preset_mode_to_silent(self):
  136. async with assert_device_properties_set(
  137. self.subject._device,
  138. {
  139. PROPERTY_TO_DPS_ID[ATTR_PRESET_MODE]: PRESET_MODE_TO_DPS_MODE[
  140. PRESET_SILENT
  141. ]
  142. },
  143. ):
  144. await self.subject.async_set_preset_mode(PRESET_SILENT)
  145. async def test_set_preset_mode_to_smart(self):
  146. async with assert_device_properties_set(
  147. self.subject._device,
  148. {
  149. PROPERTY_TO_DPS_ID[ATTR_PRESET_MODE]: PRESET_MODE_TO_DPS_MODE[
  150. PRESET_SMART
  151. ]
  152. },
  153. ):
  154. await self.subject.async_set_preset_mode(PRESET_SMART)
  155. def test_device_state_attributes(self):
  156. self.dps[PROPERTY_TO_DPS_ID[ATTR_POWER_LEVEL]] = 50
  157. self.dps[PROPERTY_TO_DPS_ID[ATTR_OPERATING_MODE]] = "cool"
  158. self.assertEqual(
  159. self.subject.device_state_attributes,
  160. {ATTR_POWER_LEVEL: 50, ATTR_OPERATING_MODE: "cool"},
  161. )
  162. async def test_update(self):
  163. result = AsyncMock()
  164. self.subject._device.async_refresh.return_value = result()
  165. await self.subject.async_update()
  166. self.subject._device.async_refresh.assert_called_once()
  167. result.assert_awaited()