test_purline_m100_heater.py 9.8 KB

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