test_sensor.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """Tests for the sensor entity."""
  2. from unittest.mock import AsyncMock, Mock
  3. import pytest
  4. from pytest_homeassistant_custom_component.common import MockConfigEntry
  5. from custom_components.tuya_local.const import (
  6. CONF_DEVICE_ID,
  7. CONF_PROTOCOL_VERSION,
  8. CONF_TYPE,
  9. DOMAIN,
  10. )
  11. from custom_components.tuya_local.helpers.device_config import TuyaEntityConfig
  12. from custom_components.tuya_local.sensor import TuyaLocalSensor, async_setup_entry
  13. @pytest.mark.asyncio
  14. async def test_init_entry(hass):
  15. """Test the initialisation."""
  16. entry = MockConfigEntry(
  17. domain=DOMAIN,
  18. data={
  19. CONF_TYPE: "goldair_dehumidifier",
  20. CONF_DEVICE_ID: "dummy",
  21. CONF_PROTOCOL_VERSION: "auto",
  22. },
  23. )
  24. m_add_entities = Mock()
  25. m_device = AsyncMock()
  26. hass.data[DOMAIN] = {
  27. "dummy": {"device": m_device},
  28. }
  29. await async_setup_entry(hass, entry, m_add_entities)
  30. assert type(hass.data[DOMAIN]["dummy"]["sensor_temperature"]) is TuyaLocalSensor
  31. m_add_entities.assert_called_once()
  32. @pytest.mark.asyncio
  33. async def test_init_entry_fails_if_device_has_no_sensor(hass):
  34. """Test initialisation when device has no matching entity"""
  35. entry = MockConfigEntry(
  36. domain=DOMAIN,
  37. data={
  38. CONF_TYPE: "mirabella_genio_usb",
  39. CONF_DEVICE_ID: "dummy",
  40. CONF_PROTOCOL_VERSION: "auto",
  41. },
  42. )
  43. m_add_entities = Mock()
  44. m_device = AsyncMock()
  45. hass.data[DOMAIN] = {
  46. "dummy": {"device": m_device},
  47. }
  48. try:
  49. await async_setup_entry(hass, entry, m_add_entities)
  50. assert False
  51. except ValueError:
  52. pass
  53. m_add_entities.assert_not_called()
  54. @pytest.mark.asyncio
  55. async def test_init_entry_fails_if_config_is_missing(hass):
  56. """Test initialisation when device has no matching entity"""
  57. entry = MockConfigEntry(
  58. domain=DOMAIN,
  59. data={
  60. CONF_TYPE: "non_existing",
  61. CONF_DEVICE_ID: "dummy",
  62. CONF_PROTOCOL_VERSION: "auto",
  63. },
  64. )
  65. m_add_entities = Mock()
  66. m_device = AsyncMock()
  67. hass.data[DOMAIN] = {
  68. "dummy": {"device": m_device},
  69. }
  70. try:
  71. await async_setup_entry(hass, entry, m_add_entities)
  72. assert False
  73. except ValueError:
  74. pass
  75. m_add_entities.assert_not_called()
  76. def test_sensor_suggested_display_precision():
  77. mock_device = Mock()
  78. config = TuyaEntityConfig(
  79. mock_device,
  80. {
  81. "entity": "sensor",
  82. "dps": [
  83. {
  84. "id": 1,
  85. "name": "sensor",
  86. "type": "integer",
  87. "precision": 1,
  88. }
  89. ],
  90. },
  91. )
  92. sensor = TuyaLocalSensor(mock_device, config)
  93. assert sensor.suggested_display_precision == 1
  94. config = TuyaEntityConfig(
  95. mock_device,
  96. {
  97. "entity": "sensor",
  98. "dps": [{"id": 1, "name": "sensor", "type": "integer"}],
  99. },
  100. )
  101. sensor = TuyaLocalSensor(mock_device, config)
  102. assert sensor.suggested_display_precision is None