4
0

test_text.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """Tests for the text 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.text import TuyaLocalText, async_setup_entry
  12. @pytest.mark.asyncio
  13. async def test_init_entry(hass):
  14. """Test the initialisation."""
  15. entry = MockConfigEntry(
  16. domain=DOMAIN,
  17. data={
  18. CONF_TYPE: "rgbcw_lightbulb",
  19. CONF_DEVICE_ID: "dummy",
  20. CONF_PROTOCOL_VERSION: "auto",
  21. },
  22. )
  23. m_add_entities = Mock()
  24. m_device = AsyncMock()
  25. m_device.get_property = Mock(return_value=None)
  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"]["text_scene"]) is TuyaLocalText
  31. m_add_entities.assert_called_once()
  32. @pytest.mark.asyncio
  33. async def test_init_entry_fails_if_device_has_no_text(hass):
  34. """Test initialisation when device has no matching entity"""
  35. entry = MockConfigEntry(
  36. domain=DOMAIN,
  37. data={
  38. CONF_TYPE: "simple_switch",
  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()