test_gardenpac_heatpump.py 7.4 KB

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