test_inkbird_itc306a_thermostat.py 10.0 KB

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