4
0

test_climate.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. """Tests for the light entity."""
  2. from pytest_homeassistant_custom_component.common import MockConfigEntry
  3. from unittest import IsolatedAsyncioTestCase
  4. from unittest.mock import AsyncMock, Mock, patch
  5. from homeassistant.components.climate.const import (
  6. ATTR_HVAC_MODE,
  7. ATTR_PRESET_MODE,
  8. HVAC_MODE_HEAT,
  9. HVAC_MODE_OFF,
  10. SUPPORT_PRESET_MODE,
  11. SUPPORT_TARGET_TEMPERATURE,
  12. )
  13. from homeassistant.const import ATTR_TEMPERATURE, STATE_UNAVAILABLE
  14. from custom_components.tuya_local.const import (
  15. CONF_CLIMATE,
  16. CONF_DEVICE_ID,
  17. CONF_TYPE,
  18. CONF_TYPE_AUTO,
  19. CONF_TYPE_GPPH_HEATER,
  20. CONF_TYPE_GSH_HEATER,
  21. DOMAIN,
  22. )
  23. from custom_components.tuya_local.heater.climate import GoldairHeater
  24. from custom_components.tuya_local.helpers.device_config import config_for_legacy_use
  25. from custom_components.tuya_local.generic.climate import TuyaLocalClimate
  26. from custom_components.tuya_local.climate import async_setup_entry
  27. from .const import GSH_HEATER_PAYLOAD
  28. from .helpers import assert_device_properties_set
  29. GSH_HVACMODE_DPS = "1"
  30. GSH_TEMPERATURE_DPS = "2"
  31. GSH_CURRENTTEMP_DPS = "3"
  32. GSH_PRESET_DPS = "4"
  33. GSH_ERROR_DPS = "12"
  34. async def test_init_entry(hass):
  35. """Test the initialisation."""
  36. entry = MockConfigEntry(
  37. domain=DOMAIN,
  38. data={CONF_TYPE: CONF_TYPE_AUTO, CONF_DEVICE_ID: "dummy"},
  39. )
  40. # although async, the async_add_entities function passed to
  41. # async_setup_entry is called truly asynchronously. If we use
  42. # AsyncMock, it expects us to await the result.
  43. m_add_entities = Mock()
  44. m_device = AsyncMock()
  45. m_device.async_inferred_type = AsyncMock(return_value=CONF_TYPE_GPPH_HEATER)
  46. hass.data[DOMAIN] = {}
  47. hass.data[DOMAIN]["dummy"] = {}
  48. hass.data[DOMAIN]["dummy"]["device"] = m_device
  49. await async_setup_entry(hass, entry, m_add_entities)
  50. assert type(hass.data[DOMAIN]["dummy"][CONF_CLIMATE]) == GoldairHeater
  51. m_add_entities.assert_called_once()
  52. class TestTuyaLocalClimate(IsolatedAsyncioTestCase):
  53. def setUp(self):
  54. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  55. self.addCleanup(device_patcher.stop)
  56. self.mock_device = device_patcher.start()
  57. gsh_heater_config = config_for_legacy_use(CONF_TYPE_GSH_HEATER)
  58. climate = gsh_heater_config.primary_entity
  59. self.climate_name = climate.name
  60. self.subject = TuyaLocalClimate(self.mock_device(), climate)
  61. self.dps = GSH_HEATER_PAYLOAD.copy()
  62. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  63. def test_supported_features(self):
  64. self.assertEqual(
  65. self.subject.supported_features,
  66. SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE,
  67. )
  68. def test_should_poll(self):
  69. self.assertTrue(self.subject.should_poll)
  70. def test_name_returns_device_name(self):
  71. self.assertEqual(self.subject.name, self.subject._device.name)
  72. def test_unique_id_returns_device_unique_id(self):
  73. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  74. def test_device_info_returns_device_info_from_device(self):
  75. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  76. def test_icon(self):
  77. self.dps[GSH_HVACMODE_DPS] = True
  78. self.assertEqual(self.subject.icon, "mdi:radiator")
  79. self.dps[GSH_HVACMODE_DPS] = False
  80. self.assertEqual(self.subject.icon, "mdi:radiator-disabled")
  81. def test_temperature_unit_returns_device_temperature_unit(self):
  82. self.assertEqual(
  83. self.subject.temperature_unit, self.subject._device.temperature_unit
  84. )
  85. def test_target_temperature(self):
  86. self.dps[GSH_TEMPERATURE_DPS] = 25
  87. self.assertEqual(self.subject.target_temperature, 25)
  88. def test_target_temperature_step(self):
  89. self.assertEqual(self.subject.target_temperature_step, 1)
  90. def test_minimum_target_temperature(self):
  91. self.assertEqual(self.subject.min_temp, 5)
  92. def test_maximum_target_temperature(self):
  93. self.assertEqual(self.subject.max_temp, 35)
  94. async def test_legacy_set_temperature_with_temperature(self):
  95. async with assert_device_properties_set(
  96. self.subject._device, {GSH_TEMPERATURE_DPS: 24}
  97. ):
  98. await self.subject.async_set_temperature(temperature=24)
  99. async def test_legacy_set_temperature_with_preset_mode(self):
  100. async with assert_device_properties_set(
  101. self.subject._device, {GSH_PRESET_DPS: "low"}
  102. ):
  103. await self.subject.async_set_temperature(preset_mode="Low")
  104. async def test_legacy_set_temperature_with_both_properties(self):
  105. async with assert_device_properties_set(
  106. self.subject._device, {GSH_TEMPERATURE_DPS: 26, GSH_PRESET_DPS: "high"}
  107. ):
  108. await self.subject.async_set_temperature(temperature=26, preset_mode="High")
  109. async def test_legacy_set_temperature_with_no_valid_properties(self):
  110. await self.subject.async_set_temperature(something="else")
  111. self.subject._device.async_set_property.assert_not_called
  112. async def test_set_target_temperature_succeeds_within_valid_range(self):
  113. async with assert_device_properties_set(
  114. self.subject._device,
  115. {GSH_TEMPERATURE_DPS: 25},
  116. ):
  117. await self.subject.async_set_target_temperature(25)
  118. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  119. async with assert_device_properties_set(
  120. self.subject._device, {GSH_TEMPERATURE_DPS: 23}
  121. ):
  122. await self.subject.async_set_target_temperature(22.6)
  123. async def test_set_target_temperature_fails_outside_valid_range(self):
  124. with self.assertRaisesRegex(
  125. ValueError, "Target temperature \\(4\\) must be between 5 and 35"
  126. ):
  127. await self.subject.async_set_target_temperature(4)
  128. with self.assertRaisesRegex(
  129. ValueError, "Target temperature \\(36\\) must be between 5 and 35"
  130. ):
  131. await self.subject.async_set_target_temperature(36)
  132. def test_current_temperature(self):
  133. self.dps[GSH_CURRENTTEMP_DPS] = 25
  134. self.assertEqual(self.subject.current_temperature, 25)
  135. def test_hvac_mode(self):
  136. self.dps[GSH_HVACMODE_DPS] = True
  137. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  138. self.dps[GSH_HVACMODE_DPS] = False
  139. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  140. self.dps[GSH_HVACMODE_DPS] = None
  141. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  142. def test_hvac_modes(self):
  143. self.assertCountEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_HEAT])
  144. async def test_turn_on(self):
  145. async with assert_device_properties_set(
  146. self.subject._device, {GSH_HVACMODE_DPS: True}
  147. ):
  148. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  149. async def test_turn_off(self):
  150. async with assert_device_properties_set(
  151. self.subject._device, {GSH_HVACMODE_DPS: False}
  152. ):
  153. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  154. def test_preset_mode(self):
  155. self.dps[GSH_PRESET_DPS] = "low"
  156. self.assertEqual(self.subject.preset_mode, "Low")
  157. self.dps[GSH_PRESET_DPS] = "high"
  158. self.assertEqual(self.subject.preset_mode, "High")
  159. self.dps[GSH_PRESET_DPS] = "af"
  160. self.assertEqual(self.subject.preset_mode, "Anti-freeze")
  161. self.dps[GSH_PRESET_DPS] = None
  162. self.assertIs(self.subject.preset_mode, None)
  163. def test_preset_modes(self):
  164. self.assertCountEqual(self.subject.preset_modes, ["Low", "High", "Anti-freeze"])
  165. async def test_set_preset_mode_to_low(self):
  166. async with assert_device_properties_set(
  167. self.subject._device,
  168. {GSH_PRESET_DPS: "low"},
  169. ):
  170. await self.subject.async_set_preset_mode("Low")
  171. async def test_set_preset_mode_to_high(self):
  172. async with assert_device_properties_set(
  173. self.subject._device,
  174. {GSH_PRESET_DPS: "high"},
  175. ):
  176. await self.subject.async_set_preset_mode("High")
  177. async def test_set_preset_mode_to_af(self):
  178. async with assert_device_properties_set(
  179. self.subject._device,
  180. {GSH_PRESET_DPS: "af"},
  181. ):
  182. await self.subject.async_set_preset_mode("Anti-freeze")
  183. def test_error_state(self):
  184. # There are currently no known error states; update this as they're discovered
  185. self.dps[GSH_ERROR_DPS] = "something"
  186. self.assertEqual(self.subject.device_state_attributes, {"error": "something"})
  187. async def test_update(self):
  188. result = AsyncMock()
  189. self.subject._device.async_refresh.return_value = result()
  190. await self.subject.async_update()
  191. self.subject._device.async_refresh.assert_called_once()
  192. result.assert_awaited()