test_awow_th213_thermostat.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. from homeassistant.components.climate.const import (
  2. CURRENT_HVAC_HEAT,
  3. CURRENT_HVAC_IDLE,
  4. CURRENT_HVAC_OFF,
  5. HVAC_MODE_HEAT,
  6. HVAC_MODE_OFF,
  7. SUPPORT_PRESET_MODE,
  8. SUPPORT_TARGET_TEMPERATURE,
  9. )
  10. from homeassistant.const import (
  11. DEVICE_CLASS_TEMPERATURE,
  12. STATE_UNAVAILABLE,
  13. TEMP_CELSIUS,
  14. )
  15. from ..const import TH213_THERMOSTAT_PAYLOAD
  16. from ..helpers import assert_device_properties_set
  17. from ..mixins.lock import BasicLockTests
  18. from ..mixins.number import MultiNumberTests
  19. from ..mixins.select import BasicSelectTests
  20. from ..mixins.sensor import BasicSensorTests
  21. from .base_device_tests import TuyaDeviceTestCase
  22. HVACMODE_DPS = "1"
  23. TEMPERATURE_DPS = "2"
  24. CURRENTTEMP_DPS = "3"
  25. PRESET_DPS = "4"
  26. LOCK_DPS = "6"
  27. ERROR_DPS = "12"
  28. EXTERNTEMP_DPS = "101"
  29. SENSOR_DPS = "102"
  30. CALIBRATE_DPS = "103"
  31. CALIBSWING_DPS = "104"
  32. HVACACTION_DPS = "105"
  33. UNKNOWN107_DPS = "107"
  34. UNKNOWN108_DPS = "108"
  35. UNKNOWN110_DPS = "110"
  36. class TestAwowTH213Thermostat(
  37. BasicLockTests,
  38. BasicSelectTests,
  39. BasicSensorTests,
  40. MultiNumberTests,
  41. TuyaDeviceTestCase,
  42. ):
  43. __test__ = True
  44. def setUp(self):
  45. self.setUpForConfig("awow_th213_thermostat.yaml", TH213_THERMOSTAT_PAYLOAD)
  46. self.subject = self.entities.get("climate")
  47. self.setUpBasicLock(LOCK_DPS, self.entities.get("lock_child_lock"))
  48. self.setUpBasicSensor(
  49. EXTERNTEMP_DPS,
  50. self.entities.get("sensor_external_temperature"),
  51. unit=TEMP_CELSIUS,
  52. device_class=DEVICE_CLASS_TEMPERATURE,
  53. state_class="measurement",
  54. )
  55. self.setUpBasicSelect(
  56. SENSOR_DPS,
  57. self.entities.get("select_temperature_sensor"),
  58. {
  59. 0: "Internal",
  60. 1: "External",
  61. 2: "Both",
  62. },
  63. )
  64. self.setUpMultiNumber(
  65. [
  66. {
  67. "name": "number_calibration_offset",
  68. "dps": CALIBRATE_DPS,
  69. "min": -9,
  70. "max": 9,
  71. },
  72. {
  73. "name": "number_calibration_swing",
  74. "dps": CALIBSWING_DPS,
  75. "min": 1,
  76. "max": 9,
  77. },
  78. ]
  79. )
  80. def test_supported_features(self):
  81. self.assertEqual(
  82. self.subject.supported_features,
  83. SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE,
  84. )
  85. def test_icon(self):
  86. self.dps[HVACACTION_DPS] = True
  87. self.assertEqual(self.subject.icon, "mdi:thermometer")
  88. self.dps[HVACACTION_DPS] = False
  89. self.assertEqual(self.subject.icon, "mdi:thermometer-off")
  90. self.dps[LOCK_DPS] = True
  91. self.assertEqual(self.basicLock.icon, "mdi:hand-back-right-off")
  92. self.dps[LOCK_DPS] = False
  93. self.assertEqual(self.basicLock.icon, "mdi:hand-back-right")
  94. def test_temperature_unit_returns_device_temperature_unit(self):
  95. self.assertEqual(
  96. self.subject.temperature_unit, self.subject._device.temperature_unit
  97. )
  98. def test_target_temperature(self):
  99. self.dps[TEMPERATURE_DPS] = 25
  100. self.assertEqual(self.subject.target_temperature, 25)
  101. def test_target_temperature_step(self):
  102. self.assertEqual(self.subject.target_temperature_step, 1)
  103. def test_minimum_target_temperature(self):
  104. self.assertEqual(self.subject.min_temp, 5)
  105. def test_maximum_target_temperature(self):
  106. self.assertEqual(self.subject.max_temp, 30)
  107. async def test_legacy_set_temperature_with_temperature(self):
  108. async with assert_device_properties_set(
  109. self.subject._device, {TEMPERATURE_DPS: 24}
  110. ):
  111. await self.subject.async_set_temperature(temperature=24)
  112. async def test_legacy_set_temperature_with_preset_mode(self):
  113. async with assert_device_properties_set(self.subject._device, {PRESET_DPS: 2}):
  114. await self.subject.async_set_temperature(preset_mode="Away")
  115. async def test_legacy_set_temperature_with_both_properties(self):
  116. async with assert_device_properties_set(
  117. self.subject._device,
  118. {
  119. TEMPERATURE_DPS: 25,
  120. PRESET_DPS: 3,
  121. },
  122. ):
  123. await self.subject.async_set_temperature(
  124. temperature=25, preset_mode="Smart"
  125. )
  126. async def test_legacy_set_temperature_with_no_valid_properties(self):
  127. await self.subject.async_set_temperature(something="else")
  128. self.subject._device.async_set_property.assert_not_called()
  129. async def test_set_target_temperature(self):
  130. async with assert_device_properties_set(
  131. self.subject._device, {TEMPERATURE_DPS: 25}
  132. ):
  133. await self.subject.async_set_target_temperature(25)
  134. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  135. async with assert_device_properties_set(
  136. self.subject._device,
  137. {TEMPERATURE_DPS: 25},
  138. ):
  139. await self.subject.async_set_target_temperature(24.6)
  140. async def test_set_target_temperature_fails_outside_valid_range(self):
  141. with self.assertRaisesRegex(
  142. ValueError, "temperature \\(4\\) must be between 5 and 30"
  143. ):
  144. await self.subject.async_set_target_temperature(4)
  145. with self.assertRaisesRegex(
  146. ValueError, "temperature \\(31\\) must be between 5 and 30"
  147. ):
  148. await self.subject.async_set_target_temperature(31)
  149. def test_current_temperature(self):
  150. self.dps[CURRENTTEMP_DPS] = 25
  151. self.assertEqual(self.subject.current_temperature, 25)
  152. def test_hvac_mode(self):
  153. self.dps[HVACMODE_DPS] = True
  154. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  155. self.dps[HVACMODE_DPS] = False
  156. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  157. self.dps[HVACMODE_DPS] = None
  158. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  159. def test_hvac_modes(self):
  160. self.assertCountEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_HEAT])
  161. async def test_turn_on(self):
  162. async with assert_device_properties_set(
  163. self.subject._device, {HVACMODE_DPS: True}
  164. ):
  165. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  166. async def test_turn_off(self):
  167. async with assert_device_properties_set(
  168. self.subject._device, {HVACMODE_DPS: False}
  169. ):
  170. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  171. def test_preset_mode(self):
  172. self.dps[PRESET_DPS] = "1"
  173. self.assertEqual(self.subject.preset_mode, "Home")
  174. self.dps[PRESET_DPS] = "2"
  175. self.assertEqual(self.subject.preset_mode, "Away")
  176. self.dps[PRESET_DPS] = "3"
  177. self.assertEqual(self.subject.preset_mode, "Smart")
  178. self.dps[PRESET_DPS] = "4"
  179. self.assertEqual(self.subject.preset_mode, "Sleep")
  180. self.dps[PRESET_DPS] = None
  181. self.assertEqual(self.subject.preset_mode, None)
  182. def test_preset_modes(self):
  183. self.assertCountEqual(
  184. self.subject.preset_modes,
  185. ["Home", "Away", "Smart", "Sleep"],
  186. )
  187. async def test_set_preset_mode_to_home(self):
  188. async with assert_device_properties_set(
  189. self.subject._device,
  190. {PRESET_DPS: 1},
  191. ):
  192. await self.subject.async_set_preset_mode("Home")
  193. async def test_set_preset_mode_to_away(self):
  194. async with assert_device_properties_set(
  195. self.subject._device,
  196. {PRESET_DPS: 2},
  197. ):
  198. await self.subject.async_set_preset_mode("Away")
  199. async def test_set_preset_mode_to_smart(self):
  200. async with assert_device_properties_set(
  201. self.subject._device,
  202. {PRESET_DPS: 3},
  203. ):
  204. await self.subject.async_set_preset_mode("Smart")
  205. async def test_set_preset_mode_to_sleep(self):
  206. async with assert_device_properties_set(
  207. self.subject._device,
  208. {PRESET_DPS: 4},
  209. ):
  210. await self.subject.async_set_preset_mode("Sleep")
  211. def test_hvac_action(self):
  212. self.dps[HVACMODE_DPS] = True
  213. self.dps[HVACACTION_DPS] = True
  214. self.assertEqual(self.subject.hvac_action, CURRENT_HVAC_HEAT)
  215. self.dps[HVACACTION_DPS] = False
  216. self.assertEqual(self.subject.hvac_action, CURRENT_HVAC_IDLE)
  217. self.dps[HVACMODE_DPS] = False
  218. self.assertEqual(self.subject.hvac_action, CURRENT_HVAC_OFF)
  219. def test_device_state_attributes(self):
  220. self.dps[ERROR_DPS] = 8
  221. self.dps[EXTERNTEMP_DPS] = 27
  222. self.dps[SENSOR_DPS] = 1
  223. self.dps[CALIBRATE_DPS] = 2
  224. self.dps[CALIBSWING_DPS] = 3
  225. self.dps[UNKNOWN107_DPS] = True
  226. self.dps[UNKNOWN108_DPS] = False
  227. self.dps[UNKNOWN110_DPS] = 110
  228. self.assertDictEqual(
  229. self.subject.device_state_attributes,
  230. {
  231. "error": 8,
  232. "external_temperature": 27,
  233. "sensor": "External",
  234. "temperature_calibration_offset": 2,
  235. "temperature_calibration_swing": 3,
  236. "unknown_107": True,
  237. "unknown_108": False,
  238. "unknown_110": 110,
  239. },
  240. )