test_purline_m100_heater.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. def test_icon(self):
  81. self.dps[HVACMODE_DPS] = True
  82. self.dps[PRESET_DPS] = "auto"
  83. self.assertEqual(self.subject.icon, "mdi:radiator")
  84. self.dps[HVACMODE_DPS] = False
  85. self.assertEqual(self.subject.icon, "mdi:radiator-disabled")
  86. self.dps[HVACMODE_DPS] = True
  87. self.dps[PRESET_DPS] = "off"
  88. self.assertEqual(self.subject.icon, "mdi:fan")
  89. def test_temperature_unit_returns_device_temperature_unit(self):
  90. self.assertEqual(
  91. self.subject.temperature_unit, self.subject._device.temperature_unit
  92. )
  93. def test_target_temperature(self):
  94. self.dps[TEMPERATURE_DPS] = 25
  95. self.assertEqual(self.subject.target_temperature, 25)
  96. def test_target_temperature_step(self):
  97. self.assertEqual(self.subject.target_temperature_step, 1)
  98. def test_minimum_target_temperature(self):
  99. self.assertEqual(self.subject.min_temp, 15)
  100. def test_maximum_target_temperature(self):
  101. self.assertEqual(self.subject.max_temp, 35)
  102. async def test_legacy_set_temperature_with_temperature(self):
  103. async with assert_device_properties_set(
  104. self.subject._device, {TEMPERATURE_DPS: 25}
  105. ):
  106. await self.subject.async_set_temperature(temperature=25)
  107. async def test_legacy_set_temperature_with_no_valid_properties(self):
  108. await self.subject.async_set_temperature(something="else")
  109. self.subject._device.async_set_property.assert_not_called
  110. async def test_set_target_temperature(self):
  111. async with assert_device_properties_set(
  112. self.subject._device, {TEMPERATURE_DPS: 25}
  113. ):
  114. await self.subject.async_set_target_temperature(25)
  115. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  116. async with assert_device_properties_set(
  117. self.subject._device,
  118. {TEMPERATURE_DPS: 25},
  119. ):
  120. await self.subject.async_set_target_temperature(24.6)
  121. async def test_set_target_temperature_fails_outside_valid_range(self):
  122. with self.assertRaisesRegex(
  123. ValueError, "temperature \\(4\\) must be between 15 and 35"
  124. ):
  125. await self.subject.async_set_target_temperature(4)
  126. with self.assertRaisesRegex(
  127. ValueError, "temperature \\(36\\) must be between 15 and 35"
  128. ):
  129. await self.subject.async_set_target_temperature(36)
  130. def test_current_temperature(self):
  131. self.dps[CURRENTTEMP_DPS] = 25
  132. self.assertEqual(self.subject.current_temperature, 25)
  133. def test_hvac_mode(self):
  134. self.dps[HVACMODE_DPS] = True
  135. self.dps[PRESET_DPS] = "auto"
  136. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  137. self.dps[PRESET_DPS] = "off"
  138. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_FAN_ONLY)
  139. self.dps[HVACMODE_DPS] = False
  140. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  141. self.dps[HVACMODE_DPS] = None
  142. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  143. def test_hvac_modes(self):
  144. self.assertCountEqual(
  145. self.subject.hvac_modes,
  146. [HVAC_MODE_OFF, HVAC_MODE_HEAT, HVAC_MODE_FAN_ONLY],
  147. )
  148. async def test_turn_on(self):
  149. async with assert_device_properties_set_optional(
  150. self.subject._device,
  151. {HVACMODE_DPS: True},
  152. {PRESET_DPS: "auto"},
  153. ):
  154. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  155. async def test_turn_off(self):
  156. async with assert_device_properties_set(
  157. self.subject._device,
  158. {HVACMODE_DPS: False},
  159. ):
  160. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  161. async def test_turn_on_fan(self):
  162. async with assert_device_properties_set_optional(
  163. self.subject._device,
  164. {HVACMODE_DPS: True},
  165. {PRESET_DPS: "off"},
  166. ):
  167. await self.subject.async_set_hvac_mode(HVAC_MODE_FAN_ONLY)
  168. def test_preset_mode(self):
  169. self.dps[PRESET_DPS] = "auto"
  170. self.assertEqual(self.subject.preset_mode, "Auto")
  171. self.dps[PRESET_DPS] = "off"
  172. self.assertEqual(self.subject.preset_mode, "Fan")
  173. self.dps[PRESET_DPS] = "4"
  174. self.assertEqual(self.subject.preset_mode, "4")
  175. self.dps[PRESET_DPS] = None
  176. self.assertIs(self.subject.preset_mode, None)
  177. def test_preset_modes(self):
  178. self.assertCountEqual(
  179. self.subject.preset_modes,
  180. ["Fan", "1", "2", "3", "4", "5", "Auto"],
  181. )
  182. async def test_set_preset_mode_numeric(self):
  183. async with assert_device_properties_set(
  184. self.subject._device,
  185. {PRESET_DPS: "3"},
  186. ):
  187. await self.subject.async_set_preset_mode("3")
  188. def test_swing_mode(self):
  189. self.dps[SWING_DPS] = True
  190. self.assertEqual(self.subject.swing_mode, SWING_VERTICAL)
  191. self.dps[SWING_DPS] = False
  192. self.assertEqual(self.subject.swing_mode, SWING_OFF)
  193. def test_swing_modes(self):
  194. self.assertCountEqual(
  195. self.subject.swing_modes,
  196. [SWING_OFF, SWING_VERTICAL],
  197. )
  198. async def test_set_swing_mode_on(self):
  199. async with assert_device_properties_set(
  200. self.subject._device, {SWING_DPS: True}
  201. ):
  202. await self.subject.async_set_swing_mode(SWING_VERTICAL)
  203. async def test_set_swing_mode_off(self):
  204. async with assert_device_properties_set(
  205. self.subject._device, {SWING_DPS: False}
  206. ):
  207. await self.subject.async_set_swing_mode(SWING_OFF)
  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()
  214. def test_light_was_created(self):
  215. self.assertIsInstance(self.light, TuyaLocalLight)
  216. def test_light_is_same_device(self):
  217. self.assertEqual(self.light._device, self.subject._device)
  218. def test_light_icon(self):
  219. self.dps[LIGHTOFF_DPS] = False
  220. self.assertEqual(self.light.icon, "mdi:led-on")
  221. self.dps[LIGHTOFF_DPS] = True
  222. self.assertEqual(self.light.icon, "mdi:led-off")
  223. def test_light_is_on(self):
  224. self.dps[LIGHTOFF_DPS] = False
  225. self.assertEqual(self.light.is_on, True)
  226. self.dps[LIGHTOFF_DPS] = True
  227. self.assertEqual(self.light.is_on, False)
  228. def test_light_state_attributes(self):
  229. self.assertEqual(self.light.device_state_attributes, {})
  230. async def test_light_turn_on(self):
  231. async with assert_device_properties_set(
  232. self.light._device, {LIGHTOFF_DPS: False}
  233. ):
  234. await self.light.async_turn_on()
  235. async def test_light_turn_off(self):
  236. async with assert_device_properties_set(
  237. self.light._device, {LIGHTOFF_DPS: True}
  238. ):
  239. await self.light.async_turn_off()
  240. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  241. self.dps[LIGHTOFF_DPS] = True
  242. async with assert_device_properties_set(
  243. self.light._device, {LIGHTOFF_DPS: False}
  244. ):
  245. await self.light.async_toggle()
  246. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  247. self.dps[LIGHTOFF_DPS] = False
  248. async with assert_device_properties_set(
  249. self.light._device, {LIGHTOFF_DPS: True}
  250. ):
  251. await self.light.async_toggle()
  252. async def test_light_update(self):
  253. result = AsyncMock()
  254. self.light._device.async_refresh.return_value = result()
  255. await self.light.async_update()
  256. self.light._device.async_refresh.assert_called_once()
  257. result.assert_awaited()
  258. def test_switch_was_created(self):
  259. self.assertIsInstance(self.switch, TuyaLocalSwitch)
  260. def test_switch_is_same_device(self):
  261. self.assertEqual(self.switch._device, self.subject._device)
  262. def test_switch_class_is_switch(self):
  263. self.assertEqual(self.switch.device_class, DEVICE_CLASS_SWITCH)
  264. def test_switch_is_on(self):
  265. self.dps[SWITCH_DPS] = True
  266. self.assertTrue(self.switch.is_on)
  267. self.dps[SWITCH_DPS] = False
  268. self.assertFalse(self.switch.is_on)
  269. def test_switch_is_on_when_unavailable(self):
  270. self.dps[SWITCH_DPS] = None
  271. self.assertEqual(self.switch.is_on, STATE_UNAVAILABLE)
  272. async def test_switch_turn_on(self):
  273. async with assert_device_properties_set(
  274. self.switch._device, {SWITCH_DPS: True}
  275. ):
  276. await self.switch.async_turn_on()
  277. async def test_switch_turn_off(self):
  278. async with assert_device_properties_set(
  279. self.switch._device, {SWITCH_DPS: False}
  280. ):
  281. await self.switch.async_turn_off()
  282. async def test_toggle_turns_the_switch_on_when_it_was_off(self):
  283. self.dps[SWITCH_DPS] = False
  284. async with assert_device_properties_set(
  285. self.switch._device, {SWITCH_DPS: True}
  286. ):
  287. await self.switch.async_toggle()
  288. async def test_toggle_turns_the_switch_off_when_it_was_on(self):
  289. self.dps[SWITCH_DPS] = True
  290. async with assert_device_properties_set(
  291. self.switch._device, {SWITCH_DPS: False}
  292. ):
  293. await self.switch.async_toggle()
  294. def test_switch_returns_none_for_power(self):
  295. self.assertIsNone(self.switch.current_power_w)
  296. def test_switch_state_attributes_set(self):
  297. self.assertEqual(self.switch.device_state_attributes, {})