test_inkbird_itc306a_thermostat.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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_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_TIME_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. UNKNOWN120_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_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_error",
  77. "dps": ERROR_DPS,
  78. "device_class": DEVICE_CLASS_PROBLEM,
  79. "testdata": (1, 0),
  80. },
  81. ]
  82. )
  83. self.setUpMultiNumber(
  84. [
  85. {
  86. "name": "number_calibration_offset",
  87. "dps": CALIBRATE_DPS,
  88. "scale": 10,
  89. "step": 0.1,
  90. "min": -9.9,
  91. "max": 9.9,
  92. "unit": TEMP_CELSIUS,
  93. },
  94. {
  95. "name": "number_continuous_heat_hours",
  96. "dps": TIME_THRES_DPS,
  97. "max": 96,
  98. "unit": TIME_HOURS,
  99. },
  100. {
  101. "name": "number_high_temperature_limit",
  102. "dps": HIGH_THRES_DPS,
  103. "scale": 10,
  104. "step": 0.1,
  105. "min": -40,
  106. "max": 100,
  107. "unit": TEMP_CELSIUS,
  108. },
  109. {
  110. "name": "number_low_temperature_limit",
  111. "dps": LOW_THRES_DPS,
  112. "scale": 10,
  113. "step": 0.1,
  114. "min": -40,
  115. "max": 100,
  116. "unit": TEMP_CELSIUS,
  117. },
  118. ]
  119. )
  120. self.mark_secondary(
  121. [
  122. "select_temperature_unit",
  123. "binary_sensor_high_temperature",
  124. "binary_sensor_low_temperature",
  125. "binary_sensor_continuous_heat",
  126. "binary_sensor_error",
  127. "number_calibration_offset",
  128. "number_continuous_heat_hours",
  129. "number_high_temperature_limit",
  130. "number_low_temperature_limit",
  131. ]
  132. )
  133. def test_supported_features(self):
  134. self.assertEqual(
  135. self.subject.supported_features,
  136. SUPPORT_TARGET_TEMPERATURE_RANGE | SUPPORT_PRESET_MODE,
  137. )
  138. def test_icon(self):
  139. """Test that the icon is as expected."""
  140. self.dps[ALARM_HIGH_DPS] = False
  141. self.dps[ALARM_LOW_DPS] = False
  142. self.dps[ALARM_TIME_DPS] = False
  143. self.dps[SWITCH_DPS] = True
  144. self.assertEqual(self.subject.icon, "mdi:thermometer")
  145. self.dps[SWITCH_DPS] = False
  146. self.assertEqual(self.subject.icon, "mdi:thermometer-off")
  147. self.dps[ALARM_HIGH_DPS] = True
  148. self.assertEqual(self.subject.icon, "mdi:thermometer-alert")
  149. self.dps[SWITCH_DPS] = True
  150. self.assertEqual(self.subject.icon, "mdi:thermometer-alert")
  151. self.dps[ALARM_HIGH_DPS] = False
  152. self.dps[ALARM_LOW_DPS] = True
  153. self.assertEqual(self.subject.icon, "mdi:thermometer-alert")
  154. self.dps[ALARM_LOW_DPS] = False
  155. self.dps[ALARM_TIME_DPS] = True
  156. self.assertEqual(self.subject.icon, "mdi:thermometer-alert")
  157. def test_climate_hvac_modes(self):
  158. self.assertEqual(self.subject.hvac_modes, [])
  159. def test_preset_mode(self):
  160. self.dps[PRESET_DPS] = "on"
  161. self.assertEqual(self.subject.preset_mode, "On")
  162. self.dps[PRESET_DPS] = "pause"
  163. self.assertEqual(self.subject.preset_mode, "Pause")
  164. self.dps[PRESET_DPS] = "off"
  165. self.assertEqual(self.subject.preset_mode, "Off")
  166. self.dps[PRESET_DPS] = None
  167. self.assertEqual(self.subject.preset_mode, None)
  168. def test_preset_modes(self):
  169. self.assertCountEqual(
  170. self.subject.preset_modes,
  171. ["On", "Pause", "Off"],
  172. )
  173. async def test_set_preset_to_on(self):
  174. async with assert_device_properties_set(
  175. self.subject._device,
  176. {
  177. PRESET_DPS: "on",
  178. },
  179. ):
  180. await self.subject.async_set_preset_mode("On")
  181. self.subject._device.anticipate_property_value.assert_not_called()
  182. async def test_set_preset_to_pause(self):
  183. async with assert_device_properties_set(
  184. self.subject._device,
  185. {
  186. PRESET_DPS: "pause",
  187. },
  188. ):
  189. await self.subject.async_set_preset_mode("Pause")
  190. self.subject._device.anticipate_property_value.assert_not_called()
  191. async def test_set_preset_to_off(self):
  192. async with assert_device_properties_set(
  193. self.subject._device,
  194. {
  195. PRESET_DPS: "off",
  196. },
  197. ):
  198. await self.subject.async_set_preset_mode("Off")
  199. self.subject._device.anticipate_property_value.assert_not_called()
  200. def test_current_temperature(self):
  201. self.dps[CURRENTTEMP_DPS] = 289
  202. self.assertEqual(self.subject.current_temperature, 28.9)
  203. def test_temperature_unit(self):
  204. self.dps[UNIT_DPS] = "F"
  205. self.assertEqual(self.subject.temperature_unit, TEMP_FAHRENHEIT)
  206. self.dps[UNIT_DPS] = "C"
  207. self.assertEqual(self.subject.temperature_unit, TEMP_CELSIUS)
  208. def test_minimum_target_temperature(self):
  209. self.dps[UNIT_DPS] = "C"
  210. self.assertEqual(self.subject.min_temp, 0.0)
  211. self.dps[UNIT_DPS] = "F"
  212. self.assertEqual(self.subject.min_temp, 32.0)
  213. def test_maximum_target_temperature(self):
  214. self.dps[UNIT_DPS] = "C"
  215. self.assertEqual(self.subject.max_temp, 45.0)
  216. self.dps[UNIT_DPS] = "F"
  217. self.assertEqual(self.subject.max_temp, 113.0)
  218. def test_temperature_range(self):
  219. self.dps[TEMPHIGH_DPS] = 301
  220. self.dps[TEMPLOW_DPS] = 255
  221. self.assertEqual(self.subject.target_temperature_high, 30.1)
  222. self.assertEqual(self.subject.target_temperature_low, 25.5)
  223. async def test_set_temperature_range(self):
  224. async with assert_device_properties_set(
  225. self.subject._device,
  226. {
  227. TEMPHIGH_DPS: 322,
  228. TEMPLOW_DPS: 266,
  229. },
  230. ):
  231. await self.subject.async_set_temperature(
  232. target_temp_high=32.2, target_temp_low=26.6
  233. )
  234. async def test_set_target_temperature_fails_outside_valid_range(self):
  235. self.dps[UNIT_DPS] = "C"
  236. with self.assertRaisesRegex(
  237. ValueError, "target_temp_low \\(-0.1\\) must be between 0.0 and 45.0"
  238. ):
  239. await self.subject.async_set_temperature(
  240. target_temp_high=32.2, target_temp_low=-0.1
  241. )
  242. self.dps[UNIT_DPS] = "F"
  243. with self.assertRaisesRegex(
  244. ValueError, "target_temp_high \\(113.1\\) must be between 32.0 and 113.0"
  245. ):
  246. await self.subject.async_set_temperature(
  247. target_temp_low=70.0, target_temp_high=113.1
  248. )
  249. def test_hvac_action(self):
  250. self.dps[SWITCH_DPS] = False
  251. self.assertEqual(self.subject.hvac_action, CURRENT_HVAC_IDLE)
  252. self.dps[SWITCH_DPS] = True
  253. self.assertEqual(self.subject.hvac_action, CURRENT_HVAC_HEAT)
  254. def test_extra_state_attributes(self):
  255. self.dps[ERROR_DPS] = 1
  256. self.dps[CALIBRATE_DPS] = 1
  257. self.dps[TIME_THRES_DPS] = 5
  258. self.dps[HIGH_THRES_DPS] = 400
  259. self.dps[LOW_THRES_DPS] = 300
  260. self.dps[ALARM_HIGH_DPS] = True
  261. self.dps[ALARM_LOW_DPS] = False
  262. self.dps[ALARM_TIME_DPS] = True
  263. self.dps[TEMPF_DPS] = 999
  264. self.dps[UNKNOWN117_DPS] = True
  265. self.dps[UNKNOWN118_DPS] = False
  266. self.dps[UNKNOWN119_DPS] = True
  267. self.dps[UNKNOWN120_DPS] = False
  268. self.assertDictEqual(
  269. self.subject.extra_state_attributes,
  270. {
  271. "error": 1,
  272. "temperature_calibration_offset": 0.1,
  273. "heat_time_alarm_threshold_hours": 5,
  274. "high_temp_alarm_threshold": 40.0,
  275. "low_temp_alarm_threshold": 30.0,
  276. "high_temp_alarm": True,
  277. "low_temp_alarm": False,
  278. "heat_time_alarm": True,
  279. "current_temperature_f": 99.9,
  280. "unknown_117": True,
  281. "unknown_118": False,
  282. "unknown_119": True,
  283. "unknown_120": False,
  284. },
  285. )