test_purline_m100_heater.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. from unittest import IsolatedAsyncioTestCase, skip
  2. from unittest.mock import AsyncMock, patch
  3. from homeassistant.components.climate.const import (
  4. HVAC_MODE_FAN_ONLY,
  5. HVAC_MODE_HEAT,
  6. HVAC_MODE_OFF,
  7. SUPPORT_PRESET_MODE,
  8. SUPPORT_SWING_MODE,
  9. SUPPORT_TARGET_TEMPERATURE,
  10. SWING_OFF,
  11. SWING_VERTICAL,
  12. )
  13. from homeassistant.components.switch import DEVICE_CLASS_SWITCH
  14. from homeassistant.const import STATE_UNAVAILABLE
  15. from custom_components.tuya_local.generic.climate import TuyaLocalClimate
  16. from custom_components.tuya_local.generic.light import TuyaLocalLight
  17. from custom_components.tuya_local.generic.switch import TuyaLocalSwitch
  18. from custom_components.tuya_local.helpers.device_config import TuyaDeviceConfig
  19. from ..const import PURLINE_M100_HEATER_PAYLOAD
  20. from ..helpers import (
  21. assert_device_properties_set,
  22. assert_device_properties_set_optional,
  23. )
  24. HVACMODE_DPS = "1"
  25. TEMPERATURE_DPS = "2"
  26. CURRENTTEMP_DPS = "3"
  27. PRESET_DPS = "5"
  28. LIGHTOFF_DPS = "10"
  29. TIMERHR_DPS = "11"
  30. TIMER_DPS = "12"
  31. SWITCH_DPS = "101"
  32. SWING_DPS = "102"
  33. class TestPulineM100Heater(IsolatedAsyncioTestCase):
  34. def setUp(self):
  35. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  36. self.addCleanup(device_patcher.stop)
  37. self.mock_device = device_patcher.start()
  38. cfg = TuyaDeviceConfig("purline_m100_heater.yaml")
  39. climate = cfg.primary_entity
  40. light = None
  41. switch = None
  42. for e in cfg.secondary_entities():
  43. if e.entity == "light":
  44. light = e
  45. elif e.entity == "switch":
  46. switch = e
  47. self.climate_name = climate.name
  48. self.light_name = "missing" if light is None else light.name
  49. self.switch_name = "missing" if switch is None else switch.name
  50. self.subject = TuyaLocalClimate(self.mock_device(), climate)
  51. self.light = TuyaLocalLight(self.mock_device(), light)
  52. self.switch = TuyaLocalSwitch(self.mock_device(), switch)
  53. self.dps = PURLINE_M100_HEATER_PAYLOAD.copy()
  54. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  55. def test_supported_features(self):
  56. self.assertEqual(
  57. self.subject.supported_features,
  58. SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE | SUPPORT_SWING_MODE,
  59. )
  60. def test_should_poll(self):
  61. self.assertTrue(self.subject.should_poll)
  62. self.assertTrue(self.light.should_poll)
  63. self.assertTrue(self.switch.should_poll)
  64. def test_name_returns_device_name(self):
  65. self.assertEqual(self.subject.name, self.subject._device.name)
  66. self.assertEqual(self.light.name, self.subject._device.name)
  67. self.assertEqual(self.switch.name, self.subject._device.name)
  68. def test_friendly_name_returns_config_name(self):
  69. self.assertEqual(self.subject.friendly_name, self.climate_name)
  70. self.assertEqual(self.light.friendly_name, self.light_name)
  71. self.assertEqual(self.switch.friendly_name, self.switch_name)
  72. def test_unique_id_returns_device_unique_id(self):
  73. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  74. self.assertEqual(self.light.unique_id, self.subject._device.unique_id)
  75. self.assertEqual(self.switch.unique_id, self.subject._device.unique_id)
  76. def test_device_info_returns_device_info_from_device(self):
  77. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  78. self.assertEqual(self.light.device_info, self.subject._device.device_info)
  79. self.assertEqual(self.switch.device_info, self.subject._device.device_info)
  80. @skip("Icon customisation not supported yet")
  81. def test_icon(self):
  82. self.dps[HVACMODE_DPS] = True
  83. self.dps[PRESET_DPS] = "auto"
  84. self.assertEqual(self.subject.icon, "mdi:radiator")
  85. self.dps[HVACMODE_DPS] = False
  86. self.assertEqual(self.subject.icon, "mdi:radiator-disabled")
  87. self.dps[HVACMODE_DPS] = True
  88. self.dps[PRESET_DPS] = "off"
  89. self.assertEqual(self.subject.icon, "mdi:fan")
  90. def test_temperature_unit_returns_device_temperature_unit(self):
  91. self.assertEqual(
  92. self.subject.temperature_unit, self.subject._device.temperature_unit
  93. )
  94. def test_target_temperature(self):
  95. self.dps[TEMPERATURE_DPS] = 25
  96. self.assertEqual(self.subject.target_temperature, 25)
  97. def test_target_temperature_step(self):
  98. self.assertEqual(self.subject.target_temperature_step, 1)
  99. def test_minimum_target_temperature(self):
  100. self.assertEqual(self.subject.min_temp, 15)
  101. def test_maximum_target_temperature(self):
  102. self.assertEqual(self.subject.max_temp, 35)
  103. async def test_legacy_set_temperature_with_temperature(self):
  104. async with assert_device_properties_set(
  105. self.subject._device, {TEMPERATURE_DPS: 25}
  106. ):
  107. await self.subject.async_set_temperature(temperature=25)
  108. async def test_legacy_set_temperature_with_no_valid_properties(self):
  109. await self.subject.async_set_temperature(something="else")
  110. self.subject._device.async_set_property.assert_not_called
  111. async def test_set_target_temperature(self):
  112. async with assert_device_properties_set(
  113. self.subject._device, {TEMPERATURE_DPS: 25}
  114. ):
  115. await self.subject.async_set_target_temperature(25)
  116. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  117. async with assert_device_properties_set(
  118. self.subject._device,
  119. {TEMPERATURE_DPS: 25},
  120. ):
  121. await self.subject.async_set_target_temperature(24.6)
  122. async def test_set_target_temperature_fails_outside_valid_range(self):
  123. with self.assertRaisesRegex(
  124. ValueError, "Target temperature \\(4\\) must be between 15 and 35"
  125. ):
  126. await self.subject.async_set_target_temperature(4)
  127. with self.assertRaisesRegex(
  128. ValueError, "Target temperature \\(36\\) must be between 15 and 35"
  129. ):
  130. await self.subject.async_set_target_temperature(36)
  131. def test_current_temperature(self):
  132. self.dps[CURRENTTEMP_DPS] = 25
  133. self.assertEqual(self.subject.current_temperature, 25)
  134. def test_hvac_mode(self):
  135. self.dps[HVACMODE_DPS] = True
  136. self.dps[PRESET_DPS] = "auto"
  137. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  138. self.dps[PRESET_DPS] = "off"
  139. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_FAN_ONLY)
  140. self.dps[HVACMODE_DPS] = False
  141. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  142. self.dps[HVACMODE_DPS] = None
  143. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  144. def test_hvac_modes(self):
  145. self.assertCountEqual(
  146. self.subject.hvac_modes,
  147. [HVAC_MODE_OFF, HVAC_MODE_HEAT, HVAC_MODE_FAN_ONLY],
  148. )
  149. async def test_turn_on(self):
  150. async with assert_device_properties_set_optional(
  151. self.subject._device,
  152. {HVACMODE_DPS: True},
  153. {PRESET_DPS: "auto"},
  154. ):
  155. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  156. async def test_turn_off(self):
  157. async with assert_device_properties_set(
  158. self.subject._device,
  159. {HVACMODE_DPS: False},
  160. ):
  161. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  162. async def test_turn_on_fan(self):
  163. async with assert_device_properties_set_optional(
  164. self.subject._device,
  165. {HVACMODE_DPS: True},
  166. {PRESET_DPS: "off"},
  167. ):
  168. await self.subject.async_set_hvac_mode(HVAC_MODE_FAN_ONLY)
  169. def test_preset_mode(self):
  170. self.dps[PRESET_DPS] = "auto"
  171. self.assertEqual(self.subject.preset_mode, "Auto")
  172. self.dps[PRESET_DPS] = "off"
  173. self.assertEqual(self.subject.preset_mode, "Fan")
  174. self.dps[PRESET_DPS] = "4"
  175. self.assertEqual(self.subject.preset_mode, "4")
  176. self.dps[PRESET_DPS] = None
  177. self.assertIs(self.subject.preset_mode, None)
  178. def test_preset_modes(self):
  179. self.assertCountEqual(
  180. self.subject.preset_modes,
  181. ["Fan", "1", "2", "3", "4", "5", "Auto"],
  182. )
  183. async def test_set_preset_mode_numeric(self):
  184. async with assert_device_properties_set(
  185. self.subject._device,
  186. {PRESET_DPS: "3"},
  187. ):
  188. await self.subject.async_set_preset_mode("3")
  189. def test_swing_mode(self):
  190. self.dps[SWING_DPS] = True
  191. self.assertEqual(self.subject.swing_mode, SWING_VERTICAL)
  192. self.dps[SWING_DPS] = False
  193. self.assertEqual(self.subject.swing_mode, SWING_OFF)
  194. def test_swing_modes(self):
  195. self.assertCountEqual(
  196. self.subject.swing_modes,
  197. [SWING_OFF, SWING_VERTICAL],
  198. )
  199. async def test_set_swing_mode_on(self):
  200. async with assert_device_properties_set(
  201. self.subject._device, {SWING_DPS: True}
  202. ):
  203. await self.subject.async_set_swing_mode(SWING_VERTICAL)
  204. async def test_set_swing_mode_off(self):
  205. async with assert_device_properties_set(
  206. self.subject._device, {SWING_DPS: False}
  207. ):
  208. await self.subject.async_set_swing_mode(SWING_OFF)
  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()
  215. def test_light_was_created(self):
  216. self.assertIsInstance(self.light, TuyaLocalLight)
  217. def test_light_is_same_device(self):
  218. self.assertEqual(self.light._device, self.subject._device)
  219. def test_light_icon(self):
  220. self.dps[LIGHTOFF_DPS] = False
  221. self.assertEqual(self.light.icon, "mdi:led-on")
  222. self.dps[LIGHTOFF_DPS] = True
  223. self.assertEqual(self.light.icon, "mdi:led-off")
  224. def test_light_is_on(self):
  225. self.dps[LIGHTOFF_DPS] = False
  226. self.assertEqual(self.light.is_on, True)
  227. self.dps[LIGHTOFF_DPS] = True
  228. self.assertEqual(self.light.is_on, False)
  229. def test_light_state_attributes(self):
  230. self.assertEqual(self.light.device_state_attributes, {})
  231. async def test_light_turn_on(self):
  232. async with assert_device_properties_set(
  233. self.light._device, {LIGHTOFF_DPS: False}
  234. ):
  235. await self.light.async_turn_on()
  236. async def test_light_turn_off(self):
  237. async with assert_device_properties_set(
  238. self.light._device, {LIGHTOFF_DPS: True}
  239. ):
  240. await self.light.async_turn_off()
  241. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  242. self.dps[LIGHTOFF_DPS] = True
  243. async with assert_device_properties_set(
  244. self.light._device, {LIGHTOFF_DPS: False}
  245. ):
  246. await self.light.async_toggle()
  247. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  248. self.dps[LIGHTOFF_DPS] = False
  249. async with assert_device_properties_set(
  250. self.light._device, {LIGHTOFF_DPS: True}
  251. ):
  252. await self.light.async_toggle()
  253. async def test_light_update(self):
  254. result = AsyncMock()
  255. self.light._device.async_refresh.return_value = result()
  256. await self.light.async_update()
  257. self.light._device.async_refresh.assert_called_once()
  258. result.assert_awaited()
  259. def test_switch_was_created(self):
  260. self.assertIsInstance(self.switch, TuyaLocalSwitch)
  261. def test_switch_is_same_device(self):
  262. self.assertEqual(self.switch._device, self.subject._device)
  263. def test_switch_class_is_switch(self):
  264. self.assertEqual(self.switch.device_class, DEVICE_CLASS_SWITCH)
  265. def test_switch_is_on(self):
  266. self.dps[SWITCH_DPS] = True
  267. self.assertTrue(self.switch.is_on)
  268. self.dps[SWITCH_DPS] = False
  269. self.assertFalse(self.switch.is_on)
  270. def test_switch_is_on_when_unavailable(self):
  271. self.dps[SWITCH_DPS] = None
  272. self.assertEqual(self.switch.is_on, STATE_UNAVAILABLE)
  273. async def test_switch_turn_on(self):
  274. async with assert_device_properties_set(
  275. self.switch._device, {SWITCH_DPS: True}
  276. ):
  277. await self.switch.async_turn_on()
  278. async def test_switch_turn_off(self):
  279. async with assert_device_properties_set(
  280. self.switch._device, {SWITCH_DPS: False}
  281. ):
  282. await self.switch.async_turn_off()
  283. async def test_toggle_turns_the_switch_on_when_it_was_off(self):
  284. self.dps[SWITCH_DPS] = False
  285. async with assert_device_properties_set(
  286. self.switch._device, {SWITCH_DPS: True}
  287. ):
  288. await self.switch.async_toggle()
  289. async def test_toggle_turns_the_switch_off_when_it_was_on(self):
  290. self.dps[SWITCH_DPS] = True
  291. async with assert_device_properties_set(
  292. self.switch._device, {SWITCH_DPS: False}
  293. ):
  294. await self.switch.async_toggle()
  295. def test_switch_returns_none_for_power(self):
  296. self.assertIsNone(self.switch.current_power_w)
  297. def test_switch_state_attributes_set(self):
  298. self.assertEqual(self.switch.device_state_attributes, {})