test_inkbird_thermostat.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. from unittest import IsolatedAsyncioTestCase, skip
  2. from unittest.mock import AsyncMock, patch
  3. from homeassistant.components.climate.const import (
  4. SUPPORT_PRESET_MODE,
  5. SUPPORT_TARGET_TEMPERATURE_RANGE,
  6. )
  7. from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT
  8. from custom_components.tuya_local.generic.climate import TuyaLocalClimate
  9. from custom_components.tuya_local.helpers.device_config import TuyaDeviceConfig
  10. from ..const import INKBIRD_THERMOSTAT_PAYLOAD
  11. from ..helpers import assert_device_properties_set
  12. ERROR_DPS = "12"
  13. UNIT_DPS = "101"
  14. CALIBRATE_DPS = "102"
  15. PRESET_DPS = "103"
  16. CURRENTTEMP_DPS = "104"
  17. TEMPLOW_DPS = "106"
  18. TIME_THRES_DPS = "108"
  19. HIGH_THRES_DPS = "109"
  20. LOW_THRES_DPS = "110"
  21. ALARM_HIGH_DPS = "111"
  22. ALARM_LOW_DPS = "112"
  23. ALARM_TIME_DPS = "113"
  24. TEMPHIGH_DPS = "114"
  25. SWITCH_DPS = "115"
  26. TEMPF_DPS = "116"
  27. UNKNOWN117_DPS = "117"
  28. UNKNOWN118_DPS = "118"
  29. UNKNOWN119_DPS = "119"
  30. UNKNOWN120_DPS = "120"
  31. class TestInkbirdThermostat(IsolatedAsyncioTestCase):
  32. def setUp(self):
  33. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  34. self.addCleanup(device_patcher.stop)
  35. self.mock_device = device_patcher.start()
  36. cfg = TuyaDeviceConfig("inkbird_thermostat.yaml")
  37. entities = {}
  38. entities[cfg.primary_entity.entity] = cfg.primary_entity
  39. for e in cfg.secondary_entities():
  40. entities[e.entity] = e
  41. self.climate_name = (
  42. "missing" if "climate" not in entities else entities["climate"].name
  43. )
  44. self.subject = TuyaLocalClimate(self.mock_device(), entities.get("climate"))
  45. self.dps = INKBIRD_THERMOSTAT_PAYLOAD.copy()
  46. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  47. def test_supported_features(self):
  48. self.assertEqual(
  49. self.subject.supported_features,
  50. SUPPORT_TARGET_TEMPERATURE_RANGE | SUPPORT_PRESET_MODE,
  51. )
  52. def test_shouldPoll(self):
  53. self.assertTrue(self.subject.should_poll)
  54. def test_name_returns_device_name(self):
  55. self.assertEqual(self.subject.name, self.subject._device.name)
  56. def test_friendly_name_returns_config_name(self):
  57. self.assertEqual(self.subject.friendly_name, self.climate_name)
  58. def test_unique_id_returns_device_unique_id(self):
  59. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  60. def test_device_info_returns_device_info_from_device(self):
  61. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  62. @skip("Icon customisation not supported yet")
  63. def test_icon(self):
  64. """Test that the icon is as expected."""
  65. self.dps[ALARM_HIGH_DPS] = False
  66. self.dps[ALARM_LOW_DPS] = False
  67. self.dps[ALARM_TIME_DPS] = False
  68. self.dps[SWITCH_DPS] = True
  69. self.assertEqual(self.subject.icon, "mdi:thermometer")
  70. self.dps[SWITCH_DPS] = False
  71. self.assertEqual(self.subject.icon, "mdi:thermometer-off")
  72. self.dps[ALARM_HIGH_DPS] = True
  73. self.assertEqual(self.subject.icon, "mdi:thermometer-alert")
  74. self.dps[SWITCH_DPS] = True
  75. self.assertEqual(self.subject.icon, "mdi:thermometer-alert")
  76. self.dps[ALARM_HIGH_DPS] = False
  77. self.dps[ALARM_LOW_DPS] = True
  78. self.assertEqual(self.subject.icon, "mdi:thermometer-alert")
  79. self.dps[ALARM_LOW_DPS] = False
  80. self.dps[ALARM_TIME_DPS] = True
  81. self.assertEqual(self.subject.icon, "mdi:thermometer-alert")
  82. def test_climate_hvac_modes(self):
  83. self.assertEqual(self.subject.hvac_modes, [])
  84. def test_preset_mode(self):
  85. self.dps[PRESET_DPS] = "on"
  86. self.assertEqual(self.subject.preset_mode, "On")
  87. self.dps[PRESET_DPS] = "pause"
  88. self.assertEqual(self.subject.preset_mode, "Pause")
  89. self.dps[PRESET_DPS] = "off"
  90. self.assertEqual(self.subject.preset_mode, "Off")
  91. self.dps[PRESET_DPS] = None
  92. self.assertEqual(self.subject.preset_mode, None)
  93. def test_preset_modes(self):
  94. self.assertCountEqual(
  95. self.subject.preset_modes,
  96. {"On", "Pause", "Off"},
  97. )
  98. async def test_set_preset_to_on(self):
  99. async with assert_device_properties_set(
  100. self.subject._device,
  101. {
  102. PRESET_DPS: "on",
  103. },
  104. ):
  105. await self.subject.async_set_preset_mode("On")
  106. self.subject._device.anticipate_property_value.assert_not_called()
  107. async def test_set_preset_to_pause(self):
  108. async with assert_device_properties_set(
  109. self.subject._device,
  110. {
  111. PRESET_DPS: "pause",
  112. },
  113. ):
  114. await self.subject.async_set_preset_mode("Pause")
  115. self.subject._device.anticipate_property_value.assert_not_called()
  116. async def test_set_preset_to_off(self):
  117. async with assert_device_properties_set(
  118. self.subject._device,
  119. {
  120. PRESET_DPS: "off",
  121. },
  122. ):
  123. await self.subject.async_set_preset_mode("Off")
  124. self.subject._device.anticipate_property_value.assert_not_called()
  125. def test_current_temperature(self):
  126. self.dps[CURRENTTEMP_DPS] = 289
  127. self.assertEqual(self.subject.current_temperature, 28.9)
  128. def test_temperature_unit(self):
  129. self.dps[UNIT_DPS] = "F"
  130. self.assertEqual(self.subject.temperature_unit, TEMP_FAHRENHEIT)
  131. self.dps[UNIT_DPS] = "C"
  132. self.assertEqual(self.subject.temperature_unit, TEMP_CELSIUS)
  133. def test_temperature_range(self):
  134. self.dps[TEMPHIGH_DPS] = 301
  135. self.dps[TEMPLOW_DPS] = 255
  136. self.assertEqual(self.subject.target_temperature_high, 30.1)
  137. self.assertEqual(self.subject.target_temperature_low, 25.5)
  138. async def test_set_temperature_range(self):
  139. async with assert_device_properties_set(
  140. self.subject._device,
  141. {
  142. TEMPHIGH_DPS: 322,
  143. TEMPLOW_DPS: 266,
  144. },
  145. ):
  146. await self.subject.async_set_temperature(
  147. target_temp_high=32.2, target_temp_low=26.6
  148. )
  149. def test_device_state_attributes(self):
  150. self.dps[ERROR_DPS] = 1
  151. self.dps[CALIBRATE_DPS] = 1
  152. self.dps[TIME_THRES_DPS] = 5
  153. self.dps[HIGH_THRES_DPS] = 400
  154. self.dps[LOW_THRES_DPS] = 300
  155. self.dps[ALARM_HIGH_DPS] = True
  156. self.dps[ALARM_LOW_DPS] = False
  157. self.dps[ALARM_TIME_DPS] = True
  158. self.dps[SWITCH_DPS] = False
  159. self.dps[TEMPF_DPS] = 999
  160. self.dps[UNKNOWN117_DPS] = True
  161. self.dps[UNKNOWN118_DPS] = False
  162. self.dps[UNKNOWN119_DPS] = True
  163. self.dps[UNKNOWN120_DPS] = False
  164. self.assertCountEqual(
  165. self.subject.device_state_attributes,
  166. {
  167. "error": 1,
  168. "temperature_calibration_offset": 0.1,
  169. "heat_time_alarm_threshold_hours": 5,
  170. "high_temp_alarm_threshold": 40.0,
  171. "low_temp_alarm_threshold": 30.0,
  172. "high_temp_alarm": True,
  173. "low_temp_alarm": False,
  174. "heat_time_alarm": True,
  175. "switch_state": False,
  176. "current_temperature_f": 99.9,
  177. "unknown_117": True,
  178. "unknown_118": False,
  179. "unknown_119": True,
  180. "unknown_120": False,
  181. },
  182. )
  183. async def test_update(self):
  184. result = AsyncMock()
  185. self.subject._device.async_refresh.return_value = result()
  186. await self.subject.async_update()
  187. self.subject._device.async_refresh.assert_called_once()
  188. result.assert_awaited()