test_inkbird_itc306a_thermostat.py 10 KB

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