コード例 #1
0
async def test_all_entities(hass: HomeAssistant,
                            component_factory: ComponentFactory) -> None:
    """Test all entities."""
    entity_registry: EntityRegistry = await hass.helpers.entity_registry.async_get_registry(
    )

    with patch(
            "homeassistant.components.withings.sensor.BaseWithingsSensor.entity_registry_enabled_default"
    ) as enabled_by_default_mock:
        enabled_by_default_mock.return_value = True

        await component_factory.configure_component(profile_configs=(PERSON0, )
                                                    )

        # Assert entities should not exist yet.
        for attribute in get_platform_attributes(SENSOR_DOMAIN):
            assert not await async_get_entity_id(hass, attribute,
                                                 PERSON0.user_id)

        # person 0
        await component_factory.setup_profile(PERSON0.user_id)

        # Assert entities should exist.
        for attribute in get_platform_attributes(SENSOR_DOMAIN):
            entity_id = await async_get_entity_id(hass, attribute,
                                                  PERSON0.user_id)
            assert entity_id
            assert entity_registry.async_is_registered(entity_id)

        resp = await component_factory.call_webhook(PERSON0.user_id,
                                                    NotifyAppli.SLEEP)
        assert resp.message_code == 0

        resp = await component_factory.call_webhook(PERSON0.user_id,
                                                    NotifyAppli.WEIGHT)
        assert resp.message_code == 0

        for person, measurement, expected in EXPECTED_DATA:
            attribute = WITHINGS_MEASUREMENTS_MAP[measurement]
            entity_id = await async_get_entity_id(hass, attribute,
                                                  person.user_id)
            state_obj = hass.states.get(entity_id)

            async_assert_state_equals(entity_id, state_obj, expected,
                                      attribute)

    # Unload
    await component_factory.unload(PERSON0)
コード例 #2
0
async def test_sensor_default_enabled_entities(
        hass: HomeAssistant, component_factory: ComponentFactory,
        current_request_with_host) -> None:
    """Test entities enabled by default."""
    entity_registry: EntityRegistry = er.async_get(hass)

    await component_factory.configure_component(profile_configs=(PERSON0, ))

    # Assert entities should not exist yet.
    for attribute in get_platform_attributes(SENSOR_DOMAIN):
        assert not await async_get_entity_id(hass, attribute, PERSON0.user_id)

    # person 0
    await component_factory.setup_profile(PERSON0.user_id)

    # Assert entities should exist.
    for attribute in get_platform_attributes(SENSOR_DOMAIN):
        entity_id = await async_get_entity_id(hass, attribute, PERSON0.user_id)
        assert entity_id
        assert entity_registry.async_is_registered(entity_id)

    resp = await component_factory.call_webhook(PERSON0.user_id,
                                                NotifyAppli.SLEEP)
    assert resp.message_code == 0

    resp = await component_factory.call_webhook(PERSON0.user_id,
                                                NotifyAppli.WEIGHT)
    assert resp.message_code == 0

    for person, measurement, expected in EXPECTED_DATA:
        attribute = WITHINGS_MEASUREMENTS_MAP[measurement]
        entity_id = await async_get_entity_id(hass, attribute, person.user_id)
        state_obj = hass.states.get(entity_id)

        if attribute.enabled_by_default:
            async_assert_state_equals(entity_id, state_obj, expected,
                                      attribute)
        else:
            assert state_obj is None

    # Unload
    await component_factory.unload(PERSON0)