test_gardenpac_heatpump.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. def test_minimum_fahrenheit_temperature(self):
  74. self.dps[UNITS_DPS] = False
  75. self.assertEqual(self.subject.min_temp, 60)
  76. def test_maximum_fahrenheit_temperature(self):
  77. self.dps[UNITS_DPS] = False
  78. self.assertEqual(self.subject.max_temp, 115)
  79. async def test_legacy_set_temperature_with_temperature(self):
  80. async with assert_device_properties_set(
  81. self.subject._device, {TEMPERATURE_DPS: 25}
  82. ):
  83. await self.subject.async_set_temperature(temperature=25)
  84. async def test_legacy_set_temperature_with_no_valid_properties(self):
  85. await self.subject.async_set_temperature(something="else")
  86. self.subject._device.async_set_property.assert_not_called
  87. async def test_set_target_temperature_succeeds_within_valid_range(self):
  88. async with assert_device_properties_set(
  89. self.subject._device, {TEMPERATURE_DPS: 25}
  90. ):
  91. await self.subject.async_set_target_temperature(25)
  92. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  93. async with assert_device_properties_set(
  94. self.subject._device,
  95. {TEMPERATURE_DPS: 25},
  96. ):
  97. await self.subject.async_set_target_temperature(24.6)
  98. async def test_set_target_temperature_fails_outside_valid_range(self):
  99. with self.assertRaisesRegex(
  100. ValueError, "temperature \\(14\\) must be between 18 and 45"
  101. ):
  102. await self.subject.async_set_target_temperature(14)
  103. with self.assertRaisesRegex(
  104. ValueError, "temperature \\(46\\) must be between 18 and 45"
  105. ):
  106. await self.subject.async_set_target_temperature(46)
  107. def test_current_temperature(self):
  108. self.dps[CURRENTTEMP_DPS] = 25
  109. self.assertEqual(self.subject.current_temperature, 25)
  110. def test_hvac_mode(self):
  111. self.dps[HVACMODE_DPS] = True
  112. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  113. self.dps[HVACMODE_DPS] = False
  114. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  115. self.dps[HVACMODE_DPS] = None
  116. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  117. def test_hvac_modes(self):
  118. self.assertCountEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_HEAT])
  119. async def test_turn_on(self):
  120. async with assert_device_properties_set(
  121. self.subject._device, {HVACMODE_DPS: True}
  122. ):
  123. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  124. async def test_turn_off(self):
  125. async with assert_device_properties_set(
  126. self.subject._device, {HVACMODE_DPS: False}
  127. ):
  128. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  129. def test_preset_mode(self):
  130. self.dps[PRESET_DPS] = False
  131. self.assertEqual(self.subject.preset_mode, "Silent")
  132. self.dps[PRESET_DPS] = True
  133. self.assertEqual(self.subject.preset_mode, "Smart")
  134. self.dps[PRESET_DPS] = None
  135. self.assertIs(self.subject.preset_mode, None)
  136. def test_preset_modes(self):
  137. self.assertCountEqual(self.subject.preset_modes, ["Silent", "Smart"])
  138. async def test_set_preset_mode_to_silent(self):
  139. async with assert_device_properties_set(
  140. self.subject._device,
  141. {PRESET_DPS: False},
  142. ):
  143. await self.subject.async_set_preset_mode("Silent")
  144. async def test_set_preset_mode_to_smart(self):
  145. async with assert_device_properties_set(
  146. self.subject._device,
  147. {PRESET_DPS: True},
  148. ):
  149. await self.subject.async_set_preset_mode("Smart")
  150. def test_device_state_attributes(self):
  151. self.dps[POWERLEVEL_DPS] = 50
  152. self.dps[OPMODE_DPS] = "cool"
  153. self.dps[UNKNOWN107_DPS] = 1
  154. self.dps[UNKNOWN108_DPS] = 2
  155. self.dps[UNKNOWN115_DPS] = 3
  156. self.dps[UNKNOWN116_DPS] = 4
  157. self.assertCountEqual(
  158. self.subject.device_state_attributes,
  159. {
  160. "power_level": 50,
  161. "operating_mode": "cool",
  162. "unknown_107": 1,
  163. "unknown_108": 2,
  164. "unknown_115": 3,
  165. "unknown_116": 4,
  166. },
  167. )
  168. async def test_update(self):
  169. result = AsyncMock()
  170. self.subject._device.async_refresh.return_value = result()
  171. await self.subject.async_update()
  172. self.subject._device.async_refresh.assert_called_once()
  173. result.assert_awaited()