conftest.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 get_credentials_cached(self, user_id):
  42. return await self.get_credentials(user_id)
  43. async def _set_user(self, user_id, options):
  44. self._users = [u for u in self._users if u.user_id != user_id]
  45. self._users.append(FakeUserData(
  46. user_id,
  47. active=options.active,
  48. user_type=options.user_type,
  49. credential_rule=options.credential_rule,
  50. expiring_timeout_minutes=options.expiring_timeout_minutes,
  51. ))
  52. async def _set_credential(self, user_id, credential_type, credential_slot, data):
  53. self._credentials[user_id] = data
  54. async def _delete_user(self, user_id):
  55. self._users = [u for u in self._users if u.user_id != user_id]
  56. self._credentials.pop(user_id, None)
  57. async def _add_user(self, user_id, options, credential=None):
  58. await self._set_user(user_id, options)
  59. if credential is not None:
  60. self._credentials[user_id] = credential.data
  61. else:
  62. self._credentials.pop(user_id, None)
  63. class FakeNode:
  64. def __init__(self, users=None, credentials=None):
  65. self.access_control = FakeAccessControl(users, credentials)
  66. @pytest.fixture(autouse=True)
  67. def auto_enable_custom_integrations(enable_custom_integrations):
  68. yield
  69. @pytest.fixture(autouse=True)
  70. def bypass_manifest_dependencies():
  71. """zwave_js/frontend/lovelace can't be set up in the test harness."""
  72. with patch(
  73. "homeassistant.setup._async_process_dependencies", return_value=[]
  74. ):
  75. yield
  76. @pytest.fixture
  77. def mock_node():
  78. return FakeNode(users=[
  79. FakeUserData(1, active=True),
  80. FakeUserData(2, active=False),
  81. ])
  82. @pytest.fixture
  83. def config_entry():
  84. return MockConfigEntry(
  85. domain=DOMAIN,
  86. data={CONF_LOCK_ENTITY_IDS: [LOCK_ENTITY_ID]},
  87. options={},
  88. entry_id="test_entry",
  89. )