test_climate.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from unittest import IsolatedAsyncioTestCase
  2. from unittest.mock import AsyncMock, patch
  3. from homeassistant.components.climate.const import (
  4. ATTR_HVAC_MODE,
  5. ATTR_PRESET_MODE,
  6. HVAC_MODE_HEAT,
  7. HVAC_MODE_OFF,
  8. SUPPORT_TARGET_TEMPERATURE,
  9. )
  10. from homeassistant.const import ATTR_TEMPERATURE, STATE_UNAVAILABLE
  11. from custom_components.tuya_local.geco_heater.climate import GoldairGECOHeater
  12. from custom_components.tuya_local.geco_heater.const import (
  13. ATTR_ERROR,
  14. ATTR_TARGET_TEMPERATURE,
  15. HVAC_MODE_TO_DPS_MODE,
  16. PROPERTY_TO_DPS_ID,
  17. )
  18. from ..const import GECO_HEATER_PAYLOAD
  19. from ..helpers import assert_device_properties_set
  20. class TestGoldairGECOHeater(IsolatedAsyncioTestCase):
  21. def setUp(self):
  22. device_patcher = patch("custom_components.tuya_local.device.TuyaLocalDevice")
  23. self.addCleanup(device_patcher.stop)
  24. self.mock_device = device_patcher.start()
  25. self.subject = GoldairGECOHeater(self.mock_device())
  26. self.dps = GECO_HEATER_PAYLOAD.copy()
  27. self.subject._device.get_property.side_effect = lambda id: self.dps[id]
  28. def test_supported_features(self):
  29. self.assertEqual(
  30. self.subject.supported_features, SUPPORT_TARGET_TEMPERATURE,
  31. )
  32. def test_should_poll(self):
  33. self.assertTrue(self.subject.should_poll)
  34. def test_name_returns_device_name(self):
  35. self.assertEqual(self.subject.name, self.subject._device.name)
  36. def test_unique_id_returns_device_unique_id(self):
  37. self.assertEqual(self.subject.unique_id, self.subject._device.unique_id)
  38. def test_device_info_returns_device_info_from_device(self):
  39. self.assertEqual(self.subject.device_info, self.subject._device.device_info)
  40. def test_icon(self):
  41. self.dps[PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]] = True
  42. self.assertEqual(self.subject.icon, "mdi:radiator")
  43. self.dps[PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]] = False
  44. self.assertEqual(self.subject.icon, "mdi:radiator-disabled")
  45. def test_temperature_unit_returns_device_temperature_unit(self):
  46. self.assertEqual(
  47. self.subject.temperature_unit, self.subject._device.temperature_unit
  48. )
  49. def test_target_temperature(self):
  50. self.dps[PROPERTY_TO_DPS_ID[ATTR_TARGET_TEMPERATURE]] = 25
  51. self.assertEqual(self.subject.target_temperature, 25)
  52. def test_target_temperature_step(self):
  53. self.assertEqual(self.subject.target_temperature_step, 1)
  54. def test_minimum_target_temperature(self):
  55. self.assertEqual(self.subject.min_temp, 15)
  56. def test_maximum_target_temperature(self):
  57. self.assertEqual(self.subject.max_temp, 35)
  58. async def test_legacy_set_temperature_method(self):
  59. async with assert_device_properties_set(
  60. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_TARGET_TEMPERATURE]: 25}
  61. ):
  62. await self.subject.async_set_temperature(temperature=25)
  63. async def test_legacy_set_temperature_does_nothing_without_temperature_value(self):
  64. await self.subject.async_set_temperature(something="else")
  65. self.subject._device.async_set_property.assert_not_called()
  66. async def test_set_target_temperature_succeeds_within_valid_range(self):
  67. async with assert_device_properties_set(
  68. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_TARGET_TEMPERATURE]: 25}
  69. ):
  70. await self.subject.async_set_target_temperature(25)
  71. async def test_set_target_temperature_rounds_value_to_closest_integer(self):
  72. async with assert_device_properties_set(
  73. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_TARGET_TEMPERATURE]: 25},
  74. ):
  75. await self.subject.async_set_target_temperature(24.6)
  76. async def test_set_target_temperature_fails_outside_valid_range(self):
  77. with self.assertRaisesRegex(
  78. ValueError, "Target temperature \\(14\\) must be between 15 and 35"
  79. ):
  80. await self.subject.async_set_target_temperature(14)
  81. with self.assertRaisesRegex(
  82. ValueError, "Target temperature \\(36\\) must be between 15 and 35"
  83. ):
  84. await self.subject.async_set_target_temperature(36)
  85. def test_current_temperature(self):
  86. self.dps[PROPERTY_TO_DPS_ID[ATTR_TEMPERATURE]] = 25
  87. self.assertEqual(self.subject.current_temperature, 25)
  88. def test_hvac_mode(self):
  89. self.dps[PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]] = True
  90. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_HEAT)
  91. self.dps[PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]] = False
  92. self.assertEqual(self.subject.hvac_mode, HVAC_MODE_OFF)
  93. self.dps[PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]] = None
  94. self.assertEqual(self.subject.hvac_mode, STATE_UNAVAILABLE)
  95. def test_hvac_modes(self):
  96. self.assertEqual(self.subject.hvac_modes, [HVAC_MODE_OFF, HVAC_MODE_HEAT])
  97. async def test_turn_on(self):
  98. async with assert_device_properties_set(
  99. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]: True}
  100. ):
  101. await self.subject.async_set_hvac_mode(HVAC_MODE_HEAT)
  102. async def test_turn_off(self):
  103. async with assert_device_properties_set(
  104. self.subject._device, {PROPERTY_TO_DPS_ID[ATTR_HVAC_MODE]: False}
  105. ):
  106. await self.subject.async_set_hvac_mode(HVAC_MODE_OFF)
  107. def test_error_state(self):
  108. # There are currently no known error states; update this as they're discovered
  109. self.dps[PROPERTY_TO_DPS_ID[ATTR_ERROR]] = "something"
  110. self.assertEqual(
  111. self.subject.device_state_attributes, {ATTR_ERROR: "something"}
  112. )
  113. async def test_update(self):
  114. result = AsyncMock()
  115. self.subject._device.async_refresh.return_value = result()
  116. await self.subject.async_update()
  117. self.subject._device.async_refresh.assert_called_once()
  118. result.assert_awaited()