示例#1
0
def test_entity_registry_items():
    """Test the EntityRegistryItems container."""
    entities = er.EntityRegistryItems()
    assert entities.get_entity_id(("a", "b", "c")) is None
    assert entities.get_entry("abc") is None

    entry1 = er.RegistryEntry("test.entity1", "1234", "hue")
    entry2 = er.RegistryEntry("test.entity2", "2345", "hue")
    entities["test.entity1"] = entry1
    entities["test.entity2"] = entry2

    assert entities["test.entity1"] is entry1
    assert entities["test.entity2"] is entry2

    assert entities.get_entity_id(("test", "hue", "1234")) is entry1.entity_id
    assert entities.get_entry(entry1.id) is entry1
    assert entities.get_entity_id(("test", "hue", "2345")) is entry2.entity_id
    assert entities.get_entry(entry2.id) is entry2

    entities.pop("test.entity1")
    del entities["test.entity2"]

    assert entities.get_entity_id(("test", "hue", "1234")) is None
    assert entities.get_entry(entry1.id) is None
    assert entities.get_entity_id(("test", "hue", "2345")) is None
    assert entities.get_entry(entry2.id) is None
async def test_unique_id_migration(mock_hc, hass, mock_write_config):
    """Test migration of switch unique ids to stable ones."""
    entry = MockConfigEntry(domain=DOMAIN,
                            data={
                                CONF_HOST: "192.0.2.0",
                                CONF_NAME: HUB_NAME
                            })

    entry.add_to_hass(hass)
    mock_registry(
        hass,
        {
            # old format
            ENTITY_WATCH_TV:
            er.RegistryEntry(
                entity_id=ENTITY_WATCH_TV,
                unique_id="123443-Watch TV",
                platform="harmony",
                config_entry_id=entry.entry_id,
            ),
            # old format, activity name with -
            ENTITY_NILE_TV:
            er.RegistryEntry(
                entity_id=ENTITY_NILE_TV,
                unique_id="123443-Nile-TV",
                platform="harmony",
                config_entry_id=entry.entry_id,
            ),
            # new format
            ENTITY_PLAY_MUSIC:
            er.RegistryEntry(
                entity_id=ENTITY_PLAY_MUSIC,
                unique_id=f"activity_{PLAY_MUSIC_ACTIVITY_ID}",
                platform="harmony",
                config_entry_id=entry.entry_id,
            ),
            # old entity which no longer has a matching activity on the hub. skipped.
            "switch.some_other_activity":
            er.RegistryEntry(
                entity_id="switch.some_other_activity",
                unique_id="123443-Some Other Activity",
                platform="harmony",
                config_entry_id=entry.entry_id,
            ),
        },
    )
    assert await async_setup_component(hass, DOMAIN, {})
    await hass.async_block_till_done()

    ent_reg = er.async_get(hass)

    switch_tv = ent_reg.async_get(ENTITY_WATCH_TV)
    assert switch_tv.unique_id == f"activity_{WATCH_TV_ACTIVITY_ID}"

    switch_nile = ent_reg.async_get(ENTITY_NILE_TV)
    assert switch_nile.unique_id == f"activity_{NILE_TV_ACTIVITY_ID}"

    switch_music = ent_reg.async_get(ENTITY_PLAY_MUSIC)
    assert switch_music.unique_id == f"activity_{PLAY_MUSIC_ACTIVITY_ID}"
async def test_config_entry_migration(hass):
    """Tests config entry without mode in unique_id can be migrated."""
    ipma_entry = MockConfigEntry(
        domain=DOMAIN,
        title="Home",
        data={
            CONF_LATITUDE: 0,
            CONF_LONGITUDE: 0,
            CONF_MODE: "daily"
        },
    )
    ipma_entry.add_to_hass(hass)

    ipma_entry2 = MockConfigEntry(
        domain=DOMAIN,
        title="Home",
        data={
            CONF_LATITUDE: 0,
            CONF_LONGITUDE: 0,
            CONF_MODE: "hourly"
        },
    )
    ipma_entry2.add_to_hass(hass)

    mock_registry(
        hass,
        {
            "weather.hometown":
            entity_registry.RegistryEntry(
                entity_id="weather.hometown",
                unique_id="0, 0",
                platform="ipma",
                config_entry_id=ipma_entry.entry_id,
            ),
            "weather.hometown_2":
            entity_registry.RegistryEntry(
                entity_id="weather.hometown_2",
                unique_id="0, 0, hourly",
                platform="ipma",
                config_entry_id=ipma_entry.entry_id,
            ),
        },
    )

    with patch(
            "homeassistant.components.ipma.weather.async_get_location",
            return_value=MockLocation(),
    ):
        assert await async_setup_component(hass, DOMAIN, {})
        await hass.async_block_till_done()

        ent_reg = await entity_registry.async_get_registry(hass)

        weather_home = ent_reg.async_get("weather.hometown")
        assert weather_home.unique_id == "0, 0, daily"

        weather_home2 = ent_reg.async_get("weather.hometown_2")
        assert weather_home2.unique_id == "0, 0, hourly"
示例#4
0
async def test_extract_entity_ids_from_area(hass):
    """Test extract_entity_ids method with areas."""
    hass.states.async_set("light.Bowl", STATE_ON)
    hass.states.async_set("light.Ceiling", STATE_OFF)
    hass.states.async_set("light.Kitchen", STATE_OFF)

    device_in_area = dev_reg.DeviceEntry(area_id="test-area")
    device_no_area = dev_reg.DeviceEntry()
    device_diff_area = dev_reg.DeviceEntry(area_id="diff-area")

    mock_device_registry(
        hass,
        {
            device_in_area.id: device_in_area,
            device_no_area.id: device_no_area,
            device_diff_area.id: device_diff_area,
        },
    )

    entity_in_area = ent_reg.RegistryEntry(
        entity_id="light.in_area",
        unique_id="in-area-id",
        platform="test",
        device_id=device_in_area.id,
    )
    entity_no_area = ent_reg.RegistryEntry(
        entity_id="light.no_area",
        unique_id="no-area-id",
        platform="test",
        device_id=device_no_area.id,
    )
    entity_diff_area = ent_reg.RegistryEntry(
        entity_id="light.diff_area",
        unique_id="diff-area-id",
        platform="test",
        device_id=device_diff_area.id,
    )
    mock_registry(
        hass,
        {
            entity_in_area.entity_id: entity_in_area,
            entity_no_area.entity_id: entity_no_area,
            entity_diff_area.entity_id: entity_diff_area,
        },
    )

    call = ha.ServiceCall("light", "turn_on", {"area_id": "test-area"})

    assert {"light.in_area"
            } == await service.async_extract_entity_ids(hass, call)

    call = ha.ServiceCall("light", "turn_on",
                          {"area_id": ["test-area", "diff-area"]})

    assert {
        "light.in_area",
        "light.diff_area",
    } == await service.async_extract_entity_ids(hass, call)
示例#5
0
async def test_extract_entity_ids_from_area(hass):
    """Test extract_entity_ids method with areas."""
    hass.states.async_set('light.Bowl', STATE_ON)
    hass.states.async_set('light.Ceiling', STATE_OFF)
    hass.states.async_set('light.Kitchen', STATE_OFF)

    device_in_area = dev_reg.DeviceEntry(area_id='test-area')
    device_no_area = dev_reg.DeviceEntry()
    device_diff_area = dev_reg.DeviceEntry(area_id='diff-area')

    mock_device_registry(
        hass, {
            device_in_area.id: device_in_area,
            device_no_area.id: device_no_area,
            device_diff_area.id: device_diff_area,
        })

    entity_in_area = ent_reg.RegistryEntry(
        entity_id='light.in_area',
        unique_id='in-area-id',
        platform='test',
        device_id=device_in_area.id,
    )
    entity_no_area = ent_reg.RegistryEntry(
        entity_id='light.no_area',
        unique_id='no-area-id',
        platform='test',
        device_id=device_no_area.id,
    )
    entity_diff_area = ent_reg.RegistryEntry(
        entity_id='light.diff_area',
        unique_id='diff-area-id',
        platform='test',
        device_id=device_diff_area.id,
    )
    mock_registry(
        hass, {
            entity_in_area.entity_id: entity_in_area,
            entity_no_area.entity_id: entity_no_area,
            entity_diff_area.entity_id: entity_diff_area,
        })

    call = ha.ServiceCall('light', 'turn_on', {'area_id': 'test-area'})

    assert {'light.in_area'} == \
        await service.async_extract_entity_ids(hass, call)

    call = ha.ServiceCall('light', 'turn_on',
                          {'area_id': ['test-area', 'diff-area']})

    assert {'light.in_area', 'light.diff_area'} == \
        await service.async_extract_entity_ids(hass, call)
示例#6
0
文件: test_init.py 项目: rikroe/core
async def test_unique_id_migration(hass: HomeAssistant,
                                   mock_asyncsleepiq) -> None:
    """Test migration of sensor unique IDs."""

    mock_entry = MockConfigEntry(
        domain=DOMAIN,
        data=SLEEPIQ_CONFIG,
        unique_id=SLEEPIQ_CONFIG[CONF_USERNAME].lower(),
    )

    mock_entry.add_to_hass(hass)

    mock_registry(
        hass,
        {
            ENTITY_IS_IN_BED:
            er.RegistryEntry(
                entity_id=ENTITY_IS_IN_BED,
                unique_id=f"{BED_ID}_{SLEEPER_L_NAME}_{IS_IN_BED}",
                platform=DOMAIN,
                config_entry_id=mock_entry.entry_id,
            ),
            ENTITY_PRESSURE:
            er.RegistryEntry(
                entity_id=ENTITY_PRESSURE,
                unique_id=f"{BED_ID}_{SLEEPER_L_NAME}_{PRESSURE}",
                platform=DOMAIN,
                config_entry_id=mock_entry.entry_id,
            ),
            ENTITY_SLEEP_NUMBER:
            er.RegistryEntry(
                entity_id=ENTITY_SLEEP_NUMBER,
                unique_id=f"{BED_ID}_{SLEEPER_L_NAME}_{SLEEP_NUMBER}",
                platform=DOMAIN,
                config_entry_id=mock_entry.entry_id,
            ),
        },
    )
    assert await async_setup_component(hass, DOMAIN, {})
    await hass.async_block_till_done()

    ent_reg = er.async_get(hass)

    sensor_is_in_bed = ent_reg.async_get(ENTITY_IS_IN_BED)
    assert sensor_is_in_bed.unique_id == f"{SLEEPER_L_ID}_{IS_IN_BED}"

    sensor_pressure = ent_reg.async_get(ENTITY_PRESSURE)
    assert sensor_pressure.unique_id == f"{SLEEPER_L_ID}_{PRESSURE}"

    sensor_sleep_number = ent_reg.async_get(ENTITY_SLEEP_NUMBER)
    assert sensor_sleep_number.unique_id == f"{SLEEPER_L_ID}_{SLEEP_NUMBER}"
示例#7
0
async def test_entity_registry_updates_invalid_entity_id(hass):
    """Test that we can't update to an invalid entity id."""
    registry = mock_registry(
        hass,
        {
            "test_domain.world": er.RegistryEntry(
                entity_id="test_domain.world",
                unique_id="1234",
                # Using component.async_add_entities is equal to platform "domain"
                platform="test_platform",
                name="Some name",
            ),
            "test_domain.existing": er.RegistryEntry(
                entity_id="test_domain.existing",
                unique_id="5678",
                platform="test_platform",
            ),
        },
    )
    platform = MockEntityPlatform(hass)
    entity = MockEntity(unique_id="1234")
    await platform.async_add_entities([entity])

    state = hass.states.get("test_domain.world")
    assert state is not None
    assert state.name == "Some name"

    with pytest.raises(ValueError):
        registry.async_update_entity(
            "test_domain.world", new_entity_id="test_domain.existing"
        )

    with pytest.raises(ValueError):
        registry.async_update_entity(
            "test_domain.world", new_entity_id="invalid_entity_id"
        )

    with pytest.raises(ValueError):
        registry.async_update_entity(
            "test_domain.world", new_entity_id="diff_domain.world"
        )

    await hass.async_block_till_done()
    await hass.async_block_till_done()

    assert hass.states.get("test_domain.world") is not None
    assert hass.states.get("invalid_entity_id") is None
    assert hass.states.get("diff_domain.world") is None
async def test_migration(hass):
    """Test that we can migrate coronavirus to stable unique ID."""
    nl_entry = MockConfigEntry(domain=DOMAIN, title="Netherlands", data={"country": 34})
    nl_entry.add_to_hass(hass)
    worldwide_entry = MockConfigEntry(
        domain=DOMAIN, title="Worldwide", data={"country": OPTION_WORLDWIDE}
    )
    worldwide_entry.add_to_hass(hass)
    mock_registry(
        hass,
        {
            "sensor.netherlands_confirmed": entity_registry.RegistryEntry(
                entity_id="sensor.netherlands_confirmed",
                unique_id="34-confirmed",
                platform="coronavirus",
                config_entry_id=nl_entry.entry_id,
            ),
            "sensor.worldwide_confirmed": entity_registry.RegistryEntry(
                entity_id="sensor.worldwide_confirmed",
                unique_id="__worldwide-confirmed",
                platform="coronavirus",
                config_entry_id=worldwide_entry.entry_id,
            ),
        },
    )
    with patch(
        "coronavirus.get_cases",
        return_value=[
            Mock(country="Netherlands", confirmed=10, recovered=8, deaths=1, current=1),
            Mock(country="Germany", confirmed=1, recovered=0, deaths=0, current=0),
        ],
    ):
        assert await async_setup_component(hass, DOMAIN, {})
        await hass.async_block_till_done()

    ent_reg = await entity_registry.async_get_registry(hass)

    sensor_nl = ent_reg.async_get("sensor.netherlands_confirmed")
    assert sensor_nl.unique_id == "Netherlands-confirmed"

    sensor_worldwide = ent_reg.async_get("sensor.worldwide_confirmed")
    assert sensor_worldwide.unique_id == "__worldwide-confirmed"

    assert hass.states.get("sensor.netherlands_confirmed").state == "10"
    assert hass.states.get("sensor.worldwide_confirmed").state == "11"

    assert nl_entry.unique_id == "Netherlands"
    assert worldwide_entry.unique_id == OPTION_WORLDWIDE
示例#9
0
async def test_disabled_in_entity_registry(hass):
    """Test entity is removed if we disable entity registry entry."""
    entry = entity_registry.RegistryEntry(
        entity_id="hello.world",
        unique_id="test-unique-id",
        platform="test-platform",
        disabled_by="user",
    )
    registry = mock_registry(hass, {"hello.world": entry})

    ent = entity.Entity()
    ent.hass = hass
    ent.entity_id = "hello.world"
    ent.registry_entry = entry
    ent.platform = MagicMock(platform_name="test-platform")

    await ent.async_internal_added_to_hass()
    ent.async_write_ha_state()
    assert hass.states.get("hello.world") is None

    entry2 = registry.async_update_entity("hello.world", disabled_by=None)
    await hass.async_block_till_done()
    assert entry2 != entry
    assert ent.registry_entry == entry2
    assert ent.enabled is True

    entry3 = registry.async_update_entity("hello.world", disabled_by="user")
    await hass.async_block_till_done()
    assert entry3 != entry2
    assert ent.registry_entry == entry3
    assert ent.enabled is False
示例#10
0
async def test_entity_registry_updates(hass):
    """Test that updates on the entity registry update platform entities."""
    registry = mock_registry(
        hass,
        {
            'test_domain.world':
            entity_registry.RegistryEntry(
                entity_id='test_domain.world',
                unique_id='1234',
                # Using component.async_add_entities is equal to platform "domain"
                platform='test_platform',
                name='before update')
        })
    platform = MockEntityPlatform(hass)
    entity = MockEntity(unique_id='1234')
    await platform.async_add_entities([entity])

    state = hass.states.get('test_domain.world')
    assert state is not None
    assert state.name == 'before update'

    registry.async_update_entity('test_domain.world', name='after update')
    await hass.async_block_till_done()
    await hass.async_block_till_done()

    state = hass.states.get('test_domain.world')
    assert state.name == 'after update'
async def test_disabled_in_entity_registry(hass):
    """Test entity is removed if we disable entity registry entry."""
    entry = entity_registry.RegistryEntry(
        entity_id="hello.world",
        unique_id="test-unique-id",
        platform="test-platform",
        disabled_by=None,
    )
    registry = mock_registry(hass, {"hello.world": entry})

    ent = entity.Entity()
    ent.hass = hass
    ent.entity_id = "hello.world"
    ent.registry_entry = entry
    assert ent.enabled is True

    ent.add_to_platform_start(hass, MagicMock(platform_name="test-platform"),
                              None)
    await ent.add_to_platform_finish()
    assert hass.states.get("hello.world") is not None

    entry2 = registry.async_update_entity(
        "hello.world", disabled_by=entity_registry.DISABLED_USER)
    await hass.async_block_till_done()
    assert entry2 != entry
    assert ent.registry_entry == entry2
    assert ent.enabled is False
    assert hass.states.get("hello.world") is None

    entry3 = registry.async_update_entity("hello.world", disabled_by=None)
    await hass.async_block_till_done()
    assert entry3 != entry2
    # Entry is no longer updated, entity is no longer tracking changes
    assert ent.registry_entry == entry2
示例#12
0
async def test_entity_registry_updates_entity_id(hass):
    """Test that updates on the entity registry update platform entities."""
    registry = mock_registry(
        hass,
        {
            "test_domain.world":
            entity_registry.RegistryEntry(
                entity_id="test_domain.world",
                unique_id="1234",
                # Using component.async_add_entities is equal to platform "domain"
                platform="test_platform",
                name="Some name",
            )
        },
    )
    platform = MockEntityPlatform(hass)
    entity = MockEntity(unique_id="1234")
    await platform.async_add_entities([entity])

    state = hass.states.get("test_domain.world")
    assert state is not None
    assert state.name == "Some name"

    registry.async_update_entity("test_domain.world",
                                 new_entity_id="test_domain.planet")
    await hass.async_block_till_done()
    await hass.async_block_till_done()

    assert hass.states.get("test_domain.world") is None
    assert hass.states.get("test_domain.planet") is not None
示例#13
0
async def test_domain_control_unauthorized(hass, hass_read_only_user):
    """Test domain verification in a service call with an unauthorized user."""
    mock_registry(
        hass,
        {
            "light.kitchen": ent_reg.RegistryEntry(
                entity_id="light.kitchen", unique_id="kitchen", platform="test_domain",
            )
        },
    )

    calls = []

    async def mock_service_log(call):
        """Define a protected service."""
        calls.append(call)

    protected_mock_service = hass.helpers.service.verify_domain_control("test_domain")(
        mock_service_log
    )

    hass.services.async_register(
        "test_domain", "test_service", protected_mock_service, schema=None
    )

    with pytest.raises(exceptions.Unauthorized):
        await hass.services.async_call(
            "test_domain",
            "test_service",
            {},
            blocking=True,
            context=ha.Context(user_id=hass_read_only_user.id),
        )

    assert len(calls) == 0
示例#14
0
async def test_domain_control_no_user(hass):
    """Test domain verification in a service call with no user."""
    mock_registry(
        hass,
        {
            "light.kitchen": ent_reg.RegistryEntry(
                entity_id="light.kitchen", unique_id="kitchen", platform="test_domain",
            )
        },
    )

    calls = []

    async def mock_service_log(call):
        """Define a protected service."""
        calls.append(call)

    protected_mock_service = hass.helpers.service.verify_domain_control("test_domain")(
        mock_service_log
    )

    hass.services.async_register(
        "test_domain", "test_service", protected_mock_service, schema=None
    )

    await hass.services.async_call(
        "test_domain",
        "test_service",
        {},
        blocking=True,
        context=ha.Context(user_id=None),
    )

    assert len(calls) == 1
async def test_warn_disabled(hass, caplog):
    """Test we warn once if we write to a disabled entity."""
    entry = entity_registry.RegistryEntry(
        entity_id="hello.world",
        unique_id="test-unique-id",
        platform="test-platform",
        disabled_by=entity_registry.DISABLED_USER,
    )
    mock_registry(hass, {"hello.world": entry})

    ent = entity.Entity()
    ent.hass = hass
    ent.entity_id = "hello.world"
    ent.registry_entry = entry
    ent.platform = MagicMock(platform_name="test-platform")

    caplog.clear()
    ent.async_write_ha_state()
    assert hass.states.get("hello.world") is None
    assert "Entity hello.world is incorrectly being triggered" in caplog.text

    caplog.clear()
    ent.async_write_ha_state()
    assert hass.states.get("hello.world") is None
    assert caplog.text == ""
示例#16
0
async def test_entity_registry_updates_invalid_entity_id(hass):
    """Test that we can't update to an invalid entity id."""
    registry = mock_registry(
        hass,
        {
            'test_domain.world':
            entity_registry.RegistryEntry(
                entity_id='test_domain.world',
                unique_id='1234',
                # Using component.async_add_entities is equal to platform "domain"
                platform='test_platform',
                name='Some name'),
            'test_domain.existing':
            entity_registry.RegistryEntry(
                entity_id='test_domain.existing',
                unique_id='5678',
                platform='test_platform',
            ),
        })
    platform = MockEntityPlatform(hass)
    entity = MockEntity(unique_id='1234')
    await platform.async_add_entities([entity])

    state = hass.states.get('test_domain.world')
    assert state is not None
    assert state.name == 'Some name'

    with pytest.raises(ValueError):
        registry.async_update_entity('test_domain.world',
                                     new_entity_id='test_domain.existing')

    with pytest.raises(ValueError):
        registry.async_update_entity('test_domain.world',
                                     new_entity_id='invalid_entity_id')

    with pytest.raises(ValueError):
        registry.async_update_entity('test_domain.world',
                                     new_entity_id='diff_domain.world')

    await hass.async_block_till_done()
    await hass.async_block_till_done()

    assert hass.states.get('test_domain.world') is not None
    assert hass.states.get('invalid_entity_id') is None
    assert hass.states.get('diff_domain.world') is None
示例#17
0
def area_mock(hass):
    """Mock including area info."""
    hass.states.async_set("light.Bowl", STATE_ON)
    hass.states.async_set("light.Ceiling", STATE_OFF)
    hass.states.async_set("light.Kitchen", STATE_OFF)

    device_in_area = dev_reg.DeviceEntry(area_id="test-area")
    device_no_area = dev_reg.DeviceEntry()
    device_diff_area = dev_reg.DeviceEntry(area_id="diff-area")

    mock_device_registry(
        hass,
        {
            device_in_area.id: device_in_area,
            device_no_area.id: device_no_area,
            device_diff_area.id: device_diff_area,
        },
    )

    entity_in_area = ent_reg.RegistryEntry(
        entity_id="light.in_area",
        unique_id="in-area-id",
        platform="test",
        device_id=device_in_area.id,
    )
    entity_no_area = ent_reg.RegistryEntry(
        entity_id="light.no_area",
        unique_id="no-area-id",
        platform="test",
        device_id=device_no_area.id,
    )
    entity_diff_area = ent_reg.RegistryEntry(
        entity_id="light.diff_area",
        unique_id="diff-area-id",
        platform="test",
        device_id=device_diff_area.id,
    )
    mock_registry(
        hass,
        {
            entity_in_area.entity_id: entity_in_area,
            entity_no_area.entity_id: entity_no_area,
            entity_diff_area.entity_id: entity_diff_area,
        },
    )
示例#18
0
async def test_migration(hass):
    """Test that we can migrate coronavirus to stable unique ID."""
    nl_entry = MockConfigEntry(domain=DOMAIN,
                               title="Netherlands",
                               data={"country": 34})
    nl_entry.add_to_hass(hass)
    worldwide_entry = MockConfigEntry(domain=DOMAIN,
                                      title="Worldwide",
                                      data={"country": OPTION_WORLDWIDE})
    worldwide_entry.add_to_hass(hass)
    mock_registry(
        hass,
        {
            "sensor.netherlands_confirmed":
            er.RegistryEntry(
                entity_id="sensor.netherlands_confirmed",
                unique_id="34-confirmed",
                platform="coronavirus",
                config_entry_id=nl_entry.entry_id,
            ),
            "sensor.worldwide_confirmed":
            er.RegistryEntry(
                entity_id="sensor.worldwide_confirmed",
                unique_id="__worldwide-confirmed",
                platform="coronavirus",
                config_entry_id=worldwide_entry.entry_id,
            ),
        },
    )
    assert await async_setup_component(hass, DOMAIN, {})
    await hass.async_block_till_done()

    ent_reg = er.async_get(hass)

    sensor_nl = ent_reg.async_get("sensor.netherlands_confirmed")
    assert sensor_nl.unique_id == "Netherlands-confirmed"

    sensor_worldwide = ent_reg.async_get("sensor.worldwide_confirmed")
    assert sensor_worldwide.unique_id == "__worldwide-confirmed"

    assert hass.states.get("sensor.netherlands_confirmed").state == "10"
    assert hass.states.get("sensor.worldwide_confirmed").state == "11"

    assert nl_entry.unique_id == "Netherlands"
    assert worldwide_entry.unique_id == OPTION_WORLDWIDE
示例#19
0
async def test_invalid_entity_category_str(hass, registry, caplog):
    """Test use of invalid entity category."""
    entry = er.RegistryEntry(
        entity_id="light.kitchen",
        unique_id="5678",
        platform="hue",
        entity_category="invalid",
    )

    assert entry.entity_category is None
示例#20
0
async def test_invalid_entity_category_str(hass, registry, caplog):
    """Test use of invalid entity category."""
    entry = er.RegistryEntry(
        "light",
        "hue",
        "5678",
        entity_category="invalid",
    )

    assert entry.entity_category is None
示例#21
0
async def test_deprecated_entity_category_str(hass, registry, caplog):
    """Test deprecated str use of entity_category converts to enum and logs a warning."""
    entry = er.RegistryEntry(
        entity_id="light.kitchen",
        unique_id="5678",
        platform="hue",
        entity_category="diagnostic",
    )

    assert entry.entity_category is EntityCategory.DIAGNOSTIC
    assert " should be updated to use EntityCategory" in caplog.text
def test_registry_respect_entity_disabled(hass):
    """Test that the registry respects entity disabled."""
    mock_registry(hass, {
        'test_domain.world': entity_registry.RegistryEntry(
            entity_id='test_domain.world',
            unique_id='1234',
            # Using component.async_add_entities is equal to platform "domain"
            platform='test_platform',
            disabled_by=entity_registry.DISABLED_USER
        )
    })
    platform = MockEntityPlatform(hass)
    entity = MockEntity(unique_id='1234')
    yield from platform.async_add_entities([entity])
    assert entity.entity_id is None
    assert hass.states.async_entity_ids() == []
示例#23
0
def test_overriding_name_from_registry(hass):
    """Test that we can override a name via the Entity Registry."""
    component = EntityComponent(_LOGGER, DOMAIN, hass)
    mock_registry(hass, {
        'test_domain.world': entity_registry.RegistryEntry(
            entity_id='test_domain.world',
            unique_id='1234',
            # Using component.async_add_entities is equal to platform "domain"
            platform='test_domain',
            name='Overridden'
        )
    })
    yield from component.async_add_entities([
        MockEntity(unique_id='1234', name='Device Name')])

    state = hass.states.get('test_domain.world')
    assert state is not None
    assert state.name == 'Overridden'
示例#24
0
async def test_registry_respect_entity_disabled(hass):
    """Test that the registry respects entity disabled."""
    mock_registry(
        hass,
        {
            "test_domain.world": er.RegistryEntry(
                entity_id="test_domain.world",
                unique_id="1234",
                # Using component.async_add_entities is equal to platform "domain"
                platform="test_platform",
                disabled_by=er.DISABLED_USER,
            )
        },
    )
    platform = MockEntityPlatform(hass)
    entity = MockEntity(unique_id="1234")
    await platform.async_add_entities([entity])
    assert entity.entity_id == "test_domain.world"
    assert hass.states.async_entity_ids() == []
示例#25
0
async def test_overriding_name_from_registry(hass):
    """Test that we can override a name via the Entity Registry."""
    component = EntityComponent(_LOGGER, DOMAIN, hass)
    mock_registry(
        hass,
        {
            "test_domain.world":
            entity_registry.RegistryEntry(
                entity_id="test_domain.world",
                unique_id="1234",
                # Using component.async_add_entities is equal to platform "domain"
                platform="test_domain",
                name="Overridden",
            )
        },
    )
    await component.async_add_entities(
        [MockEntity(unique_id="1234", name="Device Name")])

    state = hass.states.get("test_domain.world")
    assert state is not None
    assert state.name == "Overridden"
示例#26
0
async def test_entity_info_added_to_entity_registry(hass):
    """Test entity info is written to entity registry."""
    component = EntityComponent(_LOGGER, DOMAIN, hass, timedelta(seconds=20))

    entity_default = MockEntity(
        capability_attributes={"max": 100},
        device_class="mock-device-class",
        entity_category=EntityCategory.CONFIG,
        icon="nice:icon",
        name="best name",
        supported_features=5,
        unique_id="default",
        unit_of_measurement=PERCENTAGE,
    )

    await component.async_add_entities([entity_default])

    registry = er.async_get(hass)

    entry_default = registry.async_get_or_create(DOMAIN, DOMAIN, "default")
    assert entry_default == er.RegistryEntry(
        "test_domain.best_name",
        "default",
        "test_domain",
        capabilities={"max": 100},
        device_class=None,
        entity_category=EntityCategory.CONFIG,
        icon=None,
        id=ANY,
        name=None,
        original_device_class="mock-device-class",
        original_icon="nice:icon",
        original_name="best name",
        supported_features=5,
        unit_of_measurement=PERCENTAGE,
    )
async def test_removing_entity_unavailable(hass):
    """Test removing an entity that is still registered creates an unavailable state."""
    entry = entity_registry.RegistryEntry(
        entity_id="hello.world",
        unique_id="test-unique-id",
        platform="test-platform",
        disabled_by=None,
    )

    ent = entity.Entity()
    ent.hass = hass
    ent.entity_id = "hello.world"
    ent.registry_entry = entry
    ent.async_write_ha_state()

    state = hass.states.get("hello.world")
    assert state is not None
    assert state.state == STATE_UNKNOWN

    await ent.async_remove()

    state = hass.states.get("hello.world")
    assert state is not None
    assert state.state == STATE_UNAVAILABLE
示例#28
0
def area_mock(hass):
    """Mock including area info."""
    hass.states.async_set("light.Bowl", STATE_ON)
    hass.states.async_set("light.Ceiling", STATE_OFF)
    hass.states.async_set("light.Kitchen", STATE_OFF)

    device_in_area = dev_reg.DeviceEntry(area_id="test-area")
    device_no_area = dev_reg.DeviceEntry(id="device-no-area-id")
    device_diff_area = dev_reg.DeviceEntry(area_id="diff-area")
    device_area_a = dev_reg.DeviceEntry(id="device-area-a-id",
                                        area_id="area-a")

    mock_device_registry(
        hass,
        {
            device_in_area.id: device_in_area,
            device_no_area.id: device_no_area,
            device_diff_area.id: device_diff_area,
            device_area_a.id: device_area_a,
        },
    )

    entity_in_own_area = ent_reg.RegistryEntry(
        entity_id="light.in_own_area",
        unique_id="in-own-area-id",
        platform="test",
        area_id="own-area",
    )
    config_entity_in_own_area = ent_reg.RegistryEntry(
        entity_id="light.config_in_own_area",
        unique_id="config-in-own-area-id",
        platform="test",
        area_id="own-area",
        entity_category=EntityCategory.CONFIG,
    )
    hidden_entity_in_own_area = ent_reg.RegistryEntry(
        entity_id="light.hidden_in_own_area",
        unique_id="hidden-in-own-area-id",
        platform="test",
        area_id="own-area",
        hidden_by=ent_reg.RegistryEntryHider.USER,
    )
    entity_in_area = ent_reg.RegistryEntry(
        entity_id="light.in_area",
        unique_id="in-area-id",
        platform="test",
        device_id=device_in_area.id,
    )
    config_entity_in_area = ent_reg.RegistryEntry(
        entity_id="light.config_in_area",
        unique_id="config-in-area-id",
        platform="test",
        device_id=device_in_area.id,
        entity_category=EntityCategory.CONFIG,
    )
    hidden_entity_in_area = ent_reg.RegistryEntry(
        entity_id="light.hidden_in_area",
        unique_id="hidden-in-area-id",
        platform="test",
        device_id=device_in_area.id,
        hidden_by=ent_reg.RegistryEntryHider.USER,
    )
    entity_in_other_area = ent_reg.RegistryEntry(
        entity_id="light.in_other_area",
        unique_id="in-area-a-id",
        platform="test",
        device_id=device_in_area.id,
        area_id="other-area",
    )
    entity_assigned_to_area = ent_reg.RegistryEntry(
        entity_id="light.assigned_to_area",
        unique_id="assigned-area-id",
        platform="test",
        device_id=device_in_area.id,
        area_id="test-area",
    )
    entity_no_area = ent_reg.RegistryEntry(
        entity_id="light.no_area",
        unique_id="no-area-id",
        platform="test",
        device_id=device_no_area.id,
    )
    config_entity_no_area = ent_reg.RegistryEntry(
        entity_id="light.config_no_area",
        unique_id="config-no-area-id",
        platform="test",
        device_id=device_no_area.id,
        entity_category=EntityCategory.CONFIG,
    )
    hidden_entity_no_area = ent_reg.RegistryEntry(
        entity_id="light.hidden_no_area",
        unique_id="hidden-no-area-id",
        platform="test",
        device_id=device_no_area.id,
        hidden_by=ent_reg.RegistryEntryHider.USER,
    )
    entity_diff_area = ent_reg.RegistryEntry(
        entity_id="light.diff_area",
        unique_id="diff-area-id",
        platform="test",
        device_id=device_diff_area.id,
    )
    entity_in_area_a = ent_reg.RegistryEntry(
        entity_id="light.in_area_a",
        unique_id="in-area-a-id",
        platform="test",
        device_id=device_area_a.id,
        area_id="area-a",
    )
    entity_in_area_b = ent_reg.RegistryEntry(
        entity_id="light.in_area_b",
        unique_id="in-area-b-id",
        platform="test",
        device_id=device_area_a.id,
        area_id="area-b",
    )
    mock_registry(
        hass,
        {
            entity_in_own_area.entity_id: entity_in_own_area,
            config_entity_in_own_area.entity_id: config_entity_in_own_area,
            hidden_entity_in_own_area.entity_id: hidden_entity_in_own_area,
            entity_in_area.entity_id: entity_in_area,
            config_entity_in_area.entity_id: config_entity_in_area,
            hidden_entity_in_area.entity_id: hidden_entity_in_area,
            entity_in_other_area.entity_id: entity_in_other_area,
            entity_assigned_to_area.entity_id: entity_assigned_to_area,
            entity_no_area.entity_id: entity_no_area,
            config_entity_no_area.entity_id: config_entity_no_area,
            hidden_entity_no_area.entity_id: hidden_entity_no_area,
            entity_diff_area.entity_id: entity_diff_area,
            entity_in_area_a.entity_id: entity_in_area_a,
            entity_in_area_b.entity_id: entity_in_area_b,
        },
    )
示例#29
0
def zwave_migration_data_fixture(hass):
    """Return mock zwave migration data."""
    zwave_switch_device = dr.DeviceEntry(
        id=ZWAVE_SWITCH_DEVICE_ID,
        name_by_user=ZWAVE_SWITCH_DEVICE_NAME,
        area_id=ZWAVE_SWITCH_DEVICE_AREA,
    )
    zwave_switch_entry = er.RegistryEntry(
        entity_id=ZWAVE_SWITCH_ENTITY,
        unique_id=ZWAVE_SWITCH_UNIQUE_ID,
        platform="zwave",
        name=ZWAVE_SWITCH_NAME,
        icon=ZWAVE_SWITCH_ICON,
    )
    zwave_multisensor_device = dr.DeviceEntry(
        id=ZWAVE_MULTISENSOR_DEVICE_ID,
        name_by_user=ZWAVE_MULTISENSOR_DEVICE_NAME,
        area_id=ZWAVE_MULTISENSOR_DEVICE_AREA,
    )
    zwave_source_node_entry = er.RegistryEntry(
        entity_id=ZWAVE_SOURCE_NODE_ENTITY,
        unique_id=ZWAVE_SOURCE_NODE_UNIQUE_ID,
        platform="zwave",
        name="Z-Wave Source Node",
    )
    zwave_battery_entry = er.RegistryEntry(
        entity_id=ZWAVE_BATTERY_ENTITY,
        unique_id=ZWAVE_BATTERY_UNIQUE_ID,
        platform="zwave",
        name=ZWAVE_BATTERY_NAME,
        icon=ZWAVE_BATTERY_ICON,
        unit_of_measurement="%",
    )
    zwave_power_entry = er.RegistryEntry(
        entity_id=ZWAVE_POWER_ENTITY,
        unique_id=ZWAVE_POWER_UNIQUE_ID,
        platform="zwave",
        name=ZWAVE_POWER_NAME,
        icon=ZWAVE_POWER_ICON,
        unit_of_measurement="W",
    )
    zwave_tampering_entry = er.RegistryEntry(
        entity_id=ZWAVE_TAMPERING_ENTITY,
        unique_id=ZWAVE_TAMPERING_UNIQUE_ID,
        platform="zwave",
        name=ZWAVE_TAMPERING_NAME,
        icon=ZWAVE_TAMPERING_ICON,
        unit_of_measurement="",  # Test empty string unit normalization.
    )

    zwave_migration_data = {
        ZWAVE_SWITCH_ENTITY: {
            "node_id": 102,
            "node_instance": 1,
            "command_class": 37,
            "command_class_label": "",
            "value_index": 1,
            "device_id": zwave_switch_device.id,
            "domain": zwave_switch_entry.domain,
            "entity_id": zwave_switch_entry.entity_id,
            "unique_id": ZWAVE_SWITCH_UNIQUE_ID,
            "unit_of_measurement": zwave_switch_entry.unit_of_measurement,
        },
        ZWAVE_POWER_ENTITY: {
            "node_id": 102,
            "node_instance": 1,
            "command_class": 50,
            "command_class_label": "Power",
            "value_index": 8,
            "device_id": zwave_switch_device.id,
            "domain": zwave_power_entry.domain,
            "entity_id": zwave_power_entry.entity_id,
            "unique_id": ZWAVE_POWER_UNIQUE_ID,
            "unit_of_measurement": zwave_power_entry.unit_of_measurement,
        },
        ZWAVE_SOURCE_NODE_ENTITY: {
            "node_id": 52,
            "node_instance": 1,
            "command_class": 113,
            "command_class_label": "SourceNodeId",
            "value_index": 1,
            "device_id": zwave_multisensor_device.id,
            "domain": zwave_source_node_entry.domain,
            "entity_id": zwave_source_node_entry.entity_id,
            "unique_id": ZWAVE_SOURCE_NODE_UNIQUE_ID,
            "unit_of_measurement": zwave_source_node_entry.unit_of_measurement,
        },
        ZWAVE_BATTERY_ENTITY: {
            "node_id": 52,
            "node_instance": 1,
            "command_class": 128,
            "command_class_label": "Battery Level",
            "value_index": 0,
            "device_id": zwave_multisensor_device.id,
            "domain": zwave_battery_entry.domain,
            "entity_id": zwave_battery_entry.entity_id,
            "unique_id": ZWAVE_BATTERY_UNIQUE_ID,
            "unit_of_measurement": zwave_battery_entry.unit_of_measurement,
        },
        ZWAVE_TAMPERING_ENTITY: {
            "node_id": 52,
            "node_instance": 1,
            "command_class": 113,
            "command_class_label": "Burglar",
            "value_index": 10,
            "device_id": zwave_multisensor_device.id,
            "domain": zwave_tampering_entry.domain,
            "entity_id": zwave_tampering_entry.entity_id,
            "unique_id": ZWAVE_TAMPERING_UNIQUE_ID,
            "unit_of_measurement": zwave_tampering_entry.unit_of_measurement,
        },
    }

    mock_device_registry(
        hass,
        {
            zwave_switch_device.id: zwave_switch_device,
            zwave_multisensor_device.id: zwave_multisensor_device,
        },
    )
    mock_registry(
        hass,
        {
            ZWAVE_SWITCH_ENTITY: zwave_switch_entry,
            ZWAVE_SOURCE_NODE_ENTITY: zwave_source_node_entry,
            ZWAVE_BATTERY_ENTITY: zwave_battery_entry,
            ZWAVE_POWER_ENTITY: zwave_power_entry,
            ZWAVE_TAMPERING_ENTITY: zwave_tampering_entry,
        },
    )

    return zwave_migration_data
示例#30
0
def test_get_or_create_updates_data(registry):
    """Test that we update data in get_or_create."""
    orig_config_entry = MockConfigEntry(domain="light")

    orig_entry = registry.async_get_or_create(
        "light",
        "hue",
        "5678",
        area_id="mock-area-id",
        capabilities={"max": 100},
        config_entry=orig_config_entry,
        device_id="mock-dev-id",
        disabled_by=er.DISABLED_HASS,
        entity_category="config",
        original_device_class="mock-device-class",
        original_icon="initial-original_icon",
        original_name="initial-original_name",
        supported_features=5,
        unit_of_measurement="initial-unit_of_measurement",
    )

    assert orig_entry == er.RegistryEntry(
        "light.hue_5678",
        "5678",
        "hue",
        area_id="mock-area-id",
        capabilities={"max": 100},
        config_entry_id=orig_config_entry.entry_id,
        device_class=None,
        device_id="mock-dev-id",
        disabled_by=er.DISABLED_HASS,
        entity_category="config",
        icon=None,
        id=orig_entry.id,
        name=None,
        original_device_class="mock-device-class",
        original_icon="initial-original_icon",
        original_name="initial-original_name",
        supported_features=5,
        unit_of_measurement="initial-unit_of_measurement",
    )

    new_config_entry = MockConfigEntry(domain="light")

    new_entry = registry.async_get_or_create(
        "light",
        "hue",
        "5678",
        area_id="new-mock-area-id",
        capabilities={"new-max": 100},
        config_entry=new_config_entry,
        device_id="new-mock-dev-id",
        disabled_by=er.DISABLED_USER,
        entity_category=None,
        original_device_class="new-mock-device-class",
        original_icon="updated-original_icon",
        original_name="updated-original_name",
        supported_features=10,
        unit_of_measurement="updated-unit_of_measurement",
    )

    assert new_entry == er.RegistryEntry(
        "light.hue_5678",
        "5678",
        "hue",
        area_id="new-mock-area-id",
        capabilities={"new-max": 100},
        config_entry_id=new_config_entry.entry_id,
        device_class=None,
        device_id="new-mock-dev-id",
        disabled_by=er.DISABLED_HASS,  # Should not be updated
        entity_category="config",
        icon=None,
        id=orig_entry.id,
        name=None,
        original_device_class="new-mock-device-class",
        original_icon="updated-original_icon",
        original_name="updated-original_name",
        supported_features=10,
        unit_of_measurement="updated-unit_of_measurement",
    )