conftest.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import pytest
  2. from unittest.mock import AsyncMock, patch
  3. from pytest_homeassistant_custom_component.common import MockConfigEntry
  4. from custom_components.zlock_slots.const import DOMAIN, CONF_LOCK_ENTITY_IDS
  5. pytest_plugins = ["pytest_homeassistant_custom_component"]
  6. LOCK_ENTITY_ID = "lock.front_door"
  7. ALARM_TYPE_ENTITY = "sensor.front_door_alarm_type"
  8. ALARM_LEVEL_ENTITY = "sensor.front_door_alarm_level"
  9. DOOR_SENSOR_ENTITY = "binary_sensor.front_door_contact"
  10. class FakeUserData:
  11. def __init__(self, user_id: int, active: bool = True, user_type=0,
  12. credential_rule=None, expiring_timeout_minutes=None):
  13. self.user_id = user_id
  14. self.active = active
  15. self.user_type = user_type
  16. self.credential_rule = credential_rule
  17. self.expiring_timeout_minutes = expiring_timeout_minutes
  18. class FakeCredentialData:
  19. def __init__(self, user_id: int, slot: int, type=1, data=None):
  20. self.user_id = user_id
  21. self.slot = slot
  22. self.type = type
  23. self.data = data
  24. class FakeAccessControl:
  25. def __init__(self, users=None, credentials=None):
  26. self._users = list(users or [])
  27. # user_id -> PIN code string
  28. self._credentials: dict[int, str] = dict(credentials or {})
  29. self.set_user = AsyncMock(side_effect=self._set_user)
  30. self.set_credential = AsyncMock(side_effect=self._set_credential)
  31. self.delete_user = AsyncMock(side_effect=self._delete_user)
  32. self.add_user = AsyncMock(side_effect=self._add_user)
  33. async def get_users_cached(self):
  34. return list(self._users)
  35. async def get_users(self):
  36. return list(self._users)
  37. async def get_credentials(self, user_id):
  38. if user_id in self._credentials:
  39. return [FakeCredentialData(user_id, user_id, data=self._credentials[user_id])]
  40. return []
  41. async def _set_user(self, user_id, options):
  42. self._users = [u for u in self._users if u.user_id != user_id]
  43. self._users.append(FakeUserData(
  44. user_id,
  45. active=options.active,
  46. user_type=options.user_type,
  47. credential_rule=options.credential_rule,
  48. expiring_timeout_minutes=options.expiring_timeout_minutes,
  49. ))
  50. async def _set_credential(self, user_id, credential_type, credential_slot, data):
  51. self._credentials[user_id] = data
  52. async def _delete_user(self, user_id):
  53. self._users = [u for u in self._users if u.user_id != user_id]
  54. self._credentials.pop(user_id, None)
  55. async def _add_user(self, user_id, options, credential=None):
  56. await self._set_user(user_id, options)
  57. if credential is not None:
  58. self._credentials[user_id] = credential.data
  59. else:
  60. self._credentials.pop(user_id, None)
  61. class FakeNode:
  62. def __init__(self, users=None, credentials=None):
  63. self.access_control = FakeAccessControl(users, credentials)
  64. @pytest.fixture(autouse=True)
  65. def auto_enable_custom_integrations(enable_custom_integrations):
  66. yield
  67. @pytest.fixture(autouse=True)
  68. def bypass_manifest_dependencies():
  69. """zwave_js/frontend/lovelace can't be set up in the test harness."""
  70. with patch(
  71. "homeassistant.setup._async_process_dependencies", return_value=[]
  72. ):
  73. yield
  74. @pytest.fixture
  75. def mock_node():
  76. return FakeNode(users=[
  77. FakeUserData(1, active=True),
  78. FakeUserData(2, active=False),
  79. ])
  80. @pytest.fixture
  81. def config_entry():
  82. return MockConfigEntry(
  83. domain=DOMAIN,
  84. data={CONF_LOCK_ENTITY_IDS: [LOCK_ENTITY_ID]},
  85. options={},
  86. entry_id="test_entry",
  87. )