def unadopted_doorlock(doorlock: Doorlock): """Mock UniFi Protect Light device (unadopted).""" no_doorlock = doorlock.copy() no_doorlock.name = "Unadopted Lock" no_doorlock.is_adopted = False return no_doorlock
async def doorlock_fixture( hass: HomeAssistant, mock_entry: MockEntityFixture, mock_doorlock: Doorlock ): """Fixture for a single doorlock for testing the lock platform.""" # disable pydantic validation so mocking can happen Doorlock.__config__.validate_assignment = False lock_obj = mock_doorlock.copy(deep=True) lock_obj._api = mock_entry.api lock_obj.name = "Test Lock" lock_obj.lock_status = LockStatusType.OPEN mock_entry.api.bootstrap.doorlocks = { lock_obj.id: lock_obj, } await hass.config_entries.async_setup(mock_entry.entry.entry_id) await hass.async_block_till_done() assert_entity_counts(hass, Platform.LOCK, 1, 1) yield (lock_obj, "lock.test_lock_lock") Doorlock.__config__.validate_assignment = True
async def test_lock_unlocking( hass: HomeAssistant, ufp: MockUFPFixture, doorlock: Doorlock, unadopted_doorlock: Doorlock, ): """Test lock entity unlocking.""" await init_entry(hass, ufp, [doorlock, unadopted_doorlock]) assert_entity_counts(hass, Platform.LOCK, 1, 1) new_lock = doorlock.copy() new_lock.lock_status = LockStatusType.OPENING mock_msg = Mock() mock_msg.changed_data = {} mock_msg.new_obj = new_lock ufp.api.bootstrap.doorlocks = {new_lock.id: new_lock} ufp.ws_msg(mock_msg) await hass.async_block_till_done() state = hass.states.get("lock.test_lock_lock") assert state assert state.state == STATE_UNLOCKING
async def test_lock_do_unlock( hass: HomeAssistant, ufp: MockUFPFixture, doorlock: Doorlock, unadopted_doorlock: Doorlock, ): """Test lock entity unlock service.""" await init_entry(hass, ufp, [doorlock, unadopted_doorlock]) assert_entity_counts(hass, Platform.LOCK, 1, 1) new_lock = doorlock.copy() new_lock.lock_status = LockStatusType.CLOSED mock_msg = Mock() mock_msg.changed_data = {} mock_msg.new_obj = new_lock ufp.api.bootstrap.doorlocks = {new_lock.id: new_lock} ufp.ws_msg(mock_msg) await hass.async_block_till_done() new_lock.__fields__["open_lock"] = Mock() new_lock.open_lock = AsyncMock() await hass.services.async_call( "lock", "unlock", {ATTR_ENTITY_ID: "lock.test_lock_lock"}, blocking=True, ) new_lock.open_lock.assert_called_once()
async def doorlock_fixture(hass: HomeAssistant, mock_entry: MockEntityFixture, mock_doorlock: Doorlock): """Fixture for a single doorlock for testing the number platform.""" # disable pydantic validation so mocking can happen Doorlock.__config__.validate_assignment = False lock_obj = mock_doorlock.copy(deep=True) lock_obj._api = mock_entry.api lock_obj.name = "Test Lock" lock_obj.auto_close_time = timedelta(seconds=45) mock_entry.api.bootstrap.reset_objects() mock_entry.api.bootstrap.doorlocks = { lock_obj.id: lock_obj, } await hass.config_entries.async_setup(mock_entry.entry.entry_id) await hass.async_block_till_done() assert_entity_counts(hass, Platform.NUMBER, 1, 1) yield lock_obj Doorlock.__config__.validate_assignment = True
def doorlock_fixture(): """Mock UniFi Protect Doorlock device.""" # disable pydantic validation so mocking can happen Doorlock.__config__.validate_assignment = False data = json.loads(load_fixture("sample_doorlock.json", integration=DOMAIN)) yield Doorlock.from_unifi_dict(**data) Doorlock.__config__.validate_assignment = True
async def test_number_lock_auto_close( hass: HomeAssistant, ufp: MockUFPFixture, doorlock: Doorlock ): """Test auto-lock timeout for locks.""" await init_entry(hass, ufp, [doorlock]) assert_entity_counts(hass, Platform.NUMBER, 1, 1) description = DOORLOCK_NUMBERS[0] doorlock.__fields__["set_auto_close_time"] = Mock() doorlock.set_auto_close_time = AsyncMock() _, entity_id = ids_from_device_description(Platform.NUMBER, doorlock, description) await hass.services.async_call( "number", "set_value", {ATTR_ENTITY_ID: entity_id, "value": 15.0}, blocking=True ) doorlock.set_auto_close_time.assert_called_once_with(timedelta(seconds=15.0))
async def test_lock_do_lock( hass: HomeAssistant, ufp: MockUFPFixture, doorlock: Doorlock, unadopted_doorlock: Doorlock, ): """Test lock entity lock service.""" await init_entry(hass, ufp, [doorlock, unadopted_doorlock]) assert_entity_counts(hass, Platform.LOCK, 1, 1) doorlock.__fields__["close_lock"] = Mock() doorlock.close_lock = AsyncMock() await hass.services.async_call( "lock", "lock", {ATTR_ENTITY_ID: "lock.test_lock_lock"}, blocking=True, ) doorlock.close_lock.assert_called_once()
def mock_doorlock(): """Mock UniFi Protect Doorlock device.""" data = json.loads(load_fixture("sample_doorlock.json", integration=DOMAIN)) return Doorlock.from_unifi_dict(**data)