test_bwt_heatpump.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 STATE_UNAVAILABLE
  10. from custom_components.tuya_local.generic.climate import TuyaLocalClimate
  11. from custom_components.tuya_local.helpers.device_config import TuyaDeviceConfig
  12. from ..const import BWT_HEATPUMP_PAYLOAD
  13. from ..helpers import assert_device_properties_set
  14. HVACMODE_DPS = "1"
  15. TEMPERATURE_DPS = "2"
  16. CURRENTTEMP_DPS = "3"
  17. PRESET_DPS = "4"
  18. ERROR_DPS = "9"
  19. class TestBWTHeatpump(IsolatedAsyncioTestCase):
  20. def setUp(self):
  21. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  22. self.addCleanup(device_patcher.stop)
  23. self.mock_device = device_patcher.start()
  24. cfg = TuyaDeviceConfig("bwt_heatpump.yaml")
  25. climate = cfg.primary_entity
  26. self.climate_name = climate.name
  27. self.subject = TuyaLocalClimate(self.mock_device, climate)
  28. self.dps = BWT_HEATPUMP_PAYLOAD.copy()
  29. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  30. def test_supported_features(self):
  31. self.assertEqual(
  32. self.subject.supported_features,
  33. SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE,
  34. )
  35. def test_should_poll(self):
  36. self.assertTrue(self.subject.should_poll)
  37. def test_name_returns_device_name(self):
  38. self.assertEqual(self.subject.name, self.subject._device.name)
  39. def test_unique_id_returns_device_unique_id(self):
  40. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  41. def test_device_info_returns_device_info_from_device(self):
  42. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  43. @skip("Icon customisation not supported yet")
  44. def test_icon(self):
  45. self.dps[HVACMODE_DPS] = True
  46. self.assertEqual(self.subject.icon, "mdi:hot-tub")
  47. self.dps[HVACMODE_DPS] = False
  48. self.assertEqual(self.subject.icon, "mdi:hvac_off")
  49. def test_temperature_unit_returns_device_temperature_unit(self):
  50. self.assertEqual(
  51. self.subject.temperature_unit, self.subject._device.temperature_unit
  52. )
  53. def test_target_temperature(self):
  54. self.dps[TEMPERATURE_DPS] = 25
  55. self.assertEqual(self.subject.target_temperature, 25)
  56. def test_target_temperature_step(self):
  57. self.assertEqual(self.subject.target_temperature_step, 1)
  58. def test_minimum_target_temperature(self):
  59. self.assertEqual(self.subject.min_temp, 5)
  60. def test_maximum_target_temperature(self):
  61. self.assertEqual(self.subject.max_temp, 40)
  62. async def test_legacy_set_temperature_with_temperature(self):
  63. async with assert_device_properties_set(
  64. self.subject._device, {TEMPERATURE_DPS: 24}
  65. ):
  66. await self.subject.async_set_temperature(temperature=24)
  67. async def test_legacy_set_temperature_with_preset_mode(self):
  68. async with assert_device_properties_set(
  69. self.subject._device, {PRESET_DPS: "cool"}
  70. ):
  71. await self.subject.async_set_temperature(preset_mode="Smart Cooling")
  72. async def test_legacy_set_temperature_with_both_properties(self):
  73. async with assert_device_properties_set(
  74. self.subject._device, {TEMPERATURE_DPS: 26, PRESET_DPS: "heat"}
  75. ):
  76. await self.subject.async_set_temperature(
  77. temperature=26, preset_mode="Smart Heating"
  78. )
  79. async def test_legacy_set_temperature_with_no_valid_properties(self):
  80. await self.subject.async_set_temperature(something="else")
  81. self.subject._device.async_set_property.assert_not_called
  82. async def test_set_target_temperature_succeeds_within_valid_range(self):
  83. async with assert_device_properties_set(
  84. self.subject._device,
  85. {TEMPERATURE_DPS: 25},
  86. ):
  87. await self.subject.async_set_target_temperature(25)
  88. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  89. async with assert_device_properties_set(
  90. self.subject._device, {TEMPERATURE_DPS: 23}
  91. ):
  92. await self.subject.async_set_target_temperature(22.6)
  93. async def test_set_target_temperature_fails_outside_valid_range(self):
  94. with self.assertRaisesRegex(
  95. ValueError, "temperature \\(4\\) must be between 5 and 40"
  96. ):
  97. await self.subject.async_set_target_temperature(4)
  98. with self.assertRaisesRegex(
  99. ValueError, "temperature \\(41\\) must be between 5 and 40"
  100. ):
  101. await self.subject.async_set_target_temperature(41)
  102. def test_current_temperature(self):
  103. self.dps[CURRENTTEMP_DPS] = 25
  104. self.assertEqual(self.subject.current_temperature, 25)
  105. def test_hvac_mode(self):
  106. self.dps[HVACMODE_DPS] = True
  107. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  108. self.dps[HVACMODE_DPS] = False
  109. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  110. self.dps[HVACMODE_DPS] = None
  111. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  112. def test_hvac_modes(self):
  113. self.assertCountEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_HEAT])
  114. async def test_turn_on(self):
  115. async with assert_device_properties_set(
  116. self.subject._device, {HVACMODE_DPS: True}
  117. ):
  118. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  119. async def test_turn_off(self):
  120. async with assert_device_properties_set(
  121. self.subject._device, {HVACMODE_DPS: False}
  122. ):
  123. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  124. def test_preset_mode(self):
  125. self.dps[PRESET_DPS] = "heat"
  126. self.assertEqual(self.subject.preset_mode, "Smart Heating")
  127. self.dps[PRESET_DPS] = "cool"
  128. self.assertEqual(self.subject.preset_mode, "Smart Cooling")
  129. self.dps[PRESET_DPS] = "quickheat"
  130. self.assertEqual(self.subject.preset_mode, "Boost Heating")
  131. self.dps[PRESET_DPS] = "quickcool"
  132. self.assertEqual(self.subject.preset_mode, "Boost Cooling")
  133. self.dps[PRESET_DPS] = "quietheat"
  134. self.assertEqual(self.subject.preset_mode, "Eco Heating")
  135. self.dps[PRESET_DPS] = "quietcool"
  136. self.assertEqual(self.subject.preset_mode, "Eco Cooling")
  137. self.dps[PRESET_DPS] = "auto"
  138. self.assertEqual(self.subject.preset_mode, "Auto")
  139. self.dps[PRESET_DPS] = None
  140. self.assertIs(self.subject.preset_mode, None)
  141. def test_preset_modes(self):
  142. self.assertCountEqual(
  143. self.subject.preset_modes,
  144. [
  145. "Smart Heating",
  146. "Boost Heating",
  147. "Eco Heating",
  148. "Smart Cooling",
  149. "Boost Cooling",
  150. "Eco Cooling",
  151. "Auto",
  152. ],
  153. )
  154. async def test_set_preset_mode_to_heat(self):
  155. async with assert_device_properties_set(
  156. self.subject._device,
  157. {PRESET_DPS: "heat"},
  158. ):
  159. await self.subject.async_set_preset_mode("Smart Heating")
  160. async def test_set_preset_mode_to_cool(self):
  161. async with assert_device_properties_set(
  162. self.subject._device,
  163. {PRESET_DPS: "cool"},
  164. ):
  165. await self.subject.async_set_preset_mode("Smart Cooling")
  166. async def test_set_preset_mode_to_quickheat(self):
  167. async with assert_device_properties_set(
  168. self.subject._device,
  169. {PRESET_DPS: "quickheat"},
  170. ):
  171. await self.subject.async_set_preset_mode("Boost Heating")
  172. async def test_set_preset_mode_to_quickcool(self):
  173. async with assert_device_properties_set(
  174. self.subject._device,
  175. {PRESET_DPS: "quickcool"},
  176. ):
  177. await self.subject.async_set_preset_mode("Boost Cooling")
  178. async def test_set_preset_mode_to_quietheat(self):
  179. async with assert_device_properties_set(
  180. self.subject._device,
  181. {PRESET_DPS: "quietheat"},
  182. ):
  183. await self.subject.async_set_preset_mode("Eco Heating")
  184. async def test_set_preset_mode_to_quietcool(self):
  185. async with assert_device_properties_set(
  186. self.subject._device,
  187. {PRESET_DPS: "quietcool"},
  188. ):
  189. await self.subject.async_set_preset_mode("Eco Cooling")
  190. async def test_set_preset_mode_to_auto(self):
  191. async with assert_device_properties_set(
  192. self.subject._device,
  193. {PRESET_DPS: "auto"},
  194. ):
  195. await self.subject.async_set_preset_mode("Auto")
  196. def test_error_state(self):
  197. self.dps[ERROR_DPS] = 0
  198. self.assertEqual(self.subject.device_state_attributes, {"error": "OK"})
  199. self.dps[ERROR_DPS] = 1
  200. self.assertEqual(
  201. self.subject.device_state_attributes,
  202. {"error": "Water Flow Protection"},
  203. )
  204. self.dps[ERROR_DPS] = 2
  205. self.assertEqual(
  206. self.subject.device_state_attributes,
  207. {"error": 2},
  208. )
  209. async def test_update(self):
  210. result = AsyncMock()
  211. self.subject._device.async_refresh.return_value = result()
  212. await self.subject.async_update()
  213. self.subject._device.async_refresh.assert_called_once()
  214. result.assert_awaited()