test_bwt_heatpump.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. def test_icon(self):
  44. self.dps[HVACMODE_DPS] = True
  45. self.assertEqual(self.subject.icon, "mdi:hot-tub")
  46. self.dps[HVACMODE_DPS] = False
  47. self.assertEqual(self.subject.icon, "mdi:hvac-off")
  48. def test_temperature_unit_returns_device_temperature_unit(self):
  49. self.assertEqual(
  50. self.subject.temperature_unit, self.subject._device.temperature_unit
  51. )
  52. def test_target_temperature(self):
  53. self.dps[TEMPERATURE_DPS] = 25
  54. self.assertEqual(self.subject.target_temperature, 25)
  55. def test_target_temperature_step(self):
  56. self.assertEqual(self.subject.target_temperature_step, 1)
  57. def test_minimum_target_temperature(self):
  58. self.assertEqual(self.subject.min_temp, 5)
  59. def test_maximum_target_temperature(self):
  60. self.assertEqual(self.subject.max_temp, 40)
  61. async def test_legacy_set_temperature_with_temperature(self):
  62. async with assert_device_properties_set(
  63. self.subject._device, {TEMPERATURE_DPS: 24}
  64. ):
  65. await self.subject.async_set_temperature(temperature=24)
  66. async def test_legacy_set_temperature_with_preset_mode(self):
  67. async with assert_device_properties_set(
  68. self.subject._device, {PRESET_DPS: "cool"}
  69. ):
  70. await self.subject.async_set_temperature(preset_mode="Smart Cooling")
  71. async def test_legacy_set_temperature_with_both_properties(self):
  72. async with assert_device_properties_set(
  73. self.subject._device, {TEMPERATURE_DPS: 26, PRESET_DPS: "heat"}
  74. ):
  75. await self.subject.async_set_temperature(
  76. temperature=26, preset_mode="Smart Heating"
  77. )
  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,
  84. {TEMPERATURE_DPS: 25},
  85. ):
  86. await self.subject.async_set_target_temperature(25)
  87. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  88. async with assert_device_properties_set(
  89. self.subject._device, {TEMPERATURE_DPS: 23}
  90. ):
  91. await self.subject.async_set_target_temperature(22.6)
  92. async def test_set_target_temperature_fails_outside_valid_range(self):
  93. with self.assertRaisesRegex(
  94. ValueError, "temperature \\(4\\) must be between 5 and 40"
  95. ):
  96. await self.subject.async_set_target_temperature(4)
  97. with self.assertRaisesRegex(
  98. ValueError, "temperature \\(41\\) must be between 5 and 40"
  99. ):
  100. await self.subject.async_set_target_temperature(41)
  101. def test_current_temperature(self):
  102. self.dps[CURRENTTEMP_DPS] = 25
  103. self.assertEqual(self.subject.current_temperature, 25)
  104. def test_hvac_mode(self):
  105. self.dps[HVACMODE_DPS] = True
  106. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  107. self.dps[HVACMODE_DPS] = False
  108. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  109. self.dps[HVACMODE_DPS] = None
  110. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  111. def test_hvac_modes(self):
  112. self.assertCountEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_HEAT])
  113. async def test_turn_on(self):
  114. async with assert_device_properties_set(
  115. self.subject._device, {HVACMODE_DPS: True}
  116. ):
  117. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  118. async def test_turn_off(self):
  119. async with assert_device_properties_set(
  120. self.subject._device, {HVACMODE_DPS: False}
  121. ):
  122. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  123. def test_preset_mode(self):
  124. self.dps[PRESET_DPS] = "heat"
  125. self.assertEqual(self.subject.preset_mode, "Smart Heating")
  126. self.dps[PRESET_DPS] = "cool"
  127. self.assertEqual(self.subject.preset_mode, "Smart Cooling")
  128. self.dps[PRESET_DPS] = "quickheat"
  129. self.assertEqual(self.subject.preset_mode, "Boost Heating")
  130. self.dps[PRESET_DPS] = "quickcool"
  131. self.assertEqual(self.subject.preset_mode, "Boost Cooling")
  132. self.dps[PRESET_DPS] = "quietheat"
  133. self.assertEqual(self.subject.preset_mode, "Eco Heating")
  134. self.dps[PRESET_DPS] = "quietcool"
  135. self.assertEqual(self.subject.preset_mode, "Eco Cooling")
  136. self.dps[PRESET_DPS] = "auto"
  137. self.assertEqual(self.subject.preset_mode, "Auto")
  138. self.dps[PRESET_DPS] = None
  139. self.assertIs(self.subject.preset_mode, None)
  140. def test_preset_modes(self):
  141. self.assertCountEqual(
  142. self.subject.preset_modes,
  143. [
  144. "Smart Heating",
  145. "Boost Heating",
  146. "Eco Heating",
  147. "Smart Cooling",
  148. "Boost Cooling",
  149. "Eco Cooling",
  150. "Auto",
  151. ],
  152. )
  153. async def test_set_preset_mode_to_heat(self):
  154. async with assert_device_properties_set(
  155. self.subject._device,
  156. {PRESET_DPS: "heat"},
  157. ):
  158. await self.subject.async_set_preset_mode("Smart Heating")
  159. async def test_set_preset_mode_to_cool(self):
  160. async with assert_device_properties_set(
  161. self.subject._device,
  162. {PRESET_DPS: "cool"},
  163. ):
  164. await self.subject.async_set_preset_mode("Smart Cooling")
  165. async def test_set_preset_mode_to_quickheat(self):
  166. async with assert_device_properties_set(
  167. self.subject._device,
  168. {PRESET_DPS: "quickheat"},
  169. ):
  170. await self.subject.async_set_preset_mode("Boost Heating")
  171. async def test_set_preset_mode_to_quickcool(self):
  172. async with assert_device_properties_set(
  173. self.subject._device,
  174. {PRESET_DPS: "quickcool"},
  175. ):
  176. await self.subject.async_set_preset_mode("Boost Cooling")
  177. async def test_set_preset_mode_to_quietheat(self):
  178. async with assert_device_properties_set(
  179. self.subject._device,
  180. {PRESET_DPS: "quietheat"},
  181. ):
  182. await self.subject.async_set_preset_mode("Eco Heating")
  183. async def test_set_preset_mode_to_quietcool(self):
  184. async with assert_device_properties_set(
  185. self.subject._device,
  186. {PRESET_DPS: "quietcool"},
  187. ):
  188. await self.subject.async_set_preset_mode("Eco Cooling")
  189. async def test_set_preset_mode_to_auto(self):
  190. async with assert_device_properties_set(
  191. self.subject._device,
  192. {PRESET_DPS: "auto"},
  193. ):
  194. await self.subject.async_set_preset_mode("Auto")
  195. def test_error_state(self):
  196. self.dps[ERROR_DPS] = 0
  197. self.assertEqual(self.subject.device_state_attributes, {"error": "OK"})
  198. self.dps[ERROR_DPS] = 1
  199. self.assertEqual(
  200. self.subject.device_state_attributes,
  201. {"error": "Water Flow Protection"},
  202. )
  203. self.dps[ERROR_DPS] = 2
  204. self.assertEqual(
  205. self.subject.device_state_attributes,
  206. {"error": 2},
  207. )
  208. async def test_update(self):
  209. result = AsyncMock()
  210. self.subject._device.async_refresh.return_value = result()
  211. await self.subject.async_update()
  212. self.subject._device.async_refresh.assert_called_once()
  213. result.assert_awaited()