예제 #1
0
async def test_options(opp: OpenPeerPower) -> None:
    """Test updating options."""
    entry = MockConfigEntry(domain=keenetic.DOMAIN, data=MOCK_DATA)
    entry.add_to_opp(opp)
    with patch("openpeerpower.components.keenetic_ndms2.async_setup_entry",
               return_value=True) as mock_setup_entry:
        await opp.config_entries.async_setup(entry.entry_id)
        await opp.async_block_till_done()

    assert len(mock_setup_entry.mock_calls) == 1

    # fake router
    opp.data.setdefault(keenetic.DOMAIN, {})
    opp.data[keenetic.DOMAIN][entry.entry_id] = {
        keenetic.ROUTER:
        Mock(client=Mock(get_interfaces=Mock(return_value=[
            InterfaceInfo.from_dict({
                "id": name,
                "type": "bridge"
            }) for name in MOCK_OPTIONS[const.CONF_INTERFACES]
        ])))
    }

    result = await opp.config_entries.options.async_init(entry.entry_id)

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "user"

    result2 = await opp.config_entries.options.async_configure(
        result["flow_id"],
        user_input=MOCK_OPTIONS,
    )

    assert result2["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
    assert result2["data"] == MOCK_OPTIONS
예제 #2
0
async def test_duplicate_bridge_import(opp):
    """Test that creating a bridge entry with a duplicate host errors."""

    entry_mock_data = {
        CONF_HOST: "1.1.1.1",
        CONF_KEYFILE: "",
        CONF_CERTFILE: "",
        CONF_CA_CERTS: "",
    }
    mock_entry = MockConfigEntry(domain=DOMAIN, data=entry_mock_data)
    mock_entry.add_to_opp(opp)

    with patch(
        "openpeerpower.components.lutron_caseta.async_setup_entry",
        return_value=True,
    ) as mock_setup_entry:
        # Mock entry added, try initializing flow with duplicate host
        result = await opp.config_entries.flow.async_init(
            DOMAIN,
            context={"source": config_entries.SOURCE_IMPORT},
            data=entry_mock_data,
        )

    assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
    assert result["reason"] == "already_configured"
    assert len(mock_setup_entry.mock_calls) == 0
예제 #3
0
async def test_climate_thermostat_schedule_hold_available(opp):
    """Test a thermostat with the schedule hold that is online."""
    mock_thermostat = _get_mock_thermostat_schedule_hold_available()
    mock_nuheat = _get_mock_nuheat(get_thermostat=mock_thermostat)

    with patch(
        "openpeerpower.components.nuheat.nuheat.NuHeat",
        return_value=mock_nuheat,
    ):
        config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG_ENTRY)
        config_entry.add_to_opp(opp)
        assert await opp.config_entries.async_setup(config_entry.entry_id)
        await opp.async_block_till_done()

    state = opp.states.get("climate.available_bathroom")

    assert state.state == "auto"
    expected_attributes = {
        "current_temperature": 38.9,
        "friendly_name": "Available bathroom",
        "hvac_action": "idle",
        "hvac_modes": ["auto", "heat"],
        "max_temp": 180.6,
        "min_temp": -6.1,
        "preset_mode": "Run Schedule",
        "preset_modes": ["Run Schedule", "Temporary Hold", "Permanent Hold"],
        "supported_features": 17,
        "temperature": 26.1,
    }
    # Only test for a subset of attributes in case
    # OPP changes the implementation and a new one appears
    assert all(item in state.attributes.items() for item in expected_attributes.items())
예제 #4
0
async def test_entity_device_info_with_identifier(opp, mqtt_mock):
    """Test MQTT switch device registry integration."""
    entry = MockConfigEntry(domain=mqtt.DOMAIN)
    entry.add_to_opp(opp)
    await async_start(opp, "openpeerpower", {}, entry)
    registry = await opp.helpers.device_registry.async_get_registry()

    data = json.dumps({
        "platform": "mqtt",
        "name": "Test 1",
        "state_topic": "test-topic",
        "command_topic": "test-command-topic",
        "device": {
            "identifiers": ["helloworld"],
            "connections": [["mac", "02:5b:26:a8:dc:12"]],
            "manufacturer": "Whatever",
            "name": "Beer",
            "model": "Glass",
            "sw_version": "0.1-beta",
        },
        "unique_id": "veryunique",
    })
    async_fire_mqtt_message(opp, "openpeerpower/switch/bla/config", data)
    await opp.async_block_till_done()

    device = registry.async_get_device({("mqtt", "helloworld")}, set())
    assert device is not None
    assert device.identifiers == {("mqtt", "helloworld")}
    assert device.connections == {("mac", "02:5b:26:a8:dc:12")}
    assert device.manufacturer == "Whatever"
    assert device.name == "Beer"
    assert device.model == "Glass"
    assert device.sw_version == "0.1-beta"
예제 #5
0
async def test_get_triggers(opp, device_reg, entity_reg):
    """Test we get the expected triggers from a light."""
    config_entry = MockConfigEntry(domain="test", data={})
    config_entry.add_to_opp(opp)
    device_entry = device_reg.async_get_or_create(
        config_entry_id=config_entry.entry_id,
        connections={(device_registry.CONNECTION_NETWORK_MAC,
                      "12:34:56:AB:CD:EF")},
    )
    entity_reg.async_get_or_create(DOMAIN,
                                   "test",
                                   "5678",
                                   device_id=device_entry.id)
    expected_triggers = [
        {
            "platform": "device",
            "domain": DOMAIN,
            "type": "turned_off",
            "device_id": device_entry.id,
            "entity_id": f"{DOMAIN}.test_5678",
        },
        {
            "platform": "device",
            "domain": DOMAIN,
            "type": "turned_on",
            "device_id": device_entry.id,
            "entity_id": f"{DOMAIN}.test_5678",
        },
    ]
    triggers = await async_get_device_automations(opp, "trigger",
                                                  device_entry.id)
    assert triggers == expected_triggers
예제 #6
0
async def test_config_passed_to_config_entry(opp):
    """Test that configured options for a host are loaded via config entry."""
    entry = MockConfigEntry(
        domain=konnected.DOMAIN,
        data={
            config_flow.CONF_ID: "aabbccddeeff",
            config_flow.CONF_HOST: "0.0.0.0"
        },
    )
    entry.add_to_opp(opp)
    with patch.object(konnected, "AlarmPanel", autospec=True) as mock_int:
        assert (await async_setup_component(
            opp,
            konnected.DOMAIN,
            {
                konnected.DOMAIN: {
                    konnected.CONF_ACCESS_TOKEN: "abcdefgh",
                    konnected.CONF_DEVICES: [{
                        konnected.CONF_ID: "aabbccddeeff"
                    }],
                }
            },
        ) is True)

    assert len(mock_int.mock_calls) == 3
    p_opp, p_entry = mock_int.mock_calls[0][1]

    assert p_opp is opp
    assert p_entry is entry
async def test_get_actions(opp, device_reg, entity_reg):
    """Test we get the expected actions from a NEW_DOMAIN."""
    config_entry = MockConfigEntry(domain="test", data={})
    config_entry.add_to_opp(opp)
    device_entry = device_reg.async_get_or_create(
        config_entry_id=config_entry.entry_id,
        connections={(device_registry.CONNECTION_NETWORK_MAC,
                      "12:34:56:AB:CD:EF")},
    )
    entity_reg.async_get_or_create(DOMAIN,
                                   "test",
                                   "5678",
                                   device_id=device_entry.id)
    expected_actions = [
        {
            "domain": DOMAIN,
            "type": "turn_on",
            "device_id": device_entry.id,
            "entity_id": "NEW_DOMAIN.test_5678",
        },
        {
            "domain": DOMAIN,
            "type": "turn_off",
            "device_id": device_entry.id,
            "entity_id": "NEW_DOMAIN.test_5678",
        },
    ]
    actions = await async_get_device_automations(opp, "action",
                                                 device_entry.id)
    assert_lists_same(actions, expected_actions)
예제 #8
0
async def setup_denonavr(opp):
    """Initialize media_player for tests."""
    entry_data = {
        CONF_HOST: TEST_HOST,
        CONF_MODEL: TEST_MODEL,
        CONF_TYPE: TEST_RECEIVER_TYPE,
        CONF_MANUFACTURER: TEST_MANUFACTURER,
        CONF_SERIAL_NUMBER: TEST_SERIALNUMBER,
    }

    mock_entry = MockConfigEntry(
        domain=DOMAIN,
        unique_id=TEST_UNIQUE_ID,
        data=entry_data,
    )

    mock_entry.add_to_opp(opp)

    await opp.config_entries.async_setup(mock_entry.entry_id)
    await opp.async_block_till_done()

    state = opp.states.get(ENTITY_ID)

    assert state
    assert state.name == TEST_NAME
예제 #9
0
async def test_form_ssdp_gets_form_with_ignored_entry(opp):
    """Test we can still setup if there is an ignored entry."""
    await setup.async_setup_component(opp, "persistent_notification", {})
    entry = MockConfigEntry(
        domain=UNIFI_DOMAIN,
        data={"not_controller_key": None},
        source=config_entries.SOURCE_IGNORE,
    )
    entry.add_to_opp(opp)
    result = await opp.config_entries.flow.async_init(
        UNIFI_DOMAIN,
        context={"source": config_entries.SOURCE_SSDP},
        data={
            "friendlyName": "UniFi Dream Machine New",
            "modelDescription": "UniFi Dream Machine Pro",
            "ssdp_location": "http://1.2.3.4:41417/rootDesc.xml",
            "serialNumber": "e0:63:da:20:14:a9",
        },
    )
    assert result["type"] == "form"
    assert result["step_id"] == "user"
    assert result["errors"] == {}
    context = next(flow["context"]
                   for flow in opp.config_entries.flow.async_progress()
                   if flow["flow_id"] == result["flow_id"])
    assert context["title_placeholders"] == {
        "host": "1.2.3.4",
        "site": "default",
    }
예제 #10
0
async def test_setup_entry_with_tracing(opp: OpenPeerPower) -> None:
    """Test integration setup from entry with tracing enabled."""
    entry = MockConfigEntry(
        domain=DOMAIN,
        data={CONF_DSN: "http://[email protected]/1"},
        options={CONF_TRACING: True, CONF_TRACING_SAMPLE_RATE: 0.5},
    )
    entry.add_to_opp(opp)

    with patch("openpeerpower.components.sentry.AioHttpIntegration"), patch(
        "openpeerpower.components.sentry.SqlalchemyIntegration"
    ), patch("openpeerpower.components.sentry.LoggingIntegration"), patch(
        "openpeerpower.components.sentry.sentry_sdk"
    ) as sentry_mock:
        assert await opp.config_entries.async_setup(entry.entry_id)
        await opp.async_block_till_done()

    call_args = sentry_mock.init.call_args[1]
    assert set(call_args) == {
        "dsn",
        "environment",
        "integrations",
        "release",
        "before_send",
        "traces_sample_rate",
    }
    assert call_args["traces_sample_rate"] == 0.5
예제 #11
0
async def test_set_convert_unique_id_to_string(opp: OpenPeerPower) -> None:
    """Test upgrading configs to use a unique id."""
    config_entry = MockConfigEntry(
        domain=DOMAIN,
        data={
            "token": {"userid": 1234},
            "auth_implementation": "withings",
            "profile": "person0",
        },
    )
    config_entry.add_to_opp(opp)

    opp_config = {
        HA_DOMAIN: {
            CONF_UNIT_SYSTEM: CONF_UNIT_SYSTEM_METRIC,
            CONF_EXTERNAL_URL: "http://127.0.0.1:8080/",
        },
        const.DOMAIN: {
            CONF_CLIENT_ID: "my_client_id",
            CONF_CLIENT_SECRET: "my_client_secret",
            const.CONF_USE_WEBHOOK: False,
        },
    }

    with patch(
        "openpeerpower.components.withings.common.ConfigEntryWithingsApi",
        spec=ConfigEntryWithingsApi,
    ):
        await async_process_op_core_config(opp, opp_config.get(HA_DOMAIN))
        assert await async_setup_component(opp, HA_DOMAIN, {})
        assert await async_setup_component(opp, webhook.DOMAIN, opp_config)
        assert await async_setup_component(opp, const.DOMAIN, opp_config)
        await opp.async_block_till_done()

        assert config_entry.unique_id == "1234"
예제 #12
0
async def test_form_homekit(opp):
    """Test that we abort from homekit if tado is already setup."""
    await setup.async_setup_component(opp, "persistent_notification", {})

    result = await opp.config_entries.flow.async_init(
        DOMAIN,
        context={"source": config_entries.SOURCE_HOMEKIT},
        data={"properties": {
            "id": "AA:BB:CC:DD:EE:FF"
        }},
    )
    assert result["type"] == "form"
    assert result["errors"] == {}
    flow = next(flow for flow in opp.config_entries.flow.async_progress()
                if flow["flow_id"] == result["flow_id"])
    assert flow["context"]["unique_id"] == "AA:BB:CC:DD:EE:FF"

    entry = MockConfigEntry(domain=DOMAIN,
                            data={
                                CONF_USERNAME: "******",
                                CONF_PASSWORD: "******"
                            })
    entry.add_to_opp(opp)

    result = await opp.config_entries.flow.async_init(
        DOMAIN,
        context={"source": config_entries.SOURCE_HOMEKIT},
        data={"properties": {
            "id": "AA:BB:CC:DD:EE:FF"
        }},
    )
    assert result["type"] == "abort"
예제 #13
0
async def test_get_trigger_capabilities_set_tilt_pos(
        opp, device_reg, entity_reg, enable_custom_integrations):
    """Test we get the expected capabilities from a cover trigger."""
    platform = getattr(opp.components, f"test.{DOMAIN}")
    platform.init()
    ent = platform.ENTITIES[2]

    config_entry = MockConfigEntry(domain="test", data={})
    config_entry.add_to_opp(opp)
    device_entry = device_reg.async_get_or_create(
        config_entry_id=config_entry.entry_id,
        connections={(device_registry.CONNECTION_NETWORK_MAC,
                      "12:34:56:AB:CD:EF")},
    )
    entity_reg.async_get_or_create(DOMAIN,
                                   "test",
                                   ent.unique_id,
                                   device_id=device_entry.id)

    assert await async_setup_component(opp, DOMAIN,
                                       {DOMAIN: {
                                           CONF_PLATFORM: "test"
                                       }})

    expected_capabilities = {
        "extra_fields": [
            {
                "name": "above",
                "optional": True,
                "type": "integer",
                "default": 0,
                "valueMax": 100,
                "valueMin": 0,
            },
            {
                "name": "below",
                "optional": True,
                "type": "integer",
                "default": 100,
                "valueMax": 100,
                "valueMin": 0,
            },
        ]
    }
    triggers = await async_get_device_automations(opp, "trigger",
                                                  device_entry.id)
    assert len(triggers) == 5
    for trigger in triggers:
        capabilities = await async_get_device_automation_capabilities(
            opp, "trigger", trigger)
        if trigger["type"] == "tilt_position":
            assert capabilities == expected_capabilities
        else:
            assert capabilities == {
                "extra_fields": [{
                    "name": "for",
                    "optional": True,
                    "type": "positive_time_period_dict",
                }]
            }
예제 #14
0
async def test_get_trigger_capabilities_none(opp, device_reg, entity_reg):
    """Test we get the expected capabilities from a sensor trigger."""
    platform = getattr(opp.components, f"test.{DOMAIN}")
    platform.init()

    config_entry = MockConfigEntry(domain="test", data={})
    config_entry.add_to_opp(opp)

    assert await async_setup_component(opp, DOMAIN,
                                       {DOMAIN: {
                                           CONF_PLATFORM: "test"
                                       }})

    triggers = [
        {
            "platform": "device",
            "device_id": "8770c43885354d5fa27604db6817f63f",
            "domain": "sensor",
            "entity_id": "sensor.beer",
            "type": "is_battery_level",
        },
        {
            "platform": "device",
            "device_id": "8770c43885354d5fa27604db6817f63f",
            "domain": "sensor",
            "entity_id": platform.ENTITIES["none"].entity_id,
            "type": "is_battery_level",
        },
    ]

    expected_capabilities = {}
    for trigger in triggers:
        capabilities = await async_get_device_automation_capabilities(
            opp, "trigger", trigger)
        assert capabilities == expected_capabilities
예제 #15
0
async def test_discovery_updates_unique_id(opp):
    """Test a duplicate discovery id aborts and updates existing entry."""
    entry = MockConfigEntry(
        domain=DOMAIN,
        unique_id=TEST_DISCOVERY_RESULT["id"],
        data={
            "host": "dummy",
            "port": 11,
            "name": "dummy",
            "id": TEST_DISCOVERY_RESULT["id"],
        },
        state=config_entries.ConfigEntryState.SETUP_RETRY,
    )

    entry.add_to_opp(opp)

    with patch(
            "openpeerpower.components.volumio.async_setup_entry",
            return_value=True,
    ) as mock_setup_entry:
        result = await opp.config_entries.flow.async_init(
            DOMAIN,
            context={"source": config_entries.SOURCE_ZEROCONF},
            data=TEST_DISCOVERY,
        )
        await opp.async_block_till_done()

    assert result["type"] == "abort"
    assert result["reason"] == "already_configured"

    assert entry.data == TEST_DISCOVERY_RESULT
    assert len(mock_setup_entry.mock_calls) == 1
예제 #16
0
async def test_option_flow(opp):
    """Test option flow."""
    entry = MockConfigEntry(domain=DOMAIN, data=DATA)
    entry.add_to_opp(opp)

    assert not entry.options

    with patch("openpeerpower.components.aurora.async_setup_entry",
               return_value=True):
        await opp.config_entries.async_setup(entry.entry_id)
        await opp.async_block_till_done()
        result = await opp.config_entries.options.async_init(
            entry.entry_id,
            data=None,
        )

    assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
    assert result["step_id"] == "init"

    result = await opp.config_entries.options.async_configure(
        result["flow_id"],
        user_input={"forecast_threshold": 65},
    )

    assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
    assert result["title"] == ""
    assert result["data"]["forecast_threshold"] == 65
예제 #17
0
async def test_form_updates_unique_id(opp):
    """Test a duplicate id aborts and updates existing entry."""
    entry = MockConfigEntry(
        domain=DOMAIN,
        unique_id=TEST_SYSTEM_INFO["id"],
        data={
            "host": "dummy",
            "port": 11,
            "name": "dummy",
            "id": TEST_SYSTEM_INFO["id"],
        },
    )

    entry.add_to_opp(opp)

    result = await opp.config_entries.flow.async_init(
        DOMAIN, context={"source": config_entries.SOURCE_USER})
    with patch(
            "openpeerpower.components.volumio.config_flow.Volumio.get_system_info",
            return_value=TEST_SYSTEM_INFO,
    ), patch(
            "openpeerpower.components.volumio.async_setup_entry",
            return_value=True,
    ):
        result2 = await opp.config_entries.flow.async_configure(
            result["flow_id"],
            TEST_CONNECTION,
        )
        await opp.async_block_till_done()

    assert result2["type"] == "abort"
    assert result2["reason"] == "already_configured"

    assert entry.data == {**TEST_SYSTEM_INFO, **TEST_CONNECTION}
예제 #18
0
async def test_get_actions_arm_night_only(opp, device_reg, entity_reg):
    """Test we get the expected actions from a alarm_control_panel."""
    config_entry = MockConfigEntry(domain="test", data={})
    config_entry.add_to_opp(opp)
    device_entry = device_reg.async_get_or_create(
        config_entry_id=config_entry.entry_id,
        connections={(device_registry.CONNECTION_NETWORK_MAC,
                      "12:34:56:AB:CD:EF")},
    )
    entity_reg.async_get_or_create(DOMAIN,
                                   "test",
                                   "5678",
                                   device_id=device_entry.id)
    opp.states.async_set("alarm_control_panel.test_5678", "attributes",
                         {"supported_features": 4})
    expected_actions = [
        {
            "domain": DOMAIN,
            "type": "arm_night",
            "device_id": device_entry.id,
            "entity_id": "alarm_control_panel.test_5678",
        },
        {
            "domain": DOMAIN,
            "type": "disarm",
            "device_id": device_entry.id,
            "entity_id": "alarm_control_panel.test_5678",
        },
    ]
    actions = await async_get_device_automations(opp, "action",
                                                 device_entry.id)
    assert_lists_same(actions, expected_actions)
예제 #19
0
async def test_entry_setup_unload(opp, aioclient_mock):
    """Test integration setup and unload."""
    entry = MockConfigEntry(
        domain=DOMAIN,
        unique_id="test-api-user",
        data={
            "api_user": "******",
            "api_key": "test-api-key",
            "url": DEFAULT_URL,
        },
    )
    entry.add_to_opp(opp)

    aioclient_mock.get(
        "https://habitica.com/api/v3/user",
        json={"data": {"api_user": "******", "profile": {"name": "test_user"}}},
    )

    assert await opp.config_entries.async_setup(entry.entry_id)
    await opp.async_block_till_done()

    assert opp.services.has_service(DOMAIN, SERVICE_API_CALL)

    assert await opp.config_entries.async_unload(entry.entry_id)

    assert not opp.services.has_service(DOMAIN, SERVICE_API_CALL)
예제 #20
0
async def test_set_up_oauth_remote_url(opp, aioclient_mock):
    """Test we set up Almond to connect to HA if we have external url."""
    entry = MockConfigEntry(
        domain="almond",
        data={
            "type": const.TYPE_OAUTH2,
            "auth_implementation": "local",
            "host": "http://localhost:9999",
            "token": {"expires_at": time() + 1000, "access_token": "abcd"},
        },
    )
    entry.add_to_opp(opp)

    with patch(
        "openpeerpower.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation",
        return_value=mock_coro(),
    ):
        assert await async_setup_component(opp, "almond", {})

    assert entry.state == config_entries.ENTRY_STATE_LOADED

    with patch("openpeerpower.components.almond.ALMOND_SETUP_DELAY", 0), patch(
        "openpeerpower.helpers.network.async_get_external_url",
        return_value="https://example.nabu.casa",
    ), patch(
        "pyalmond.WebAlmondAPI.async_create_device", return_value=mock_coro()
    ) as mock_create_device:
        opp.bus.async_fire(EVENT_OPENPEERPOWER_START)
        await opp.async_block_till_done()
        async_fire_time_changed(opp, utcnow())
        await opp.async_block_till_done()

    assert len(mock_create_device.mock_calls) == 1
예제 #21
0
async def init_integration(opp) -> MockConfigEntry:
    """Set up the Kodi integration in Open Peer Power."""
    entry_data = {
        CONF_NAME: "name",
        CONF_HOST: "1.1.1.1",
        CONF_PORT: 8080,
        CONF_WS_PORT: 9090,
        CONF_USERNAME: "******",
        CONF_PASSWORD: "******",
        CONF_SSL: False,
    }
    entry = MockConfigEntry(domain=DOMAIN, data=entry_data, title="name")
    entry.add_to_opp(opp)

    with patch(
            "openpeerpower.components.kodi.Kodi.ping", return_value=True
    ), patch(
            "openpeerpower.components.kodi.Kodi.get_application_properties",
            return_value={"version": {
                "major": 1,
                "minor": 1
            }},
    ), patch(
            "openpeerpower.components.kodi.get_kodi_connection",
            return_value=MockConnection(),
    ):
        await opp.config_entries.async_setup(entry.entry_id)
        await opp.async_block_till_done()

    return entry
예제 #22
0
async def test_setup_unload_entry(opp, mock_remote):
    """Test if config entry is unloaded."""
    mock_entry = MockConfigEntry(
        domain=DOMAIN, unique_id=MOCK_DEVICE_INFO[ATTR_UDN], data=MOCK_CONFIG_DATA
    )

    mock_entry.add_to_opp(opp)

    await opp.config_entries.async_setup(mock_entry.entry_id)
    await opp.async_block_till_done()

    await opp.config_entries.async_unload(mock_entry.entry_id)
    assert mock_entry.state is ConfigEntryState.NOT_LOADED

    state_tv = opp.states.get("media_player.panasonic_viera_tv")
    state_remote = opp.states.get("remote.panasonic_viera_tv")

    assert state_tv.state == STATE_UNAVAILABLE
    assert state_remote.state == STATE_UNAVAILABLE

    await opp.config_entries.async_remove(mock_entry.entry_id)
    await opp.async_block_till_done()

    state_tv = opp.states.get("media_player.panasonic_viera_tv")
    state_remote = opp.states.get("remote.panasonic_viera_tv")

    assert state_tv is None
    assert state_remote is None
예제 #23
0
async def test_options_flow(opp):
    """Test config flow options."""
    geography_conf = {
        CONF_API_KEY: "abcde12345",
        CONF_LATITUDE: 51.528308,
        CONF_LONGITUDE: -0.3817765,
    }

    config_entry = MockConfigEntry(
        domain=DOMAIN,
        unique_id="51.528308, -0.3817765",
        data=geography_conf,
        options={CONF_SHOW_ON_MAP: True},
    )
    config_entry.add_to_opp(opp)

    with patch("openpeerpower.components.airvisual.async_setup_entry",
               return_value=True):
        await opp.config_entries.async_setup(config_entry.entry_id)
        result = await opp.config_entries.options.async_init(
            config_entry.entry_id)

        assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
        assert result["step_id"] == "init"

        result = await opp.config_entries.options.async_configure(
            result["flow_id"], user_input={CONF_SHOW_ON_MAP: False})

        assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
        assert config_entry.options == {CONF_SHOW_ON_MAP: False}
예제 #24
0
async def test_setup_entry_encrypted_missing_device_info_none(opp):
    """Test setup with encrypted config entry and device info set to None."""
    mock_entry = MockConfigEntry(
        domain=DOMAIN,
        unique_id=MOCK_CONFIG_DATA[CONF_HOST],
        data={**MOCK_CONFIG_DATA, **MOCK_ENCRYPTION_DATA},
    )

    mock_entry.add_to_opp(opp)

    mock_remote = get_mock_remote(device_info=None)

    with patch(
        "openpeerpower.components.panasonic_viera.RemoteControl",
        return_value=mock_remote,
    ):
        await opp.config_entries.async_setup(mock_entry.entry_id)
        await opp.async_block_till_done()

        assert mock_entry.data[ATTR_DEVICE_INFO] is None
        assert mock_entry.unique_id == MOCK_CONFIG_DATA[CONF_HOST]

        state_tv = opp.states.get("media_player.panasonic_viera_tv")
        state_remote = opp.states.get("remote.panasonic_viera_tv")

        assert state_tv
        assert state_tv.name == DEFAULT_NAME

        assert state_remote
        assert state_remote.name == DEFAULT_NAME
예제 #25
0
async def test_get_trigger_capabilities(opp, device_reg, entity_reg):
    """Test we get the expected capabilities from a light trigger."""
    config_entry = MockConfigEntry(domain="test", data={})
    config_entry.add_to_opp(opp)
    device_entry = device_reg.async_get_or_create(
        config_entry_id=config_entry.entry_id,
        connections={(device_registry.CONNECTION_NETWORK_MAC,
                      "12:34:56:AB:CD:EF")},
    )
    entity_reg.async_get_or_create(DOMAIN,
                                   "test",
                                   "5678",
                                   device_id=device_entry.id)
    expected_capabilities = {
        "extra_fields": [{
            "name": "for",
            "optional": True,
            "type": "positive_time_period_dict"
        }]
    }
    triggers = await async_get_device_automations(opp, "trigger",
                                                  device_entry.id)
    for trigger in triggers:
        capabilities = await async_get_device_automation_capabilities(
            opp, "trigger", trigger)
        assert capabilities == expected_capabilities
예제 #26
0
async def test_restoring_devices(opp):
    """Test restoring existing device_tracker entities if not detected on startup."""
    config_entry = MockConfigEntry(domain=mikrotik.DOMAIN,
                                   data=MOCK_DATA,
                                   options=MOCK_OPTIONS)
    config_entry.add_to_opp(opp)

    registry = er.async_get(opp)
    registry.async_get_or_create(
        device_tracker.DOMAIN,
        mikrotik.DOMAIN,
        "00:00:00:00:00:01",
        suggested_object_id="device_1",
        config_entry=config_entry,
    )
    registry.async_get_or_create(
        device_tracker.DOMAIN,
        mikrotik.DOMAIN,
        "00:00:00:00:00:02",
        suggested_object_id="device_2",
        config_entry=config_entry,
    )

    await setup_mikrotik_entry(opp)

    # test device_2 which is not in wireless list is restored
    device_1 = opp.states.get("device_tracker.device_1")
    assert device_1 is not None
    assert device_1.state == "home"
    device_2 = opp.states.get("device_tracker.device_2")
    assert device_2 is not None
    assert device_2.state == "not_home"
예제 #27
0
async def test_device_info_gogogate2(gogogate2api_mock,
                                     opp: OpenPeerPower) -> None:
    """Test device info."""
    device_registry = mock_device_registry(opp)

    closed_door_response = _mocked_gogogate_open_door_response()

    api = MagicMock(GogoGate2Api)
    api.async_info.return_value = closed_door_response
    gogogate2api_mock.return_value = api

    config_entry = MockConfigEntry(
        domain=DOMAIN,
        source=SOURCE_USER,
        title="mycontroller",
        unique_id="xyz",
        data={
            CONF_DEVICE: DEVICE_TYPE_GOGOGATE2,
            CONF_IP_ADDRESS: "127.0.0.1",
            CONF_USERNAME: "******",
            CONF_PASSWORD: "******",
        },
    )
    config_entry.add_to_opp(opp)
    assert await opp.config_entries.async_setup(config_entry.entry_id)
    await opp.async_block_till_done()

    device = device_registry.async_get_device({(DOMAIN, "xyz")})
    assert device
    assert device.manufacturer == MANUFACTURER
    assert device.name == "mycontroller"
    assert device.model == "gogogate2"
    assert device.sw_version == "222"
예제 #28
0
async def test_no_aid_collision(opp, device_reg, entity_reg):
    """Test generating aids."""
    config_entry = MockConfigEntry(domain="test", data={})
    config_entry.add_to_opp(opp)
    device_entry = device_reg.async_get_or_create(
        config_entry_id=config_entry.entry_id,
        connections={(device_registry.CONNECTION_NETWORK_MAC,
                      "12:34:56:AB:CD:EF")},
    )

    with patch(
            "openpeerpower.components.homekit.aidmanager.AccessoryAidStorage.async_schedule_save"
    ):
        aid_storage = AccessoryAidStorage(opp, config_entry)
    await aid_storage.async_initialize()

    seen_aids = set()

    for unique_id in range(0, 202):
        ent = entity_reg.async_get_or_create("light",
                                             "device",
                                             unique_id,
                                             device_id=device_entry.id)
        opp.states.async_set(ent.entity_id, "on")
        aid = aid_storage.get_or_allocate_aid_for_entity_id(ent.entity_id)
        assert aid not in seen_aids
        seen_aids.add(aid)
예제 #29
0
async def test_setup_entry_error(opp: OpenPeerPower):
    """Test for successfully setting a config entry."""
    with patch("huisbaasje.Huisbaasje.authenticate",
               side_effect=HuisbaasjeException) as mock_authenticate:
        opp.config.components.add(huisbaasje.DOMAIN)
        config_entry = MockConfigEntry(
            version=1,
            domain=huisbaasje.DOMAIN,
            title="userId",
            data={
                CONF_ID: "userId",
                CONF_USERNAME: "******",
                CONF_PASSWORD: "******",
            },
            source="test",
        )
        config_entry.add_to_opp(opp)

        assert config_entry.state is ConfigEntryState.NOT_LOADED
        await opp.config_entries.async_setup(config_entry.entry_id)
        await opp.async_block_till_done()

        # Assert integration is loaded with error
        assert config_entry.state is ConfigEntryState.SETUP_ERROR
        assert huisbaasje.DOMAIN not in opp.data

        # Assert entities are not loaded
        entities = opp.states.async_entity_ids("sensor")
        assert len(entities) == 0

        # Assert mocks are called
        assert len(mock_authenticate.mock_calls) == 1
예제 #30
0
async def test_state(opp):
    """Test state of the entity."""
    mocked_device = _create_mocked_device()
    entry = MockConfigEntry(domain=songpal.DOMAIN, data=CONF_DATA)
    entry.add_to_opp(opp)

    with _patch_media_player_device(mocked_device):
        await opp.config_entries.async_setup(entry.entry_id)
        await opp.async_block_till_done()

    state = opp.states.get(ENTITY_ID)
    assert state.name == FRIENDLY_NAME
    assert state.state == STATE_ON
    attributes = state.as_dict()["attributes"]
    assert attributes["volume_level"] == 0.5
    assert attributes["is_volume_muted"] is False
    assert attributes["source_list"] == ["title1", "title2"]
    assert attributes["source"] == "title2"
    assert attributes["supported_features"] == SUPPORT_SONGPAL

    device_registry = dr.async_get(opp)
    device = device_registry.async_get_device(identifiers={(songpal.DOMAIN,
                                                            MAC)})
    assert device.connections == {(dr.CONNECTION_NETWORK_MAC, MAC)}
    assert device.manufacturer == "Sony Corporation"
    assert device.name == FRIENDLY_NAME
    assert device.sw_version == SW_VERSION
    assert device.model == MODEL

    entity_registry = er.async_get(opp)
    entity = entity_registry.async_get(ENTITY_ID)
    assert entity.unique_id == MAC