test_inkbird_itc306a_thermostat.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. from homeassistant.components.binary_sensor import (
  2. DEVICE_CLASS_COLD,
  3. DEVICE_CLASS_HEAT,
  4. DEVICE_CLASS_PROBLEM,
  5. )
  6. from homeassistant.components.climate.const import (
  7. CURRENT_HVAC_HEAT,
  8. CURRENT_HVAC_IDLE,
  9. SUPPORT_PRESET_MODE,
  10. SUPPORT_TARGET_TEMPERATURE_RANGE,
  11. )
  12. from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT, TIME_HOURS
  13. from ..const import INKBIRD_ITC306A_THERMOSTAT_PAYLOAD
  14. from ..helpers import assert_device_properties_set
  15. from ..mixins.binary_sensor import MultiBinarySensorTests
  16. from ..mixins.number import MultiNumberTests
  17. from ..mixins.select import BasicSelectTests
  18. from .base_device_tests import TuyaDeviceTestCase
  19. ERROR_DPS = "12"
  20. UNIT_DPS = "101"
  21. CALIBRATE_DPS = "102"
  22. PRESET_DPS = "103"
  23. CURRENTTEMP_DPS = "104"
  24. TEMPLOW_DPS = "106"
  25. TIME_THRES_DPS = "108"
  26. HIGH_THRES_DPS = "109"
  27. LOW_THRES_DPS = "110"
  28. ALARM_HIGH_DPS = "111"
  29. ALARM_LOW_DPS = "112"
  30. ALARM_DIFF_DPS = "113"
  31. TEMPHIGH_DPS = "114"
  32. SWITCH_DPS = "115"
  33. TEMPF_DPS = "116"
  34. UNKNOWN117_DPS = "117"
  35. UNKNOWN118_DPS = "118"
  36. UNKNOWN119_DPS = "119"
  37. ALARM_TIME_DPS = "120"
  38. class TestInkbirdThermostat(
  39. BasicSelectTests,
  40. MultiBinarySensorTests,
  41. MultiNumberTests,
  42. TuyaDeviceTestCase,
  43. ):
  44. __test__ = True
  45. def setUp(self):
  46. self.setUpForConfig(
  47. "inkbird_itc306a_thermostat.yaml", INKBIRD_ITC306A_THERMOSTAT_PAYLOAD
  48. )
  49. self.subject = self.entities.get("climate")
  50. self.setUpBasicSelect(
  51. UNIT_DPS,
  52. self.entities.get("select_temperature_unit"),
  53. {
  54. "C": "Celsius",
  55. "F": "Fahrenheit",
  56. },
  57. )
  58. self.setUpMultiBinarySensors(
  59. [
  60. {
  61. "name": "binary_sensor_high_temperature",
  62. "dps": ALARM_HIGH_DPS,
  63. "device_class": DEVICE_CLASS_HEAT,
  64. },
  65. {
  66. "name": "binary_sensor_low_temperature",
  67. "dps": ALARM_LOW_DPS,
  68. "device_class": DEVICE_CLASS_COLD,
  69. },
  70. {
  71. "name": "binary_sensor_continuous_heat",
  72. "dps": ALARM_TIME_DPS,
  73. "device_class": DEVICE_CLASS_PROBLEM,
  74. },
  75. {
  76. "name": "binary_sensor_unbalanced",
  77. "dps": ALARM_DIFF_DPS,
  78. "device_class": DEVICE_CLASS_PROBLEM,
  79. },
  80. {
  81. "name": "binary_sensor_error",
  82. "dps": ERROR_DPS,
  83. "device_class": DEVICE_CLASS_PROBLEM,
  84. "testdata": (1, 0),
  85. },
  86. ]
  87. )
  88. self.setUpMultiNumber(
  89. [
  90. {
  91. "name": "number_calibration_offset",
  92. "dps": CALIBRATE_DPS,
  93. "scale": 10,
  94. "step": 0.1,
  95. "min": -9.9,
  96. "max": 9.9,
  97. },
  98. {
  99. "name": "number_continuous_heat_hours",
  100. "dps": TIME_THRES_DPS,
  101. "max": 96,
  102. "unit": TIME_HOURS,
  103. },
  104. {
  105. "name": "number_high_temperature_limit",
  106. "dps": HIGH_THRES_DPS,
  107. "scale": 10,
  108. "step": 0.1,
  109. "min": -40,
  110. "max": 100,
  111. "unit": TEMP_CELSIUS,
  112. },
  113. {
  114. "name": "number_low_temperature_limit",
  115. "dps": LOW_THRES_DPS,
  116. "scale": 10,
  117. "step": 0.1,
  118. "min": -40,
  119. "max": 100,
  120. "unit": TEMP_CELSIUS,
  121. },
  122. ]
  123. )
  124. self.mark_secondary(
  125. [
  126. "select_temperature_unit",
  127. "binary_sensor_high_temperature",
  128. "binary_sensor_low_temperature",
  129. "binary_sensor_continuous_heat",
  130. "binary_sensor_unbalanced",
  131. "binary_sensor_error",
  132. "number_calibration_offset",
  133. "number_continuous_heat_hours",
  134. "number_high_temperature_limit",
  135. "number_low_temperature_limit",
  136. ]
  137. )
  138. def test_supported_features(self):
  139. self.assertEqual(
  140. self.subject.supported_features,
  141. SUPPORT_TARGET_TEMPERATURE_RANGE | SUPPORT_PRESET_MODE,
  142. )
  143. def test_icon(self):
  144. """Test that the icon is as expected."""
  145. self.dps[ALARM_HIGH_DPS] = False
  146. self.dps[ALARM_LOW_DPS] = False
  147. self.dps[ALARM_TIME_DPS] = False
  148. self.dps[SWITCH_DPS] = True
  149. self.assertEqual(self.subject.icon, "mdi:thermometer")
  150. self.dps[SWITCH_DPS] = False
  151. self.assertEqual(self.subject.icon, "mdi:thermometer-off")
  152. self.dps[ALARM_HIGH_DPS] = True
  153. self.assertEqual(self.subject.icon, "mdi:thermometer-alert")
  154. self.dps[SWITCH_DPS] = True
  155. self.assertEqual(self.subject.icon, "mdi:thermometer-alert")
  156. self.dps[ALARM_HIGH_DPS] = False
  157. self.dps[ALARM_LOW_DPS] = True
  158. self.assertEqual(self.subject.icon, "mdi:snowflake-alert")
  159. self.dps[ALARM_LOW_DPS] = False
  160. self.dps[ALARM_TIME_DPS] = True
  161. self.assertEqual(self.subject.icon, "mdi:clock-alert")
  162. self.dps[ALARM_TIME_DPS] = False
  163. self.dps[ALARM_DIFF_DPS] = True
  164. self.assertEqual(self.subject.icon, "mdi:thermometer-alert")
  165. def test_climate_hvac_modes(self):
  166. self.assertEqual(self.subject.hvac_modes, [])
  167. def test_preset_mode(self):
  168. self.dps[PRESET_DPS] = "on"
  169. self.assertEqual(self.subject.preset_mode, "On")
  170. self.dps[PRESET_DPS] = "pause"
  171. self.assertEqual(self.subject.preset_mode, "Pause")
  172. self.dps[PRESET_DPS] = "off"
  173. self.assertEqual(self.subject.preset_mode, "Off")
  174. self.dps[PRESET_DPS] = None
  175. self.assertEqual(self.subject.preset_mode, None)
  176. def test_preset_modes(self):
  177. self.assertCountEqual(
  178. self.subject.preset_modes,
  179. ["On", "Pause", "Off"],
  180. )
  181. async def test_set_preset_to_on(self):
  182. async with assert_device_properties_set(
  183. self.subject._device,
  184. {
  185. PRESET_DPS: "on",
  186. },
  187. ):
  188. await self.subject.async_set_preset_mode("On")
  189. self.subject._device.anticipate_property_value.assert_not_called()
  190. async def test_set_preset_to_pause(self):
  191. async with assert_device_properties_set(
  192. self.subject._device,
  193. {
  194. PRESET_DPS: "pause",
  195. },
  196. ):
  197. await self.subject.async_set_preset_mode("Pause")
  198. self.subject._device.anticipate_property_value.assert_not_called()
  199. async def test_set_preset_to_off(self):
  200. async with assert_device_properties_set(
  201. self.subject._device,
  202. {
  203. PRESET_DPS: "off",
  204. },
  205. ):
  206. await self.subject.async_set_preset_mode("Off")
  207. self.subject._device.anticipate_property_value.assert_not_called()
  208. def test_current_temperature(self):
  209. self.dps[UNIT_DPS] = "C"
  210. self.dps[CURRENTTEMP_DPS] = 289
  211. self.assertEqual(self.subject.current_temperature, 28.9)
  212. self.dps[UNIT_DPS] = "F"
  213. self.dps[TEMPF_DPS] = 789
  214. self.assertEqual(self.subject.current_temperature, 78.9)
  215. def test_temperature_unit(self):
  216. self.dps[UNIT_DPS] = "F"
  217. self.assertEqual(self.subject.temperature_unit, TEMP_FAHRENHEIT)
  218. self.dps[UNIT_DPS] = "C"
  219. self.assertEqual(self.subject.temperature_unit, TEMP_CELSIUS)
  220. def test_minimum_target_temperature(self):
  221. self.dps[UNIT_DPS] = "C"
  222. self.assertEqual(self.subject.min_temp, 0.0)
  223. self.dps[UNIT_DPS] = "F"
  224. self.assertEqual(self.subject.min_temp, 32.0)
  225. def test_maximum_target_temperature(self):
  226. self.dps[UNIT_DPS] = "C"
  227. self.assertEqual(self.subject.max_temp, 45.0)
  228. self.dps[UNIT_DPS] = "F"
  229. self.assertEqual(self.subject.max_temp, 113.0)
  230. def test_temperature_range(self):
  231. self.dps[TEMPHIGH_DPS] = 301
  232. self.dps[TEMPLOW_DPS] = 255
  233. self.assertEqual(self.subject.target_temperature_high, 30.1)
  234. self.assertEqual(self.subject.target_temperature_low, 25.5)
  235. async def test_set_temperature_range(self):
  236. async with assert_device_properties_set(
  237. self.subject._device,
  238. {
  239. TEMPHIGH_DPS: 322,
  240. TEMPLOW_DPS: 266,
  241. },
  242. ):
  243. await self.subject.async_set_temperature(
  244. target_temp_high=32.2, target_temp_low=26.6
  245. )
  246. async def test_set_target_temperature_fails_outside_valid_range(self):
  247. self.dps[UNIT_DPS] = "C"
  248. with self.assertRaisesRegex(
  249. ValueError, "target_temp_low \\(-0.1\\) must be between 0.0 and 45.0"
  250. ):
  251. await self.subject.async_set_temperature(
  252. target_temp_high=32.2, target_temp_low=-0.1
  253. )
  254. self.dps[UNIT_DPS] = "F"
  255. with self.assertRaisesRegex(
  256. ValueError, "target_temp_high \\(113.1\\) must be between 32.0 and 113.0"
  257. ):
  258. await self.subject.async_set_temperature(
  259. target_temp_low=70.0, target_temp_high=113.1
  260. )
  261. def test_hvac_action(self):
  262. self.dps[SWITCH_DPS] = False
  263. self.assertEqual(self.subject.hvac_action, CURRENT_HVAC_IDLE)
  264. self.dps[SWITCH_DPS] = True
  265. self.assertEqual(self.subject.hvac_action, CURRENT_HVAC_HEAT)
  266. def test_extra_state_attributes(self):
  267. self.dps[ERROR_DPS] = 1
  268. self.dps[UNKNOWN117_DPS] = True
  269. self.dps[UNKNOWN118_DPS] = False
  270. self.dps[UNKNOWN119_DPS] = True
  271. self.assertDictEqual(
  272. self.subject.extra_state_attributes,
  273. {
  274. "error": 1,
  275. "unknown_117": True,
  276. "unknown_118": False,
  277. "unknown_119": True,
  278. },
  279. )