test_inkbird_itc306a_thermostat.py 10 KB

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