test_binary_sensor.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """Tests for the binary_sensor entity."""
  2. from pytest_homeassistant_custom_component.common import MockConfigEntry
  3. import pytest
  4. from unittest.mock import AsyncMock, Mock
  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.binary_sensor import (
  12. async_setup_entry,
  13. TuyaLocalBinarySensor,
  14. )
  15. @pytest.mark.asyncio
  16. async def test_init_entry(hass):
  17. """Test the initialisation."""
  18. entry = MockConfigEntry(
  19. domain=DOMAIN,
  20. data={
  21. CONF_TYPE: "goldair_dehumidifier",
  22. CONF_DEVICE_ID: "dummy",
  23. CONF_PROTOCOL_VERSION: "auto",
  24. },
  25. )
  26. m_add_entities = Mock()
  27. m_device = AsyncMock()
  28. hass.data[DOMAIN] = {
  29. "dummy": {"device": m_device},
  30. }
  31. await async_setup_entry(hass, entry, m_add_entities)
  32. assert (
  33. type(hass.data[DOMAIN]["dummy"]["binary_sensor_tank"]) == TuyaLocalBinarySensor
  34. )
  35. m_add_entities.assert_called_once()
  36. @pytest.mark.asyncio
  37. async def test_init_entry_fails_if_device_has_no_binary_sensor(hass):
  38. """Test initialisation when device has no matching entity"""
  39. entry = MockConfigEntry(
  40. domain=DOMAIN,
  41. data={
  42. CONF_TYPE: "mirabella_genio_usb",
  43. CONF_DEVICE_ID: "dummy",
  44. CONF_PROTOCOL_VERSION: "auto",
  45. },
  46. )
  47. m_add_entities = Mock()
  48. m_device = AsyncMock()
  49. hass.data[DOMAIN] = {
  50. "dummy": {"device": m_device},
  51. }
  52. try:
  53. await async_setup_entry(hass, entry, m_add_entities)
  54. assert False
  55. except ValueError:
  56. pass
  57. m_add_entities.assert_not_called()
  58. @pytest.mark.asyncio
  59. async def test_init_entry_fails_if_config_is_missing(hass):
  60. """Test initialisation when device has no matching entity"""
  61. entry = MockConfigEntry(
  62. domain=DOMAIN,
  63. data={
  64. CONF_TYPE: "non_existing",
  65. CONF_DEVICE_ID: "dummy",
  66. CONF_PROTOCOL_VERSION: "auto",
  67. },
  68. )
  69. m_add_entities = Mock()
  70. m_device = AsyncMock()
  71. hass.data[DOMAIN] = {
  72. "dummy": {"device": m_device},
  73. }
  74. try:
  75. await async_setup_entry(hass, entry, m_add_entities)
  76. assert False
  77. except ValueError:
  78. pass
  79. m_add_entities.assert_not_called()