test_gardenpac_heatpump.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.const import (
  10. STATE_UNAVAILABLE,
  11. TEMP_CELSIUS,
  12. TEMP_FAHRENHEIT,
  13. )
  14. from custom_components.tuya_local.generic.climate import TuyaLocalClimate
  15. from custom_components.tuya_local.helpers.device_config import TuyaDeviceConfig
  16. from ..const import GARDENPAC_HEATPUMP_PAYLOAD
  17. from ..helpers import assert_device_properties_set
  18. HVACMODE_DPS = "1"
  19. CURRENTTEMP_DPS = "102"
  20. UNITS_DPS = "103"
  21. POWERLEVEL_DPS = "104"
  22. OPMODE_DPS = "105"
  23. TEMPERATURE_DPS = "106"
  24. UNKNOWN107_DPS = "107"
  25. UNKNOWN108_DPS = "108"
  26. UNKNOWN115_DPS = "115"
  27. UNKNOWN116_DPS = "116"
  28. PRESET_DPS = "117"
  29. class TestGardenPACPoolHeatpump(IsolatedAsyncioTestCase):
  30. def setUp(self):
  31. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  32. self.addCleanup(device_patcher.stop)
  33. self.mock_device = device_patcher.start()
  34. cfg = TuyaDeviceConfig("gardenpac_heatpump.yaml")
  35. climate = cfg.primary_entity
  36. self.climate_name = climate.name
  37. self.subject = TuyaLocalClimate(self.mock_device(), climate)
  38. self.dps = GARDENPAC_HEATPUMP_PAYLOAD.copy()
  39. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  40. def test_supported_features(self):
  41. self.assertEqual(
  42. self.subject.supported_features,
  43. SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE,
  44. )
  45. def test_should_poll(self):
  46. self.assertTrue(self.subject.should_poll)
  47. def test_name_returns_device_name(self):
  48. self.assertEqual(self.subject.name, self.subject._device.name)
  49. def test_unique_id_returns_device_unique_id(self):
  50. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  51. def test_device_info_returns_device_info_from_device(self):
  52. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  53. @skip("Icon customisation not supported yet")
  54. def test_icon(self):
  55. self.dps[HVACMODE_DPS] = True
  56. self.assertEqual(self.subject.icon, "mdi:hot-tub")
  57. self.dps[HVACMODE_DPS] = False
  58. self.assertEqual(self.subject.icon, "mdi:radiator-disabled")
  59. def test_temperature_unit(self):
  60. self.dps[UNITS_DPS] = False
  61. self.assertEqual(self.subject.temperature_unit, TEMP_FAHRENHEIT)
  62. self.dps[UNITS_DPS] = True
  63. self.assertEqual(self.subject.temperature_unit, TEMP_CELSIUS)
  64. def test_target_temperature(self):
  65. self.dps[TEMPERATURE_DPS] = 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. @skip("Conditional ranges not supported yet")
  74. def test_minimum_fahrenheit_temperature(self):
  75. self.dps[UNITS_DPS] = "F"
  76. self.assertEqual(self.subject.min_temp, 60)
  77. @skip("Conditional ranges not supported yet")
  78. def test_maximum_fahrenheit_temperature(self):
  79. self.dps[UNITS_DPS] = "F"
  80. self.assertEqual(self.subject.max_temp, 115)
  81. async def test_legacy_set_temperature_with_temperature(self):
  82. async with assert_device_properties_set(
  83. self.subject._device, {TEMPERATURE_DPS: 25}
  84. ):
  85. await self.subject.async_set_temperature(temperature=25)
  86. async def test_legacy_set_temperature_with_no_valid_properties(self):
  87. await self.subject.async_set_temperature(something="else")
  88. self.subject._device.async_set_property.assert_not_called
  89. async def test_set_target_temperature_succeeds_within_valid_range(self):
  90. async with assert_device_properties_set(
  91. self.subject._device, {TEMPERATURE_DPS: 25}
  92. ):
  93. await self.subject.async_set_target_temperature(25)
  94. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  95. async with assert_device_properties_set(
  96. self.subject._device,
  97. {TEMPERATURE_DPS: 25},
  98. ):
  99. await self.subject.async_set_target_temperature(24.6)
  100. async def test_set_target_temperature_fails_outside_valid_range(self):
  101. with self.assertRaisesRegex(
  102. ValueError, "Target temperature \\(14\\) must be between 18 and 45"
  103. ):
  104. await self.subject.async_set_target_temperature(14)
  105. with self.assertRaisesRegex(
  106. ValueError, "Target temperature \\(46\\) must be between 18 and 45"
  107. ):
  108. await self.subject.async_set_target_temperature(46)
  109. def test_current_temperature(self):
  110. self.dps[CURRENTTEMP_DPS] = 25
  111. self.assertEqual(self.subject.current_temperature, 25)
  112. def test_hvac_mode(self):
  113. self.dps[HVACMODE_DPS] = True
  114. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  115. self.dps[HVACMODE_DPS] = False
  116. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  117. self.dps[HVACMODE_DPS] = None
  118. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  119. def test_hvac_modes(self):
  120. self.assertCountEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_HEAT])
  121. async def test_turn_on(self):
  122. async with assert_device_properties_set(
  123. self.subject._device, {HVACMODE_DPS: True}
  124. ):
  125. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  126. async def test_turn_off(self):
  127. async with assert_device_properties_set(
  128. self.subject._device, {HVACMODE_DPS: False}
  129. ):
  130. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  131. def test_preset_mode(self):
  132. self.dps[PRESET_DPS] = False
  133. self.assertEqual(self.subject.preset_mode, "Silent")
  134. self.dps[PRESET_DPS] = True
  135. self.assertEqual(self.subject.preset_mode, "Smart")
  136. self.dps[PRESET_DPS] = None
  137. self.assertIs(self.subject.preset_mode, None)
  138. def test_preset_modes(self):
  139. self.assertCountEqual(self.subject.preset_modes, ["Silent", "Smart"])
  140. async def test_set_preset_mode_to_silent(self):
  141. async with assert_device_properties_set(
  142. self.subject._device,
  143. {PRESET_DPS: False},
  144. ):
  145. await self.subject.async_set_preset_mode("Silent")
  146. async def test_set_preset_mode_to_smart(self):
  147. async with assert_device_properties_set(
  148. self.subject._device,
  149. {PRESET_DPS: True},
  150. ):
  151. await self.subject.async_set_preset_mode("Smart")
  152. def test_device_state_attributes(self):
  153. self.dps[POWERLEVEL_DPS] = 50
  154. self.dps[OPMODE_DPS] = "cool"
  155. self.dps[UNKNOWN107_DPS] = 1
  156. self.dps[UNKNOWN108_DPS] = 2
  157. self.dps[UNKNOWN115_DPS] = 3
  158. self.dps[UNKNOWN116_DPS] = 4
  159. self.assertCountEqual(
  160. self.subject.device_state_attributes,
  161. {
  162. "power_level": 50,
  163. "operating_mode": "cool",
  164. "unknown_107": 1,
  165. "unknown_108": 2,
  166. "unknown_115": 3,
  167. "unknown_116": 4,
  168. },
  169. )
  170. async def test_update(self):
  171. result = AsyncMock()
  172. self.subject._device.async_refresh.return_value = result()
  173. await self.subject.async_update()
  174. self.subject._device.async_refresh.assert_called_once()
  175. result.assert_awaited()