async def setup_deconz_integration(hass, config, options, get_state_response):
    """Create the deCONZ gateway."""
    config_entry = config_entries.ConfigEntry(
        version=1,
        domain=deconz.DOMAIN,
        title="Mock Title",
        data=config,
        source="test",
        connection_class=config_entries.CONN_CLASS_LOCAL_PUSH,
        system_options={},
        options=options,
        entry_id="1",
    )

    for resource in ("groups", "lights", "sensors"):
        if resource not in get_state_response:
            get_state_response[resource] = {}

    with patch("pydeconz.DeconzSession.request",
               return_value=get_state_response), patch(
                   "pydeconz.DeconzSession.start", return_value=True):
        await deconz.async_setup_entry(hass, config_entry)
    await hass.async_block_till_done()

    hass.config_entries._entries.append(config_entry)

    return hass.data[deconz.DOMAIN].get(config[deconz.CONF_BRIDGEID])
Beispiel #2
0
async def setup_device(hass):
    """Load the Axis binary sensor platform."""
    from axis import AxisDevice
    loop = Mock()

    config_entry = config_entries.ConfigEntry(
        1,
        axis.DOMAIN,
        'Mock Title',
        ENTRY_CONFIG,
        'test',
        config_entries.CONN_CLASS_LOCAL_PUSH,
        options=ENTRY_OPTIONS)
    device = axis.AxisNetworkDevice(hass, config_entry)
    device.api = AxisDevice(loop=loop,
                            **config_entry.data[axis.CONF_DEVICE],
                            signal=device.async_signal_callback)
    hass.data[axis.DOMAIN] = {device.serial: device}

    await hass.config_entries.async_forward_entry_setup(
        config_entry, 'binary_sensor')
    # To flush out the service call to update the group
    await hass.async_block_till_done()

    return device
Beispiel #3
0
async def setup_test_accessories(hass, accessories):
    """Load a fake homekit device based on captured JSON profile."""
    fake_controller = await setup_platform(hass)
    pairing = fake_controller.add(accessories)

    discovery_info = {
        'name': 'TestDevice',
        'host': '127.0.0.1',
        'port': 8080,
        'properties': {
            'md': 'TestDevice',
            'id': '00:00:00:00:00:00',
            'c#': 1,
        }
    }

    pairing.pairing_data.update({
        'AccessoryPairingID':
        discovery_info['properties']['id'],
    })

    config_entry = config_entries.ConfigEntry(
        1, 'homekit_controller', 'TestData', pairing.pairing_data, 'test',
        config_entries.CONN_CLASS_LOCAL_PUSH)

    pairing_cls_loc = 'homekit.controller.ip_implementation.IpPairing'
    with mock.patch(pairing_cls_loc) as pairing_cls:
        pairing_cls.return_value = pairing
        await async_setup_entry(hass, config_entry)
        await hass.async_block_till_done()

    return pairing
Beispiel #4
0
async def test_hue_activate_scene_transition(hass, mock_api_v1):
    """Test successful hue_activate_scene with transition."""
    config_entry = config_entries.ConfigEntry(
        1,
        hue.DOMAIN,
        "Mock Title",
        {"host": "1.2.3.4", "api_key": "mock-api-key", "api_version": 1},
        "test",
        options={CONF_ALLOW_HUE_GROUPS: True, CONF_ALLOW_UNREACHABLE: False},
    )

    mock_api_v1.mock_group_responses.append(GROUP_RESPONSE)
    mock_api_v1.mock_scene_responses.append(SCENE_RESPONSE)

    with patch.object(bridge, "HueBridgeV1", return_value=mock_api_v1), patch.object(
        hass.config_entries, "async_forward_entry_setup"
    ):
        hue_bridge = bridge.HueBridge(hass, config_entry)
        assert await hue_bridge.async_initialize_bridge() is True

    assert hue_bridge.api is mock_api_v1

    with patch("aiohue.HueBridgeV1", return_value=mock_api_v1):
        assert (
            await hue.services.hue_activate_scene_v1(
                hue_bridge, "Group 1", "Cozy dinner", 30
            )
            is True
        )

    assert len(mock_api_v1.mock_requests) == 3
    assert mock_api_v1.mock_requests[2]["json"]["scene"] == "scene_1"
    assert mock_api_v1.mock_requests[2]["json"]["transitiontime"] == 30
    assert mock_api_v1.mock_requests[2]["path"] == "groups/group_1/action"
Beispiel #5
0
async def setup_gateway(hass, data, allow_clip_sensor=True):
    """Load the deCONZ binary sensor platform."""
    from pydeconz import DeconzSession

    loop = Mock()
    session = Mock()

    ENTRY_OPTIONS[deconz.const.CONF_ALLOW_CLIP_SENSOR] = allow_clip_sensor

    config_entry = config_entries.ConfigEntry(
        1,
        deconz.DOMAIN,
        "Mock Title",
        ENTRY_CONFIG,
        "test",
        config_entries.CONN_CLASS_LOCAL_PUSH,
        system_options={},
        options=ENTRY_OPTIONS,
    )
    gateway = deconz.DeconzGateway(hass, config_entry)
    gateway.api = DeconzSession(loop, session, **config_entry.data)
    gateway.api.config = Mock()
    hass.data[deconz.DOMAIN] = {gateway.bridgeid: gateway}

    with patch("pydeconz.DeconzSession.async_get_state",
               return_value=mock_coro(data)):
        await gateway.api.async_load_parameters()

    await hass.config_entries.async_forward_entry_setup(
        config_entry, "binary_sensor")
    # To flush out the service call to update the group
    await hass.async_block_till_done()
    return gateway
async def setup_gateway(hass, data, allow_clip_sensor=True):
    """Load the deCONZ sensor platform."""
    from pydeconz import DeconzSession

    response = Mock(status=200,
                    json=asynctest.CoroutineMock(),
                    text=asynctest.CoroutineMock())
    response.content_type = 'application/json'

    session = Mock(put=asynctest.CoroutineMock(return_value=response))

    ENTRY_CONFIG[deconz.const.CONF_ALLOW_CLIP_SENSOR] = allow_clip_sensor

    config_entry = config_entries.ConfigEntry(
        1, deconz.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
        config_entries.CONN_CLASS_LOCAL_PUSH)
    gateway = deconz.DeconzGateway(hass, config_entry)
    gateway.api = DeconzSession(hass.loop, session, **config_entry.data)
    gateway.api.config = Mock()
    hass.data[deconz.DOMAIN] = {gateway.bridgeid: gateway}

    with patch('pydeconz.DeconzSession.async_get_state',
               return_value=mock_coro(data)):
        await gateway.api.async_load_parameters()

    await hass.config_entries.async_forward_entry_setup(
        config_entry, 'climate')
    # To flush out the service call to update the group
    await hass.async_block_till_done()
    return gateway
Beispiel #7
0
async def test_hue_activate_scene_scene_not_found(hass, mock_api_v1):
    """Test failed hue_activate_scene due to missing scene."""
    config_entry = config_entries.ConfigEntry(
        1,
        hue.DOMAIN,
        "Mock Title",
        {"host": "1.2.3.4", "api_key": "mock-api-key", "api_version": 1},
        "test",
        options={CONF_ALLOW_HUE_GROUPS: True, CONF_ALLOW_UNREACHABLE: False},
    )

    mock_api_v1.mock_group_responses.append(GROUP_RESPONSE)
    mock_api_v1.mock_scene_responses.append({})

    with patch.object(bridge, "HueBridgeV1", return_value=mock_api_v1), patch.object(
        hass.config_entries, "async_forward_entry_setup"
    ):
        hue_bridge = bridge.HueBridge(hass, config_entry)
        assert await hue_bridge.async_initialize_bridge() is True

    assert hue_bridge.api is mock_api_v1

    with patch("aiohue.HueBridgeV1", return_value=mock_api_v1):
        assert (
            await hue.services.hue_activate_scene_v1(
                hue_bridge, "Group 1", "Cozy dinner"
            )
            is False
        )
Beispiel #8
0
async def setup_gateway(hass, data):
    """Load the deCONZ cover platform."""
    from pydeconz import DeconzSession

    loop = Mock()
    session = Mock()

    config_entry = config_entries.ConfigEntry(
        1,
        deconz.DOMAIN,
        "Mock Title",
        ENTRY_CONFIG,
        "test",
        config_entries.CONN_CLASS_LOCAL_PUSH,
    )
    gateway = deconz.DeconzGateway(hass, config_entry)
    gateway.api = DeconzSession(loop, session, **config_entry.data)
    gateway.api.config = Mock()
    hass.data[deconz.DOMAIN] = {gateway.bridgeid: gateway}

    with patch("pydeconz.DeconzSession.async_get_state", return_value=mock_coro(data)):
        await gateway.api.async_load_parameters()

    await hass.config_entries.async_forward_entry_setup(config_entry, "cover")
    # To flush out the service call to update the group
    await hass.async_block_till_done()
    return gateway
Beispiel #9
0
async def setup_device(hass):
    """Load the Axis binary sensor platform."""
    from axis import AxisDevice

    loop = Mock()

    config_entry = config_entries.ConfigEntry(
        1,
        axis.DOMAIN,
        "Mock Title",
        ENTRY_CONFIG,
        "test",
        config_entries.CONN_CLASS_LOCAL_PUSH,
        system_options={},
        options=ENTRY_OPTIONS,
    )
    device = axis.AxisNetworkDevice(hass, config_entry)
    device.api = AxisDevice(loop=loop, **config_entry.data[axis.CONF_DEVICE])
    hass.data[axis.DOMAIN] = {device.serial: device}
    device.api.enable_events(event_callback=device.async_event_callback)

    await hass.config_entries.async_forward_entry_setup(config_entry, "camera")
    # To flush out the service call to update the group
    await hass.async_block_till_done()

    return device
Beispiel #10
0
async def test_hue_activate_scene_transition(hass, mock_api):
    """Test successful hue_activate_scene with transition."""
    config_entry = config_entries.ConfigEntry(
        1,
        hue.DOMAIN,
        "Mock Title",
        {"host": "mock-host", "username": "******"},
        "test",
        config_entries.CONN_CLASS_LOCAL_POLL,
        system_options={},
        options={CONF_ALLOW_HUE_GROUPS: True, CONF_ALLOW_UNREACHABLE: False},
    )
    hue_bridge = bridge.HueBridge(hass, config_entry)

    mock_api.mock_group_responses.append(GROUP_RESPONSE)
    mock_api.mock_scene_responses.append(SCENE_RESPONSE)

    with patch("aiohue.Bridge", return_value=mock_api), patch.object(
        hass.config_entries, "async_forward_entry_setup"
    ):
        assert await hue_bridge.async_setup() is True

    assert hue_bridge.api is mock_api

    call = Mock()
    call.data = {"group_name": "Group 1", "scene_name": "Cozy dinner", "transition": 30}
    with patch("aiohue.Bridge", return_value=mock_api):
        assert await hue_bridge.hue_activate_scene(call.data) is None

    assert len(mock_api.mock_requests) == 3
    assert mock_api.mock_requests[2]["json"]["scene"] == "scene_1"
    assert mock_api.mock_requests[2]["json"]["transitiontime"] == 30
    assert mock_api.mock_requests[2]["path"] == "groups/group_1/action"
async def setup_zwave(hass, fixture=None):
    """Set up Z-Wave and load a dump."""
    hass.config.components.add("mqtt")

    with patch(
            "homeassistant.components.mqtt.async_subscribe") as mock_subscribe:
        await hass.config_entries.async_add(
            config_entries.ConfigEntry(
                1,
                DOMAIN,
                "Z-Wave",
                {},
                config_entries.SOURCE_USER,
                config_entries.CONN_CLASS_LOCAL_PUSH,
                {},
            ))
        await hass.async_block_till_done()

    assert "zwave_mqtt" in hass.config.components
    assert len(mock_subscribe.mock_calls) == 1
    receive_message = mock_subscribe.mock_calls[0][1][2]

    if fixture is not None:
        data = Path(__file__).parent / "fixtures" / fixture

        with data.open("rt") as fp:
            for line in fp:
                topic, payload = line.strip().split(",", 1)
                receive_message(Mock(topic=topic, payload=payload))

        await hass.async_block_till_done()

    return receive_message
Beispiel #12
0
async def test_hue_activate_scene_scene_not_found(hass, mock_api):
    """Test failed hue_activate_scene due to missing scene."""
    config_entry = config_entries.ConfigEntry(
        1,
        hue.DOMAIN,
        "Mock Title",
        {
            "host": "mock-host",
            "username": "******"
        },
        "test",
        system_options={},
        options={
            CONF_ALLOW_HUE_GROUPS: True,
            CONF_ALLOW_UNREACHABLE: False
        },
    )
    hue_bridge = bridge.HueBridge(hass, config_entry)

    mock_api.mock_group_responses.append(GROUP_RESPONSE)
    mock_api.mock_scene_responses.append({})

    with patch("aiohue.Bridge", return_value=mock_api), patch.object(
            hass.config_entries, "async_forward_entry_setup"):
        assert await hue_bridge.async_setup() is True

    assert hue_bridge.api is mock_api

    call = Mock()
    call.data = {"group_name": "Group 1", "scene_name": "Cozy dinner"}
    with patch("aiohue.Bridge", return_value=mock_api):
        assert await hue_bridge.hue_activate_scene(call.data) is False
Beispiel #13
0
async def setup_bridge(hass, mock_bridge):
    """Load the Hue light platform with the provided bridge."""
    hass.config.components.add(hue.DOMAIN)
    hass.data[hue.DOMAIN] = {'mock-host': mock_bridge}
    config_entry = config_entries.ConfigEntry(1, hue.DOMAIN, 'Mock Title',
                                              {'host': 'mock-host'}, 'test')
    await hass.config_entries.async_forward_entry_setup(config_entry, 'light')
    # To flush out the service call to update the group
    await hass.async_block_till_done()
Beispiel #14
0
async def setup_ozw(hass, mock_openzwave):
    """Set up the mock ZWave config entry."""
    hass.config.components.add('zwave')
    config_entry = config_entries.ConfigEntry(
        1, 'zwave', 'Mock Title', {
            'usb_path': 'mock-path',
            'network_key': 'mock-key'
        }, 'test', config_entries.CONN_CLASS_LOCAL_PUSH)
    await hass.config_entries.async_forward_entry_setup(config_entry, 'lock')
    await hass.async_block_till_done()
Beispiel #15
0
async def setup_controller(hass, mock_controller):
    """Load the UniFi switch platform with the provided controller."""
    hass.config.components.add(unifi.DOMAIN)
    hass.data[unifi.DOMAIN] = {CONTROLLER_ID: mock_controller}
    config_entry = config_entries.ConfigEntry(
        1, unifi.DOMAIN, 'Mock Title', ENTRY_CONFIG, 'test',
        config_entries.CONN_CLASS_LOCAL_POLL)
    await hass.config_entries.async_forward_entry_setup(config_entry, 'switch')
    # To flush out the service call to update the group
    await hass.async_block_till_done()
def create_config_entry():
    """Mock a config entry."""
    return config_entries.ConfigEntry(
        1,
        hue.DOMAIN,
        "Mock Title",
        {"host": "mock-host"},
        "test",
        config_entries.CONN_CLASS_LOCAL_POLL,
        system_options={},
    )
async def test_restoring_client(hass, aioclient_mock):
    """Verify clients are restored from clients_all if they ever was registered to entity registry."""
    client = {
        "hostname": "client",
        "is_wired": True,
        "last_seen": 1562600145,
        "mac": "00:00:00:00:00:01",
    }
    restored = {
        "hostname": "restored",
        "is_wired": True,
        "last_seen": 1562600145,
        "mac": "00:00:00:00:00:02",
    }
    not_restored = {
        "hostname": "not_restored",
        "is_wired": True,
        "last_seen": 1562600145,
        "mac": "00:00:00:00:00:03",
    }

    config_entry = config_entries.ConfigEntry(
        version=1,
        domain=UNIFI_DOMAIN,
        title="Mock Title",
        data=ENTRY_CONFIG,
        source="test",
        connection_class=config_entries.CONN_CLASS_LOCAL_POLL,
        system_options={},
        options={},
        entry_id=1,
    )

    registry = er.async_get(hass)
    registry.async_get_or_create(
        TRACKER_DOMAIN,
        UNIFI_DOMAIN,
        f'{restored["mac"]}-site_id',
        suggested_object_id=restored["hostname"],
        config_entry=config_entry,
    )

    await setup_unifi_integration(
        hass,
        aioclient_mock,
        options={CONF_BLOCK_CLIENT: True},
        clients_response=[client],
        clients_all_response=[restored, not_restored],
    )

    assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 2
    assert hass.states.get("device_tracker.client")
    assert hass.states.get("device_tracker.restored")
    assert not hass.states.get("device_tracker.not_restored")
async def setup_ozw(hass, mock_openzwave):
    """Set up the mock ZWave config entry."""
    hass.config.components.add("zwave")
    config_entry = config_entries.ConfigEntry(
        1,
        "zwave",
        "Mock Title",
        {"usb_path": "mock-path", "network_key": "mock-key"},
        "test",
    )
    await hass.config_entries.async_forward_entry_setup(config_entry, "lock")
    await hass.async_block_till_done()
Beispiel #19
0
async def setup_bridge(hass, mock_bridge):
    """Load the Hue platform with the provided bridge."""
    hass.config.components.add(hue.DOMAIN)
    hass.data[hue.DOMAIN] = {'mock-host': mock_bridge}
    config_entry = config_entries.ConfigEntry(
        1, hue.DOMAIN, 'Mock Title', {'host': 'mock-host'}, 'test',
        config_entries.CONN_CLASS_LOCAL_POLL)
    await hass.config_entries.async_forward_entry_setup(
        config_entry, 'binary_sensor')
    await hass.config_entries.async_forward_entry_setup(config_entry, 'sensor')
    # and make sure it completes before going further
    await hass.async_block_till_done()
Beispiel #20
0
def config_entry_fixture(hass):
    """Fixture representing a config entry."""
    config_entry = config_entries.ConfigEntry(
        1,
        DOMAIN,
        "Mock Title",
        {},
        "test",
        config_entries.CONN_CLASS_LOCAL_PUSH,
        system_options={},
    )
    return config_entry
Beispiel #21
0
async def test_restoring_client(hass):
    """Test the update_items function with some clients."""
    config_entry = config_entries.ConfigEntry(
        version=1,
        domain=unifi.DOMAIN,
        title="Mock Title",
        data=ENTRY_CONFIG,
        source="test",
        connection_class=config_entries.CONN_CLASS_LOCAL_POLL,
        system_options={},
        options={},
        entry_id=1,
    )

    registry = await entity_registry.async_get_registry(hass)
    registry.async_get_or_create(
        switch.DOMAIN,
        unifi.DOMAIN,
        "poe-{}".format(CLIENT_1["mac"]),
        suggested_object_id=CLIENT_1["hostname"],
        config_entry=config_entry,
    )
    registry.async_get_or_create(
        switch.DOMAIN,
        unifi.DOMAIN,
        "poe-{}".format(CLIENT_2["mac"]),
        suggested_object_id=CLIENT_2["hostname"],
        config_entry=config_entry,
    )

    controller = await setup_unifi_integration(
        hass,
        ENTRY_CONFIG,
        options={
            unifi.CONF_BLOCK_CLIENT: ["random mac"],
            unifi.const.CONF_TRACK_CLIENTS: False,
            unifi.const.CONF_TRACK_DEVICES: False,
        },
        sites=SITES,
        clients_response=[CLIENT_2],
        devices_response=[DEVICE_1],
        clients_all_response=[CLIENT_1],
    )

    assert len(controller.mock_requests) == 3
    assert len(hass.states.async_all()) == 5

    device_1 = hass.states.get("switch.client_1")
    assert device_1 is not None
Beispiel #22
0
async def setup_bridge(hass, mock_bridge):
    """Load the Hue light platform with the provided bridge."""
    hass.config.components.add(hue.DOMAIN)
    config_entry = config_entries.ConfigEntry(
        1,
        hue.DOMAIN,
        "Mock Title",
        {"host": "mock-host"},
        "test",
    )
    mock_bridge.config_entry = config_entry
    hass.data[hue.DOMAIN] = {config_entry.entry_id: mock_bridge}
    await hass.config_entries.async_forward_entry_setup(config_entry, "light")
    # To flush out the service call to update the group
    await hass.async_block_till_done()
Beispiel #23
0
async def setup_bridge(hass, mock_bridge):
    """Load the Hue light platform with the provided bridge."""
    hass.config.components.add(hue.DOMAIN)
    hass.data[hue.DOMAIN] = {"mock-host": mock_bridge}
    config_entry = config_entries.ConfigEntry(
        1,
        hue.DOMAIN,
        "Mock Title",
        {"host": "mock-host"},
        "test",
        config_entries.CONN_CLASS_LOCAL_POLL,
        system_options={},
    )
    await hass.config_entries.async_forward_entry_setup(config_entry, "light")
    # To flush out the service call to update the group
    await hass.async_block_till_done()
Beispiel #24
0
async def test_restoring_client(hass):
    """Test the update_items function with some clients."""
    config_entry = config_entries.ConfigEntry(
        version=1,
        domain=UNIFI_DOMAIN,
        title="Mock Title",
        data=ENTRY_CONFIG,
        source="test",
        connection_class=config_entries.CONN_CLASS_LOCAL_POLL,
        system_options={},
        options={},
        entry_id=1,
    )

    registry = await entity_registry.async_get_registry(hass)
    registry.async_get_or_create(
        SWITCH_DOMAIN,
        UNIFI_DOMAIN,
        f'{POE_SWITCH}-{CLIENT_1["mac"]}',
        suggested_object_id=CLIENT_1["hostname"],
        config_entry=config_entry,
    )
    registry.async_get_or_create(
        SWITCH_DOMAIN,
        UNIFI_DOMAIN,
        f'{POE_SWITCH}-{CLIENT_2["mac"]}',
        suggested_object_id=CLIENT_2["hostname"],
        config_entry=config_entry,
    )

    controller = await setup_unifi_integration(
        hass,
        options={
            CONF_BLOCK_CLIENT: ["random mac"],
            CONF_TRACK_CLIENTS: False,
            CONF_TRACK_DEVICES: False,
        },
        clients_response=[CLIENT_2],
        devices_response=[DEVICE_1],
        clients_all_response=[CLIENT_1],
    )

    assert len(controller.mock_requests) == 4
    assert len(hass.states.async_entity_ids(SWITCH_DOMAIN)) == 2

    device_1 = hass.states.get("switch.client_1")
    assert device_1 is not None
Beispiel #25
0
async def setup_bridge(hass, data):
    """Load the deCONZ scene platform."""
    from pydeconz import DeconzSession
    loop = Mock()
    session = Mock()
    entry = Mock()
    entry.data = {'host': '1.2.3.4', 'port': 80, 'api_key': '1234567890ABCDEF'}
    bridge = DeconzSession(loop, session, **entry.data)
    with patch('pydeconz.DeconzSession.async_get_state',
               return_value=mock_coro(data)):
        await bridge.async_load_parameters()
    hass.data[deconz.DOMAIN] = bridge
    hass.data[deconz.DATA_DECONZ_ID] = {}
    config_entry = config_entries.ConfigEntry(1, deconz.DOMAIN, 'Mock Title',
                                              {'host': 'mock-host'}, 'test')
    await hass.config_entries.async_forward_entry_setup(config_entry, 'scene')
    # To flush out the service call to update the group
    await hass.async_block_till_done()
async def setup_bridge(hass, mock_bridge, hostname=None):
    """Load the Hue platform with the provided bridge."""
    if hostname is None:
        hostname = "mock-host"
    hass.config.components.add(hue.DOMAIN)
    hass.data[hue.DOMAIN] = {hostname: mock_bridge}
    config_entry = config_entries.ConfigEntry(
        1,
        hue.DOMAIN,
        "Mock Title",
        {"host": hostname},
        "test",
        config_entries.CONN_CLASS_LOCAL_POLL,
    )
    await hass.config_entries.async_forward_entry_setup(
        config_entry, "binary_sensor")
    await hass.config_entries.async_forward_entry_setup(config_entry, "sensor")
    # and make sure it completes before going further
    await hass.async_block_till_done()
Beispiel #27
0
async def test_restoring_client(hass):
    """Test the update_items function with some clients."""
    config_entry = config_entries.ConfigEntry(
        version=1,
        domain=unifi.DOMAIN,
        title="Mock Title",
        data=ENTRY_CONFIG,
        source="test",
        connection_class=config_entries.CONN_CLASS_LOCAL_POLL,
        system_options={},
        options={},
        entry_id=1,
    )

    registry = await entity_registry.async_get_registry(hass)
    registry.async_get_or_create(
        device_tracker.DOMAIN,
        unifi.DOMAIN,
        "{}-site_id".format(CLIENT_1["mac"]),
        suggested_object_id=CLIENT_1["hostname"],
        config_entry=config_entry,
    )
    registry.async_get_or_create(
        device_tracker.DOMAIN,
        unifi.DOMAIN,
        "{}-site_id".format(CLIENT_2["mac"]),
        suggested_object_id=CLIENT_2["hostname"],
        config_entry=config_entry,
    )

    await setup_unifi_integration(
        hass,
        ENTRY_CONFIG,
        options={unifi.CONF_BLOCK_CLIENT: True},
        sites=SITES,
        clients_response=[CLIENT_2],
        devices_response=[],
        clients_all_response=[CLIENT_1],
    )
    assert len(hass.states.async_all()) == 4

    device_1 = hass.states.get("device_tracker.client_1")
    assert device_1 is not None
async def setup_controller(hass, mock_controller):
    """Load the UniFi switch platform with the provided controller."""
    hass.config.components.add(unifi.DOMAIN)
    hass.data[unifi.DOMAIN] = {CONTROLLER_ID: mock_controller}
    config_entry = config_entries.ConfigEntry(
        1,
        unifi.DOMAIN,
        "Mock Title",
        ENTRY_CONFIG,
        "test",
        config_entries.CONN_CLASS_LOCAL_POLL,
        entry_id=1,
    )
    mock_controller.config_entry = config_entry

    await mock_controller.async_update()
    await hass.config_entries.async_forward_entry_setup(
        config_entry, device_tracker.DOMAIN)

    await hass.async_block_till_done()
Beispiel #29
0
async def test_storage_is_removed_on_config_entry_removal(hass, utcnow):
    """Test entity map storage is cleaned up on config entry removal."""
    bulb = create_lightbulb_service()
    await setup_test_component(hass, [bulb])

    hkid = '00:00:00:00:00:00'

    pairing_data = {
        'AccessoryPairingID': hkid,
    }

    entry = config_entries.ConfigEntry(1, 'homekit_controller', 'TestData',
                                       pairing_data, 'test',
                                       config_entries.CONN_CLASS_LOCAL_PUSH)

    assert hkid in hass.data[ENTITY_MAP].storage_data

    await async_remove_entry(hass, entry)

    assert hkid not in hass.data[ENTITY_MAP].storage_data
async def test_restoring_client(hass, aioclient_mock):
    """Test the update_items function with some clients."""
    config_entry = config_entries.ConfigEntry(
        version=1,
        domain=UNIFI_DOMAIN,
        title="Mock Title",
        data=ENTRY_CONFIG,
        source="test",
        connection_class=config_entries.CONN_CLASS_LOCAL_POLL,
        system_options={},
        options={},
        entry_id=1,
    )

    registry = await entity_registry.async_get_registry(hass)
    registry.async_get_or_create(
        TRACKER_DOMAIN,
        UNIFI_DOMAIN,
        f'{CLIENT_1["mac"]}-site_id',
        suggested_object_id=CLIENT_1["hostname"],
        config_entry=config_entry,
    )
    registry.async_get_or_create(
        TRACKER_DOMAIN,
        UNIFI_DOMAIN,
        f'{CLIENT_2["mac"]}-site_id',
        suggested_object_id=CLIENT_2["hostname"],
        config_entry=config_entry,
    )

    await setup_unifi_integration(
        hass,
        aioclient_mock,
        options={CONF_BLOCK_CLIENT: True},
        clients_response=[CLIENT_2],
        clients_all_response=[CLIENT_1],
    )
    assert len(hass.states.async_entity_ids(TRACKER_DOMAIN)) == 2

    device_1 = hass.states.get("device_tracker.client_1")
    assert device_1 is not None