import responses from responses import matchers from unifi_access.schemas import DoorGroupId, DoorId, DoorLockingRuleType from .base import UnifiAccessTests class SpaceTests(UnifiAccessTests): @responses.activate def test_fetch_door_group_topology(self) -> None: """7.1 Fetch Door Group Topology""" responses.get( f"https://{self.host}/api/v1/developer/door_groups/topology", match=[matchers.header_matcher(self.common_headers)], json={ "code": "SUCCESS", "data": [ { "id": "d5573467-d6b3-4e8f-8e48-8a322b91664a", "name": "All Doors", "resource_topologies": [ { "id": "9bee6e0e-108d-4c52-9107-76f2c7dea4f1", "name": "Main Floor", "resources": [ { "id": "6ff875d2-af87-470b-9cb5-774c6596afc8", "name": "Door 3855", "type": "door", "is_bind_hub": True, } ], "type": "floor", } ], "type": "building", }, { "id": "5c496423-6d25-4e4f-8cdf-95ad5135188a", "name": "customized group", "resource_topologies": [ { "id": "9bee6e0e-108d-4c52-9107-76f2c7dea4f1", "name": "Main Floor", "resources": [ { "id": "6ff875d2-af87-470b-9cb5-774c6596afc8", "name": "Door 3855", "type": "door", "is_bind_hub": True, } ], "type": "floor", } ], "type": "access", }, ], "msg": "success", }, ) resp = self.client.fetch_door_group_topology() @responses.activate def test_create_door_group(self) -> None: """7.2 Create Door Group""" responses.post( f"https://{self.host}/api/v1/developer/door_groups", match=[ matchers.header_matcher(self.common_headers), matchers.json_params_matcher( { "group_name": "Test", "resources": ["6ff875d2-af87-470b-9cb5-774c6596afc8"], } ), ], json={ "code": "SUCCESS", "data": { "id": "0140fa3d-8973-4305-a0ce-5306ae277878", "name": "Customized Door Group", "resources": [ {"id": "6ff875d2-af87-470b-9cb5-774c6596afc8", "type": "door"} ], "type": "access", }, "msg": "success", }, ) # XXX: TODO: test if this supports both doors and door groups resp = self.client.create_door_group( group_name="Test", resources=[DoorId("6ff875d2-af87-470b-9cb5-774c6596afc8")], ) @responses.activate def test_fetch_door_group_building(self) -> None: """7.3 Fetch Door Group""" door_group_id = DoorGroupId("d5573467-d6b3-4e8f-8e48-8a322b91664a") responses.get( f"https://{self.host}/api/v1/developer/door_groups/{door_group_id}", match=[matchers.header_matcher(self.common_headers)], # Group type is building json={ "code": "SUCCESS", "data": { "id": "d5573467-d6b3-4e8f-8e48-8a322b91664a", "name": "All Doors", "resources": [ { "id": "6ff875d2-af87-470b-9cb5-774c6596afc8", "name": "Door 3855", "type": "door", }, { "id": "7cc1823f-9cdb-447b-b01b-4cb2abc661c0", "name": "A2 Door", "type": "door", }, { "id": "ececa68e-239f-4b82-adc4-0c9ce70c60ff", "name": "A3", "type": "door", }, ], "type": "building", }, "msg": "success", }, ) resp = self.client.fetch_door_group(door_group_id=door_group_id) @responses.activate def test_fetch_door_group_customized_groups(self) -> None: """7.3 Fetch Door Group""" door_group_id = DoorGroupId("1be0c995-0347-4cb2-93b3-66a9624af568") responses.get( f"https://{self.host}/api/v1/developer/door_groups/{door_group_id}", match=[matchers.header_matcher(self.common_headers)], # Customized groups json={ "code": "SUCCESS", "data": { "id": "1be0c995-0347-4cb2-93b3-66a9624af568", "name": "Door Group 01", "resources": [ { "id": "6ff875d2-af87-470b-9cb5-774c6596afc8", "type": "door", "name": "Door 385", } ], "type": "access", }, "msg": "success", }, ) resp = self.client.fetch_door_group(door_group_id=door_group_id) @responses.activate def test_update_door_group(self) -> None: """7.4 Update Door Group""" door_group_id = DoorGroupId("0140fa3d-8973-4305-a0ce-5306ae277878") responses.put( f"https://{self.host}/api/v1/developer/door_groups/{door_group_id}", match=[ matchers.header_matcher(self.common_headers), matchers.json_params_matcher( { "resources": [ "6ff875d2-af87-470b-9cb5-774c6596afc8", "5a2c3d4e-1f6b-4c8d-9e0f-2a3b4c5d6e7f", "2p3q4r5s-6t7u-8v9w-x0y1-z2a3b4c5d6e", ] } ), ], json={ "code": "SUCCESS", "data": { "id": "0140fa3d-8973-4305-a0ce-5306ae277878", "name": "test", "resources": [ {"id": "6ff875d2-af87-470b-9cb5-774c6596afc8", "type": "door"} ], "type": "access", }, "msg": "success", }, ) # XXX: TODO: verify that this accepts both Doors and Door Groups resp = self.client.update_door_group( door_group_id=door_group_id, resources=[ DoorId("6ff875d2-af87-470b-9cb5-774c6596afc8"), DoorId("5a2c3d4e-1f6b-4c8d-9e0f-2a3b4c5d6e7f"), DoorGroupId("2p3q4r5s-6t7u-8v9w-x0y1-z2a3b4c5d6e"), ], ) @responses.activate def test_update_door_group_delete_resources(self) -> None: """7.4 Update Door Group""" door_group_id = DoorGroupId("0140fa3d-8973-4305-a0ce-5306ae277878") responses.put( f"https://{self.host}/api/v1/developer/door_groups/{door_group_id}", match=[ matchers.header_matcher(self.common_headers), matchers.json_params_matcher({"resources": []}), ], # NOTE: no example provided in API docs json={ "code": "SUCCESS", "data": { "id": "0140fa3d-8973-4305-a0ce-5306ae277878", "name": "test", "resources": [], "type": "access", }, "msg": "success", }, ) resp = self.client.update_door_group( door_group_id=door_group_id, resources=[], ) @responses.activate def test_fetch_all_door_groups(self) -> None: """7.5 Fetch All Door Groups""" responses.get( f"https://{self.host}/api/v1/developer/door_groups", match=[matchers.header_matcher(self.common_headers)], json={ "code": "SUCCESS", "data": [ { "id": "5c496423-6d25-4e4f-8cdf-95ad5135188a", "name": "Test", "resources": [ { "id": "6ff875d2-af87-470b-9cb5-774c6596afc8", "type": "door", } ], "type": "access", }, { "id": "1907cc46-0a73-4077-94c1-95b625bdb0f8", "name": "Test2", "resources": [ { "id": "6ff875d2-af87-470b-9cb5-774c6596afc8", "type": "door", } ], "type": "access", }, ], "msg": "success", }, ) resp = self.client.fetch_all_door_groups() @responses.activate def test_delete_door_groups(self) -> None: """7.6 Delete Door Group""" door_group_id = DoorGroupId("0140fa3d-8973-4305-a0ce-5306ae277878") responses.get( f"https://{self.host}/api/v1/developer/door_groups/{door_group_id}", match=[matchers.header_matcher(self.common_headers)], json={"code": "SUCCESS", "data": "success", "msg": "success"}, ) resp = self.client.delete_door_group(door_group_id=door_group_id) @responses.activate def test_fetch_door(self) -> None: """7.7 Fetch Door""" door_id = DoorId("0ed545f8-2fcd-4839-9021-b39e707f6aa9") responses.get( f"https://{self.host}/api/v1/developer/doors/{door_id}", match=[matchers.header_matcher(self.common_headers)], json={ "code": "SUCCESS", "data": { "door_lock_relay_status": "lock", "door_position_status": "", "floor_id": "3275af8d-3fa7-4902-a11b-011e41c8464a", "full_name": "UNVR - 1F - Main Door", "id": "0ed545f8-2fcd-4839-9021-b39e707f6aa9", "is_bind_hub": True, "name": "Main Door", "type": "door", }, "msg": "success", }, ) resp = self.client.fetch_door(door_id=door_id) @responses.activate def test_fetch_all_doors(self) -> None: """7.8 Fetch All Doors""" responses.get( f"https://{self.host}/api/v1/developer/doors", match=[matchers.header_matcher(self.common_headers)], json={ "code": "SUCCESS", "data": [ { "door_lock_relay_status": "unlock", "door_position_status": "open", "floor_id": "23c5db06-b59b-494d-94f1-23e88fbe4909", "full_name": "UNVR - 2F - A2 Door", "id": "0ed545f8-2fcd-4839-9021-b39e707f6aa9", "is_bind_hub": True, "name": "A2 Door", "type": "door", }, { "door_lock_relay_status": "lock", "door_position_status": "close", "floor_id": "7c62b4b3-692f-44ea-8eb8-e212833b4e0f", "full_name": "UNVR - 1F - Door 3855", "id": "5785e97b-6123-4596-ba49-b6e51164db9b", "is_bind_hub": True, "name": "Door 3855", "type": "door", }, ], "msg": "success", }, ) resp = self.client.fetch_all_doors() @responses.activate def test_unlock_door(self) -> None: """7.9 Remote Door Unlocking""" door_id = DoorId("5785e97b-6123-4596-ba49-b6e51164db9b") responses.put( f"https://{self.host}/api/v1/developer/doors/{door_id}/unlock", match=[matchers.header_matcher(self.common_headers)], json={"code": "SUCCESS", "data": "success", "msg": "success"}, ) resp = self.client.unlock_door(door_id=door_id) @responses.activate def test_set_temporary_door_locking_rule(self) -> None: """7.10 Set Temporary Door Locking Rule""" door_id = DoorId("e4978b83-203d-4015-97df-b86efc91cb0c") responses.put( f"https://{self.host}/api/v1/developer/doors/{door_id}/lock_rule", match=[ matchers.header_matcher(self.common_headers), matchers.json_params_matcher({"type": "custom", "interval": 10}), ], json={"code": "SUCCESS", "data": "success", "msg": "success"}, ) # Customized 10-minute unlocked resp = self.client.set_temporary_door_locking_rule( door_id=door_id, type_=DoorLockingRuleType.CUSTOM, interval=10 ) # XXX: TODO: test the other examples? @responses.activate def test_fetch_door_locking_rule(self) -> None: """7.11 Fetch Door Locking Rule""" door_id = DoorId("e4978b83-203d-4015-97df-b86efc91cb0c") responses.get( f"https://{self.host}/api/v1/developer/doors/{door_id}/lock_rule", match=[matchers.header_matcher(self.common_headers)], # Keep it locked json={ "code": "SUCCESS", "data": {"ended_time": 3602128309, "type": "keep_lock"}, "msg": "success", }, ) resp = self.client.fetch_door_locking_rule(door_id=door_id) # XXX: TODO: test the other examples? @responses.activate def test_set_door_emergency_status(self) -> None: """7.12 Set Door Emergency Status""" responses.put( f"https://{self.host}/api/v1/developer/doors/settings/emergency", match=[ matchers.header_matcher(self.common_headers), matchers.json_params_matcher({"lockdown": True, "evacuation": False}), ], json={"code": "SUCCESS", "data": "success", "msg": "success"}, ) # Keep it locked resp = self.client.set_door_emergency_status(lockdown=True, evacuation=False) # XXX: TODO: test the other examples? @responses.activate def test_fetch_door_emergency_status(self) -> None: """7.13 Fetch Door Emergency Status""" responses.get( f"https://{self.host}/api/v1/developer/doors/settings/emergency", match=[matchers.header_matcher(self.common_headers)], json={ "code": "SUCCESS", "data": {"evacuation": True, "lockdown": False}, "msg": "success", }, ) resp = self.client.fetch_door_emergency_status()