test_eurom_600_heater.py 5.9 KB

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