Example #1
0
def test_bootstrap(bootstrap):
    obj = Bootstrap.from_unifi_dict(**deepcopy(bootstrap))

    set_no_debug()
    obj_construct = Bootstrap.from_unifi_dict(**deepcopy(bootstrap))
    set_debug()

    obj_dict = obj.unifi_dict()

    # TODO:
    del bootstrap["legacyUFVs"]
    del bootstrap["displays"]
    del bootstrap["doorlocks"]
    del bootstrap["chimes"]
    del bootstrap["schedules"]
    del bootstrap["nvr"]["uiVersion"]
    del bootstrap["nvr"]["errorCode"]
    del bootstrap["nvr"]["wifiSettings"]
    del bootstrap["nvr"]["ssoChannel"]
    del bootstrap["nvr"]["smartDetectAgreement"]

    for model_type in ModelType.bootstrap_models():
        key = model_type + "s"
        expected_data = bootstrap.pop(key)
        actual_data = obj_dict.pop(key)

        assert len(expected_data) == len(actual_data)

        for index, expected in enumerate(expected_data):
            actual = actual_data[index]
            compare_objs(expected["modelKey"], expected, actual)

    assert bootstrap == obj_dict
    assert obj == obj_construct
Example #2
0
def test_bootstrap_device_not_adopted_no_api(bootstrap):
    bootstrap["cameras"][0]["isAdopted"] = False
    obj = Bootstrap.from_unifi_dict(**deepcopy(bootstrap))

    set_no_debug()
    obj_construct: Bootstrap = Bootstrap.from_unifi_dict(**deepcopy(bootstrap))
    set_debug()

    assert len(obj.cameras) == len(bootstrap["cameras"])
    assert obj.cameras == obj_construct.cameras
Example #3
0
def test_bootstrap_device_not_adopted(bootstrap, protect_client: ProtectApiClient):
    bootstrap["cameras"][0]["isAdopted"] = False
    obj: Bootstrap = Bootstrap.from_unifi_dict(**deepcopy(bootstrap), api=protect_client)

    set_no_debug()
    obj_construct: Bootstrap = Bootstrap.from_unifi_dict(**deepcopy(bootstrap), api=protect_client)
    set_debug()

    assert len(obj.cameras) == len(bootstrap["cameras"]) - 1
    assert obj.cameras == obj_construct.cameras
Example #4
0
    async def get_bootstrap(self) -> Bootstrap:
        """
        Gets bootstrap object from UFP instance

        This is a great alternative if you need metadata about the NVR without connecting to the Websocket
        """

        data = await self.api_request_obj("bootstrap")
        # fix for UniFi Protect bug, some cameras may come back with and old recording mode
        # "motion" and "smartDetect" recording modes was combined into "detections" in Protect 1.20.0
        call_again = False
        for camera_dict in data["cameras"]:
            if camera_dict.get("recordingSettings",
                               {}).get("mode",
                                       "detections") in ("motion",
                                                         "smartDetect"):
                await self.update_device(ModelType.CAMERA, camera_dict["id"], {
                    "recordingSettings": {
                        "mode": RecordingMode.DETECTIONS.value
                    }
                })
                call_again = True

        if call_again:
            data = await self.api_request_obj("bootstrap")
        return Bootstrap.from_unifi_dict(**data, api=self)
Example #5
0
async def test_browse_media_root_multiple_consoles_only_one_media(
        hass: HomeAssistant, ufp: MockUFPFixture, bootstrap: Bootstrap):
    """Test browsing root level media with multiple consoles."""

    ufp.api.bootstrap._has_media = True

    await hass.config_entries.async_setup(ufp.entry.entry_id)
    await hass.async_block_till_done()

    bootstrap2 = bootstrap.copy()
    bootstrap2._has_media = False
    bootstrap2.nvr = bootstrap.nvr.copy()
    bootstrap2.nvr.id = "test_id2"
    bootstrap2.nvr.mac = "A2E00C826924"
    bootstrap2.nvr.name = "UnifiProtect2"

    api2 = Mock()
    bootstrap2.nvr._api = api2
    bootstrap2._api = api2

    api2.bootstrap = bootstrap2
    api2._bootstrap = bootstrap2
    api2.api_path = "/api"
    api2.base_url = "https://127.0.0.2"
    api2.connection_host = IPv4Address("127.0.0.2")
    api2.get_nvr = AsyncMock(return_value=bootstrap2.nvr)
    api2.update = AsyncMock(return_value=bootstrap2)
    api2.async_disconnect_ws = AsyncMock()

    with patch("homeassistant.components.unifiprotect.ProtectApiClient"
               ) as mock_api:
        mock_config = MockConfigEntry(
            domain=DOMAIN,
            data={
                "host": "1.1.1.2",
                "username": "******",
                "password": "******",
                "id": "UnifiProtect2",
                "port": 443,
                "verify_ssl": False,
            },
            version=2,
        )
        mock_config.add_to_hass(hass)

        mock_api.return_value = api2

        await hass.config_entries.async_setup(mock_config.entry_id)
        await hass.async_block_till_done()

    source = await async_get_media_source(hass)
    media_item = MediaSourceItem(hass, DOMAIN, None, None)

    browse = await source.async_browse_media(media_item)

    assert browse.title == "UnifiProtect"
    assert browse.identifier == "test_id:browse"
    assert len(browse.children) == 1
    assert browse.children[0].title == "All Cameras"
    assert browse.children[0].identifier == "test_id:browse:all"
Example #6
0
    async def get_bootstrap(self) -> Bootstrap:
        """
        Gets bootstrap object from UFP instance

        This is a great alternative if you need metadata about the NVR without connecting to the Websocket
        """

        data = await self.api_request_obj("bootstrap")
        return Bootstrap.from_unifi_dict(**data, api=self)
Example #7
0
def test_bootstrap(bootstrap):
    obj = Bootstrap.from_unifi_dict(**deepcopy(bootstrap))

    set_no_debug()
    obj_construct = Bootstrap.from_unifi_dict(**deepcopy(bootstrap))
    set_debug()

    obj_dict = obj.unifi_dict()

    # TODO:
    del bootstrap["legacyUFVs"]
    del bootstrap["displays"]
    del bootstrap["chimes"]
    del bootstrap["schedules"]
    if "deviceGroups" in bootstrap:
        del bootstrap["deviceGroups"]
    del bootstrap["nvr"]["uiVersion"]
    del bootstrap["nvr"]["errorCode"]
    del bootstrap["nvr"]["wifiSettings"]
    del bootstrap["nvr"]["ssoChannel"]
    del bootstrap["nvr"]["smartDetectAgreement"]
    bootstrap["nvr"]["systemInfo"].pop("ustorage", None)

    for model_type in ModelType.bootstrap_models():
        key = model_type + "s"
        expected_data = bootstrap.pop(key)
        actual_data = obj_dict.pop(key)

        assert len(expected_data) == len(actual_data)

        for index, expected in enumerate(expected_data):
            actual = actual_data[index]
            compare_objs(expected["modelKey"], expected, actual)

    for key in NEW_FIELDS.intersection(obj_dict["nvr"].keys()):
        if key not in bootstrap["nvr"]:
            del obj_dict["nvr"][key]
    bootstrap["nvr"]["ports"]["piongw"] = bootstrap["nvr"]["ports"].get("piongw")

    assert bootstrap == obj_dict
    assert obj == obj_construct
Example #8
0
File: utils.py Project: jbouwh/core
def reset_objects(bootstrap: Bootstrap):
    """Reset bootstrap objects."""

    bootstrap.cameras = {}
    bootstrap.lights = {}
    bootstrap.sensors = {}
    bootstrap.viewers = {}
    bootstrap.events = {}
    bootstrap.doorlocks = {}
    bootstrap.chimes = {}
Example #9
0
def bootstrap_fixture(nvr: NVR):
    """Mock Bootstrap fixture."""
    data = json.loads(load_fixture("sample_bootstrap.json",
                                   integration=DOMAIN))
    data["nvr"] = nvr
    data["cameras"] = []
    data["lights"] = []
    data["sensors"] = []
    data["viewers"] = []
    data["liveviews"] = []
    data["events"] = []
    data["doorlocks"] = []
    data["chimes"] = []

    return Bootstrap.from_unifi_dict(**data)
Example #10
0
def test_bootstrap_dns_host(bootstrap):
    bootstrap["nvr"]["hosts"] = ["se-gw.local"]

    bootstrap_obj = Bootstrap.from_unifi_dict(**bootstrap)
    assert bootstrap_obj.nvr.hosts == ["se-gw.local"]
Example #11
0
def test_bootstrap_ip_host(bootstrap):
    bootstrap["nvr"]["hosts"] = ["1.1.1.1"]

    bootstrap_obj = Bootstrap.from_unifi_dict(**bootstrap)
    assert bootstrap_obj.nvr.hosts == [IPv4Address("1.1.1.1")]
Example #12
0
 def create():
     Bootstrap.from_unifi_dict(**deepcopy(bootstrap))