test_awow_th213_thermostat.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.components.lock import STATE_LOCKED, STATE_UNLOCKED
  11. from homeassistant.const import STATE_UNAVAILABLE
  12. from ..const import TH213_THERMOSTAT_PAYLOAD
  13. from ..helpers import assert_device_properties_set
  14. from .base_device_tests import TuyaDeviceTestCase
  15. HVACMODE_DPS = "1"
  16. TEMPERATURE_DPS = "2"
  17. CURRENTTEMP_DPS = "3"
  18. PRESET_DPS = "4"
  19. LOCK_DPS = "6"
  20. ERROR_DPS = "12"
  21. EXTERNTEMP_DPS = "101"
  22. SENSOR_DPS = "102"
  23. CALIBRATE_DPS = "103"
  24. CALIBSWING_DPS = "104"
  25. HVACACTION_DPS = "105"
  26. UNKNOWN107_DPS = "107"
  27. UNKNOWN108_DPS = "108"
  28. UNKNOWN110_DPS = "110"
  29. class TestAwowTH213Thermostat(TuyaDeviceTestCase):
  30. __test__ = True
  31. def setUp(self):
  32. self.setUpForConfig("awow_th213_thermostat.yaml", TH213_THERMOSTAT_PAYLOAD)
  33. self.subject = self.entities.get("climate")
  34. self.lock = self.entities.get("lock")
  35. def test_supported_features(self):
  36. self.assertEqual(
  37. self.subject.supported_features,
  38. SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE,
  39. )
  40. def test_icon(self):
  41. self.dps[HVACACTION_DPS] = True
  42. self.assertEqual(self.subject.icon, "mdi:thermometer")
  43. self.dps[HVACACTION_DPS] = False
  44. self.assertEqual(self.subject.icon, "mdi:thermometer-off")
  45. self.dps[LOCK_DPS] = True
  46. self.assertEqual(self.lock.icon, "mdi:account-lock")
  47. self.dps[LOCK_DPS] = False
  48. self.assertEqual(self.lock.icon, "mdi:account")
  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, 5)
  60. def test_maximum_target_temperature(self):
  61. self.assertEqual(self.subject.max_temp, 30)
  62. async def test_legacy_set_temperature_with_temperature(self):
  63. async with assert_device_properties_set(
  64. self.subject._device, {TEMPERATURE_DPS: 24}
  65. ):
  66. await self.subject.async_set_temperature(temperature=24)
  67. async def test_legacy_set_temperature_with_preset_mode(self):
  68. async with assert_device_properties_set(self.subject._device, {PRESET_DPS: 2}):
  69. await self.subject.async_set_temperature(preset_mode="Away")
  70. async def test_legacy_set_temperature_with_both_properties(self):
  71. async with assert_device_properties_set(
  72. self.subject._device,
  73. {
  74. TEMPERATURE_DPS: 25,
  75. PRESET_DPS: 3,
  76. },
  77. ):
  78. await self.subject.async_set_temperature(
  79. temperature=25, preset_mode="Smart"
  80. )
  81. async def test_legacy_set_temperature_with_no_valid_properties(self):
  82. await self.subject.async_set_temperature(something="else")
  83. self.subject._device.async_set_property.assert_not_called()
  84. async def test_set_target_temperature(self):
  85. async with assert_device_properties_set(
  86. self.subject._device, {TEMPERATURE_DPS: 25}
  87. ):
  88. await self.subject.async_set_target_temperature(25)
  89. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  90. async with assert_device_properties_set(
  91. self.subject._device,
  92. {TEMPERATURE_DPS: 25},
  93. ):
  94. await self.subject.async_set_target_temperature(24.6)
  95. async def test_set_target_temperature_fails_outside_valid_range(self):
  96. with self.assertRaisesRegex(
  97. ValueError, "temperature \\(4\\) must be between 5 and 30"
  98. ):
  99. await self.subject.async_set_target_temperature(4)
  100. with self.assertRaisesRegex(
  101. ValueError, "temperature \\(31\\) must be between 5 and 30"
  102. ):
  103. await self.subject.async_set_target_temperature(31)
  104. def test_current_temperature(self):
  105. self.dps[CURRENTTEMP_DPS] = 25
  106. self.assertEqual(self.subject.current_temperature, 25)
  107. def test_hvac_mode(self):
  108. self.dps[HVACMODE_DPS] = True
  109. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  110. self.dps[HVACMODE_DPS] = False
  111. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  112. self.dps[HVACMODE_DPS] = None
  113. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  114. def test_hvac_modes(self):
  115. self.assertCountEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_HEAT])
  116. async def test_turn_on(self):
  117. async with assert_device_properties_set(
  118. self.subject._device, {HVACMODE_DPS: True}
  119. ):
  120. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  121. async def test_turn_off(self):
  122. async with assert_device_properties_set(
  123. self.subject._device, {HVACMODE_DPS: False}
  124. ):
  125. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  126. def test_preset_mode(self):
  127. self.dps[PRESET_DPS] = "1"
  128. self.assertEqual(self.subject.preset_mode, "Home")
  129. self.dps[PRESET_DPS] = "2"
  130. self.assertEqual(self.subject.preset_mode, "Away")
  131. self.dps[PRESET_DPS] = "3"
  132. self.assertEqual(self.subject.preset_mode, "Smart")
  133. self.dps[PRESET_DPS] = "4"
  134. self.assertEqual(self.subject.preset_mode, "Sleep")
  135. self.dps[PRESET_DPS] = None
  136. self.assertEqual(self.subject.preset_mode, None)
  137. def test_preset_modes(self):
  138. self.assertCountEqual(
  139. self.subject.preset_modes,
  140. ["Home", "Away", "Smart", "Sleep"],
  141. )
  142. async def test_set_preset_mode_to_home(self):
  143. async with assert_device_properties_set(
  144. self.subject._device,
  145. {PRESET_DPS: 1},
  146. ):
  147. await self.subject.async_set_preset_mode("Home")
  148. async def test_set_preset_mode_to_away(self):
  149. async with assert_device_properties_set(
  150. self.subject._device,
  151. {PRESET_DPS: 2},
  152. ):
  153. await self.subject.async_set_preset_mode("Away")
  154. async def test_set_preset_mode_to_smart(self):
  155. async with assert_device_properties_set(
  156. self.subject._device,
  157. {PRESET_DPS: 3},
  158. ):
  159. await self.subject.async_set_preset_mode("Smart")
  160. async def test_set_preset_mode_to_sleep(self):
  161. async with assert_device_properties_set(
  162. self.subject._device,
  163. {PRESET_DPS: 4},
  164. ):
  165. await self.subject.async_set_preset_mode("Sleep")
  166. def test_hvac_action(self):
  167. self.dps[HVACMODE_DPS] = True
  168. self.dps[HVACACTION_DPS] = True
  169. self.assertEqual(self.subject.hvac_action, CURRENT_HVAC_HEAT)
  170. self.dps[HVACACTION_DPS] = False
  171. self.assertEqual(self.subject.hvac_action, CURRENT_HVAC_IDLE)
  172. self.dps[HVACMODE_DPS] = False
  173. self.assertEqual(self.subject.hvac_action, CURRENT_HVAC_OFF)
  174. def test_device_state_attributes(self):
  175. self.dps[ERROR_DPS] = 8
  176. self.dps[EXTERNTEMP_DPS] = 27
  177. self.dps[SENSOR_DPS] = 1
  178. self.dps[CALIBRATE_DPS] = 2
  179. self.dps[CALIBSWING_DPS] = 3
  180. self.dps[UNKNOWN107_DPS] = True
  181. self.dps[UNKNOWN108_DPS] = False
  182. self.dps[UNKNOWN110_DPS] = 110
  183. self.assertCountEqual(
  184. self.subject.device_state_attributes,
  185. {
  186. "error": 8,
  187. "external_temperature": 27,
  188. "sensor": "External",
  189. "temperature_calibration_offset": 2,
  190. "temperature_calibration_swing": 3,
  191. "unknown_107": True,
  192. "unknown_108": False,
  193. "unknown_110": 110,
  194. },
  195. )
  196. def test_lock_state(self):
  197. self.dps[LOCK_DPS] = True
  198. self.assertEqual(self.lock.state, STATE_LOCKED)
  199. self.dps[LOCK_DPS] = False
  200. self.assertEqual(self.lock.state, STATE_UNLOCKED)
  201. self.dps[LOCK_DPS] = None
  202. self.assertEqual(self.lock.state, STATE_UNAVAILABLE)
  203. def test_lock_is_locked(self):
  204. self.dps[LOCK_DPS] = True
  205. self.assertTrue(self.lock.is_locked)
  206. self.dps[LOCK_DPS] = False
  207. self.assertFalse(self.lock.is_locked)
  208. self.dps[LOCK_DPS] = None
  209. self.assertFalse(self.lock.is_locked)
  210. async def test_lock_locks(self):
  211. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: True}):
  212. await self.lock.async_lock()
  213. async def test_lock_unlocks(self):
  214. async with assert_device_properties_set(self.lock._device, {LOCK_DPS: False}):
  215. await self.lock.async_unlock()