test_inkbird_itc306a_thermostat.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. from homeassistant.components.binary_sensor import BinarySensorDeviceClass
  2. from homeassistant.components.climate.const import (
  3. ClimateEntityFeature,
  4. HVACAction,
  5. )
  6. from homeassistant.components.number.const import NumberDeviceClass
  7. from homeassistant.const import UnitOfTime, UnitOfTemperature
  8. from ..const import INKBIRD_ITC306A_THERMOSTAT_PAYLOAD
  9. from ..helpers import assert_device_properties_set
  10. from ..mixins.binary_sensor import MultiBinarySensorTests
  11. from ..mixins.number import MultiNumberTests
  12. from ..mixins.select import BasicSelectTests
  13. from .base_device_tests import TuyaDeviceTestCase
  14. ERROR_DPS = "12"
  15. UNIT_DPS = "101"
  16. CALIBRATE_DPS = "102"
  17. PRESET_DPS = "103"
  18. CURRENTTEMP_DPS = "104"
  19. TEMPLOW_DPS = "106"
  20. TIME_THRES_DPS = "108"
  21. HIGH_THRES_DPS = "109"
  22. LOW_THRES_DPS = "110"
  23. ALARM_HIGH_DPS = "111"
  24. ALARM_LOW_DPS = "112"
  25. ALARM_DIFF_DPS = "113"
  26. TEMPHIGH_DPS = "114"
  27. SWITCH_DPS = "115"
  28. TEMPF_DPS = "116"
  29. UNKNOWN117_DPS = "117"
  30. UNKNOWN118_DPS = "118"
  31. UNKNOWN119_DPS = "119"
  32. ALARM_TIME_DPS = "120"
  33. class TestInkbirdThermostat(
  34. BasicSelectTests,
  35. MultiBinarySensorTests,
  36. MultiNumberTests,
  37. TuyaDeviceTestCase,
  38. ):
  39. __test__ = True
  40. def setUp(self):
  41. self.setUpForConfig(
  42. "inkbird_itc306a_thermostat.yaml", INKBIRD_ITC306A_THERMOSTAT_PAYLOAD
  43. )
  44. self.subject = self.entities.get("climate")
  45. self.setUpBasicSelect(
  46. UNIT_DPS,
  47. self.entities.get("select_temperature_unit"),
  48. {
  49. "C": "Celsius",
  50. "F": "Fahrenheit",
  51. },
  52. )
  53. self.setUpMultiBinarySensors(
  54. [
  55. {
  56. "name": "binary_sensor_high_temperature",
  57. "dps": ALARM_HIGH_DPS,
  58. "device_class": BinarySensorDeviceClass.HEAT,
  59. },
  60. {
  61. "name": "binary_sensor_low_temperature",
  62. "dps": ALARM_LOW_DPS,
  63. "device_class": BinarySensorDeviceClass.COLD,
  64. },
  65. {
  66. "name": "binary_sensor_continuous_heat",
  67. "dps": ALARM_TIME_DPS,
  68. "device_class": BinarySensorDeviceClass.PROBLEM,
  69. },
  70. {
  71. "name": "binary_sensor_unbalanced",
  72. "dps": ALARM_DIFF_DPS,
  73. "device_class": BinarySensorDeviceClass.PROBLEM,
  74. },
  75. {
  76. "name": "binary_sensor_error",
  77. "dps": ERROR_DPS,
  78. "device_class": BinarySensorDeviceClass.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. "unit": UnitOfTime.HOURS,
  98. },
  99. {
  100. "name": "number_high_temperature_limit",
  101. "dps": HIGH_THRES_DPS,
  102. "device_class": NumberDeviceClass.TEMPERATURE,
  103. "scale": 10,
  104. "step": 0.1,
  105. "min": -40,
  106. "max": 100,
  107. "unit": UnitOfTemperature.CELSIUS,
  108. },
  109. {
  110. "name": "number_low_temperature_limit",
  111. "dps": LOW_THRES_DPS,
  112. "device_class": NumberDeviceClass.TEMPERATURE,
  113. "scale": 10,
  114. "step": 0.1,
  115. "min": -40,
  116. "max": 100,
  117. "unit": UnitOfTemperature.CELSIUS,
  118. },
  119. ]
  120. )
  121. self.mark_secondary(
  122. [
  123. "select_temperature_unit",
  124. "binary_sensor_high_temperature",
  125. "binary_sensor_low_temperature",
  126. "binary_sensor_continuous_heat",
  127. "binary_sensor_unbalanced",
  128. "binary_sensor_error",
  129. "number_calibration_offset",
  130. "number_continuous_heat_hours",
  131. "number_high_temperature_limit",
  132. "number_low_temperature_limit",
  133. ]
  134. )
  135. def test_supported_features(self):
  136. self.assertEqual(
  137. self.subject.supported_features,
  138. (
  139. ClimateEntityFeature.TARGET_TEMPERATURE_RANGE
  140. | ClimateEntityFeature.PRESET_MODE
  141. ),
  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, UnitOfTemperature.FAHRENHEIT)
  218. self.dps[UNIT_DPS] = "C"
  219. self.assertEqual(self.subject.temperature_unit, UnitOfTemperature.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, HVACAction.IDLE)
  264. self.dps[SWITCH_DPS] = True
  265. self.assertEqual(self.subject.hvac_action, HVACAction.HEATING)
  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. )