test_purline_m100_heater.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. from homeassistant.components.climate.const import (
  2. HVAC_MODE_FAN_ONLY,
  3. HVAC_MODE_HEAT,
  4. HVAC_MODE_OFF,
  5. SUPPORT_PRESET_MODE,
  6. SUPPORT_SWING_MODE,
  7. SUPPORT_TARGET_TEMPERATURE,
  8. SWING_OFF,
  9. SWING_VERTICAL,
  10. )
  11. from homeassistant.components.light import COLOR_MODE_ONOFF
  12. from homeassistant.components.switch import DEVICE_CLASS_SWITCH
  13. from homeassistant.const import STATE_UNAVAILABLE
  14. from ..const import PURLINE_M100_HEATER_PAYLOAD
  15. from ..helpers import (
  16. assert_device_properties_set,
  17. assert_device_properties_set_optional,
  18. )
  19. from .base_device_tests import TuyaDeviceTestCase
  20. HVACMODE_DPS = "1"
  21. TEMPERATURE_DPS = "2"
  22. CURRENTTEMP_DPS = "3"
  23. PRESET_DPS = "5"
  24. LIGHTOFF_DPS = "10"
  25. TIMERHR_DPS = "11"
  26. TIMER_DPS = "12"
  27. SWITCH_DPS = "101"
  28. SWING_DPS = "102"
  29. class TestPulineM100Heater(TuyaDeviceTestCase):
  30. __test__ = True
  31. def setUp(self):
  32. self.setUpForConfig("purline_m100_heater.yaml", PURLINE_M100_HEATER_PAYLOAD)
  33. self.subject = self.entities.get("climate")
  34. self.light = self.entities.get("light")
  35. self.switch = self.entities.get("switch")
  36. def test_supported_features(self):
  37. self.assertEqual(
  38. self.subject.supported_features,
  39. SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE | SUPPORT_SWING_MODE,
  40. )
  41. def test_icon(self):
  42. self.dps[HVACMODE_DPS] = True
  43. self.dps[PRESET_DPS] = "auto"
  44. self.assertEqual(self.subject.icon, "mdi:radiator")
  45. self.dps[HVACMODE_DPS] = False
  46. self.assertEqual(self.subject.icon, "mdi:radiator-disabled")
  47. self.dps[HVACMODE_DPS] = True
  48. self.dps[PRESET_DPS] = "off"
  49. self.assertEqual(self.subject.icon, "mdi:fan")
  50. def test_temperature_unit_returns_device_temperature_unit(self):
  51. self.assertEqual(
  52. self.subject.temperature_unit, self.subject._device.temperature_unit
  53. )
  54. def test_target_temperature(self):
  55. self.dps[TEMPERATURE_DPS] = 25
  56. self.assertEqual(self.subject.target_temperature, 25)
  57. def test_target_temperature_step(self):
  58. self.assertEqual(self.subject.target_temperature_step, 1)
  59. def test_minimum_target_temperature(self):
  60. self.assertEqual(self.subject.min_temp, 15)
  61. def test_maximum_target_temperature(self):
  62. self.assertEqual(self.subject.max_temp, 35)
  63. async def test_legacy_set_temperature_with_temperature(self):
  64. async with assert_device_properties_set(
  65. self.subject._device, {TEMPERATURE_DPS: 25}
  66. ):
  67. await self.subject.async_set_temperature(temperature=25)
  68. async def test_legacy_set_temperature_with_no_valid_properties(self):
  69. await self.subject.async_set_temperature(something="else")
  70. self.subject._device.async_set_property.assert_not_called()
  71. async def test_set_target_temperature(self):
  72. async with assert_device_properties_set(
  73. self.subject._device, {TEMPERATURE_DPS: 25}
  74. ):
  75. await self.subject.async_set_target_temperature(25)
  76. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  77. async with assert_device_properties_set(
  78. self.subject._device,
  79. {TEMPERATURE_DPS: 25},
  80. ):
  81. await self.subject.async_set_target_temperature(24.6)
  82. async def test_set_target_temperature_fails_outside_valid_range(self):
  83. with self.assertRaisesRegex(
  84. ValueError, "temperature \\(4\\) must be between 15 and 35"
  85. ):
  86. await self.subject.async_set_target_temperature(4)
  87. with self.assertRaisesRegex(
  88. ValueError, "temperature \\(36\\) must be between 15 and 35"
  89. ):
  90. await self.subject.async_set_target_temperature(36)
  91. def test_current_temperature(self):
  92. self.dps[CURRENTTEMP_DPS] = 25
  93. self.assertEqual(self.subject.current_temperature, 25)
  94. def test_hvac_mode(self):
  95. self.dps[HVACMODE_DPS] = True
  96. self.dps[PRESET_DPS] = "auto"
  97. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  98. self.dps[PRESET_DPS] = "off"
  99. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_FAN_ONLY)
  100. self.dps[HVACMODE_DPS] = False
  101. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  102. self.dps[HVACMODE_DPS] = None
  103. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  104. def test_hvac_modes(self):
  105. self.assertCountEqual(
  106. self.subject.hvac_modes,
  107. [HVAC_MODE_OFF, HVAC_MODE_HEAT, HVAC_MODE_FAN_ONLY],
  108. )
  109. async def test_turn_on(self):
  110. async with assert_device_properties_set_optional(
  111. self.subject._device,
  112. {HVACMODE_DPS: True},
  113. {PRESET_DPS: "auto"},
  114. ):
  115. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  116. async def test_turn_off(self):
  117. async with assert_device_properties_set(
  118. self.subject._device,
  119. {HVACMODE_DPS: False},
  120. ):
  121. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  122. async def test_turn_on_fan(self):
  123. async with assert_device_properties_set_optional(
  124. self.subject._device,
  125. {HVACMODE_DPS: True},
  126. {PRESET_DPS: "off"},
  127. ):
  128. await self.subject.async_set_hvac_mode(HVAC_MODE_FAN_ONLY)
  129. def test_preset_mode(self):
  130. self.dps[PRESET_DPS] = "auto"
  131. self.assertEqual(self.subject.preset_mode, "Auto")
  132. self.dps[PRESET_DPS] = "off"
  133. self.assertEqual(self.subject.preset_mode, "Fan")
  134. self.dps[PRESET_DPS] = "4"
  135. self.assertEqual(self.subject.preset_mode, "4")
  136. self.dps[PRESET_DPS] = None
  137. self.assertIs(self.subject.preset_mode, None)
  138. def test_preset_modes(self):
  139. self.assertCountEqual(
  140. self.subject.preset_modes,
  141. ["Fan", "1", "2", "3", "4", "5", "Auto"],
  142. )
  143. async def test_set_preset_mode_numeric(self):
  144. async with assert_device_properties_set(
  145. self.subject._device,
  146. {PRESET_DPS: "3"},
  147. ):
  148. await self.subject.async_set_preset_mode("3")
  149. def test_swing_mode(self):
  150. self.dps[SWING_DPS] = True
  151. self.assertEqual(self.subject.swing_mode, SWING_VERTICAL)
  152. self.dps[SWING_DPS] = False
  153. self.assertEqual(self.subject.swing_mode, SWING_OFF)
  154. def test_swing_modes(self):
  155. self.assertCountEqual(
  156. self.subject.swing_modes,
  157. [SWING_OFF, SWING_VERTICAL],
  158. )
  159. async def test_set_swing_mode_on(self):
  160. async with assert_device_properties_set(
  161. self.subject._device, {SWING_DPS: True}
  162. ):
  163. await self.subject.async_set_swing_mode(SWING_VERTICAL)
  164. async def test_set_swing_mode_off(self):
  165. async with assert_device_properties_set(
  166. self.subject._device, {SWING_DPS: False}
  167. ):
  168. await self.subject.async_set_swing_mode(SWING_OFF)
  169. def test_light_supported_color_modes(self):
  170. self.assertCountEqual(
  171. self.light.supported_color_modes,
  172. [COLOR_MODE_ONOFF],
  173. )
  174. def test_light_color_mode(self):
  175. self.assertEqual(self.light.color_mode, COLOR_MODE_ONOFF)
  176. def test_light_icon(self):
  177. self.dps[LIGHTOFF_DPS] = False
  178. self.assertEqual(self.light.icon, "mdi:led-on")
  179. self.dps[LIGHTOFF_DPS] = True
  180. self.assertEqual(self.light.icon, "mdi:led-off")
  181. def test_light_is_on(self):
  182. self.dps[LIGHTOFF_DPS] = False
  183. self.assertEqual(self.light.is_on, True)
  184. self.dps[LIGHTOFF_DPS] = True
  185. self.assertEqual(self.light.is_on, False)
  186. def test_light_state_attributes(self):
  187. self.assertEqual(self.light.device_state_attributes, {})
  188. async def test_light_turn_on(self):
  189. async with assert_device_properties_set(
  190. self.light._device, {LIGHTOFF_DPS: False}
  191. ):
  192. await self.light.async_turn_on()
  193. async def test_light_turn_off(self):
  194. async with assert_device_properties_set(
  195. self.light._device, {LIGHTOFF_DPS: True}
  196. ):
  197. await self.light.async_turn_off()
  198. async def test_toggle_turns_the_light_on_when_it_was_off(self):
  199. self.dps[LIGHTOFF_DPS] = True
  200. async with assert_device_properties_set(
  201. self.light._device, {LIGHTOFF_DPS: False}
  202. ):
  203. await self.light.async_toggle()
  204. async def test_toggle_turns_the_light_off_when_it_was_on(self):
  205. self.dps[LIGHTOFF_DPS] = False
  206. async with assert_device_properties_set(
  207. self.light._device, {LIGHTOFF_DPS: True}
  208. ):
  209. await self.light.async_toggle()
  210. def test_switch_class_is_switch(self):
  211. self.assertEqual(self.switch.device_class, DEVICE_CLASS_SWITCH)
  212. def test_switch_is_on(self):
  213. self.dps[SWITCH_DPS] = True
  214. self.assertTrue(self.switch.is_on)
  215. self.dps[SWITCH_DPS] = False
  216. self.assertFalse(self.switch.is_on)
  217. def test_switch_is_on_when_unavailable(self):
  218. self.dps[SWITCH_DPS] = None
  219. self.assertEqual(self.switch.is_on, STATE_UNAVAILABLE)
  220. async def test_switch_turn_on(self):
  221. async with assert_device_properties_set(
  222. self.switch._device, {SWITCH_DPS: True}
  223. ):
  224. await self.switch.async_turn_on()
  225. async def test_switch_turn_off(self):
  226. async with assert_device_properties_set(
  227. self.switch._device, {SWITCH_DPS: False}
  228. ):
  229. await self.switch.async_turn_off()
  230. async def test_toggle_turns_the_switch_on_when_it_was_off(self):
  231. self.dps[SWITCH_DPS] = False
  232. async with assert_device_properties_set(
  233. self.switch._device, {SWITCH_DPS: True}
  234. ):
  235. await self.switch.async_toggle()
  236. async def test_toggle_turns_the_switch_off_when_it_was_on(self):
  237. self.dps[SWITCH_DPS] = True
  238. async with assert_device_properties_set(
  239. self.switch._device, {SWITCH_DPS: False}
  240. ):
  241. await self.switch.async_toggle()
  242. def test_switch_returns_none_for_power(self):
  243. self.assertIsNone(self.switch.current_power_w)
  244. def test_switch_state_attributes_set(self):
  245. self.assertEqual(self.switch.device_state_attributes, {})