async def test_update_entity_no_changes(hass, client):
    """Test get entry."""
    mock_registry(hass, {
        'test_domain.world': RegistryEntry(
            entity_id='test_domain.world',
            unique_id='1234',
            # Using component.async_add_entities is equal to platform "domain"
            platform='test_platform',
            name='name of entity'
        )
    })
    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 == 'name of entity'

    await client.send_json({
        'id': 6,
        'type': 'config/entity_registry/update',
        'entity_id': 'test_domain.world',
        'name': 'name of entity',
    })

    msg = await client.receive_json()

    assert msg['result'] == {
        'entity_id': 'test_domain.world',
        'name': 'name of entity'
    }

    state = hass.states.get('test_domain.world')
    assert state.name == 'name of entity'
async def test_setup_entry(hass):
    """Test we can setup an entry."""
    registry = mock_registry(hass)

    async def async_setup_entry(hass, config_entry, async_add_entities):
        """Mock setup entry method."""
        async_add_entities([
            MockEntity(name='test1', unique_id='unique')
        ])
        return True

    platform = MockPlatform(
        async_setup_entry=async_setup_entry
    )
    config_entry = MockConfigEntry(entry_id='super-mock-id')
    entity_platform = MockEntityPlatform(
        hass,
        platform_name=config_entry.domain,
        platform=platform
    )

    assert await entity_platform.async_setup_entry(config_entry)
    await hass.async_block_till_done()
    full_name = '{}.{}'.format(entity_platform.domain, config_entry.domain)
    assert full_name in hass.config.components
    assert len(hass.states.async_entity_ids()) == 1
    assert len(registry.entities) == 1
    assert registry.entities['test_domain.test1'].config_entry_id == \
        'super-mock-id'
Пример #3
0
def test_registry_respect_entity_namespace(hass):
    """Test that the registry respects entity namespace."""
    mock_registry(hass)
    platform = MockEntityPlatform(hass, entity_namespace='ns')
    entity = MockEntity(unique_id='1234', name='Device Name')
    yield from platform.async_add_entities([entity])
    assert entity.entity_id == 'test_domain.ns_device_name'
Пример #4
0
async def test_entity_registry_updates_name(opp):
    """Test that updates on the entity registry update platform entities."""
    registry = mock_registry(
        opp,
        {
            "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="before update",
            )
        },
    )
    platform = MockEntityPlatform(opp)
    entity = MockEntity(unique_id="1234")
    await platform.async_add_entities([entity])

    state = opp.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 opp.async_block_till_done()
    await opp.async_block_till_done()

    state = opp.states.get("test_domain.world")
    assert state.name == "after update"
async def test_update_invalid_entity_id(hass, client):
    """Test update entity id to an invalid entity id."""
    mock_registry(
        hass,
        {
            "test_domain.world": RegistryEntry(
                entity_id="test_domain.world",
                unique_id="1234",
                # Using component.async_add_entities is equal to platform "domain"
                platform="test_platform",
            )
        },
    )
    platform = MockEntityPlatform(hass)
    entities = [MockEntity(unique_id="1234"), MockEntity(unique_id="2345")]
    await platform.async_add_entities(entities)

    await client.send_json(
        {
            "id": 6,
            "type": "config/entity_registry/update",
            "entity_id": "test_domain.world",
            "new_entity_id": "another_domain.planet",
        }
    )

    msg = await client.receive_json()

    assert not msg["success"]
Пример #6
0
async def test_setup_source(hass):
    """Check that we register sources correctly."""
    platform = MockEntityPlatform(hass)

    entity_platform = MockEntity(name="Platform Config Source")
    await platform.async_add_entities([entity_platform])

    platform.config_entry = MockConfigEntry()
    entity_entry = MockEntity(name="Config Entry Source")
    await platform.async_add_entities([entity_entry])

    assert entity.entity_sources(hass) == {
        "test_domain.platform_config_source": {
            "source": entity.SOURCE_PLATFORM_CONFIG,
            "domain": "test_platform",
        },
        "test_domain.config_entry_source": {
            "source": entity.SOURCE_CONFIG_ENTRY,
            "config_entry": platform.config_entry.entry_id,
            "domain": "test_platform",
        },
    }

    await platform.async_reset()

    assert entity.entity_sources(hass) == {}
Пример #7
0
async def test_device_info_not_overrides(hass):
    """Test device info is forwarded correctly."""
    registry = await hass.helpers.device_registry.async_get_registry()
    device = registry.async_get_or_create(config_entry_id='bla',
                                          connections={('mac', 'abcd')},
                                          manufacturer='test-manufacturer',
                                          model='test-model')

    assert device.manufacturer == 'test-manufacturer'
    assert device.model == 'test-model'

    async def async_setup_entry(hass, config_entry, async_add_entities):
        """Mock setup entry method."""
        async_add_entities([
            MockEntity(unique_id='qwer',
                       device_info={
                           'connections': {('mac', 'abcd')},
                       }),
        ])
        return True

    platform = MockPlatform(async_setup_entry=async_setup_entry)
    config_entry = MockConfigEntry(entry_id='super-mock-id')
    entity_platform = MockEntityPlatform(hass,
                                         platform_name=config_entry.domain,
                                         platform=platform)

    assert await entity_platform.async_setup_entry(config_entry)
    await hass.async_block_till_done()

    device2 = registry.async_get_device(set(), {('mac', 'abcd')})
    assert device2 is not None
    assert device.id == device2.id
    assert device2.manufacturer == 'test-manufacturer'
    assert device2.model == 'test-model'
Пример #8
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
Пример #9
0
async def test_device_info_called(hass):
    """Test device info is forwarded correctly."""
    registry = dr.async_get(hass)
    via = registry.async_get_or_create(
        config_entry_id="123",
        connections=set(),
        identifiers={("hue", "via-id")},
        manufacturer="manufacturer",
        model="via",
    )

    async def async_setup_entry(hass, config_entry, async_add_entities):
        """Mock setup entry method."""
        async_add_entities(
            [
                # Invalid device info
                MockEntity(unique_id="abcd", device_info={}),
                # Valid device info
                MockEntity(
                    unique_id="qwer",
                    device_info={
                        "identifiers": {("hue", "1234")},
                        "configuration_url": "http://192.168.0.100/config",
                        "connections": {(dr.CONNECTION_NETWORK_MAC, "abcd")},
                        "manufacturer": "test-manuf",
                        "model": "test-model",
                        "name": "test-name",
                        "sw_version": "test-sw",
                        "suggested_area": "Heliport",
                        "entry_type": dr.DeviceEntryType.SERVICE,
                        "via_device": ("hue", "via-id"),
                    },
                ),
            ]
        )
        return True

    platform = MockPlatform(async_setup_entry=async_setup_entry)
    config_entry = MockConfigEntry(entry_id="super-mock-id")
    entity_platform = MockEntityPlatform(
        hass, platform_name=config_entry.domain, platform=platform
    )

    assert await entity_platform.async_setup_entry(config_entry)
    await hass.async_block_till_done()

    assert len(hass.states.async_entity_ids()) == 2

    device = registry.async_get_device({("hue", "1234")})
    assert device is not None
    assert device.identifiers == {("hue", "1234")}
    assert device.configuration_url == "http://192.168.0.100/config"
    assert device.connections == {(dr.CONNECTION_NETWORK_MAC, "abcd")}
    assert device.entry_type is dr.DeviceEntryType.SERVICE
    assert device.manufacturer == "test-manuf"
    assert device.model == "test-model"
    assert device.name == "test-name"
    assert device.suggested_area == "Heliport"
    assert device.sw_version == "test-sw"
    assert device.via_device_id == via.id
Пример #10
0
async def test_update_entity_id(hass, client):
    """Test update entity id."""
    mock_registry(
        hass,
        {
            'test_domain.world':
            RegistryEntry(
                entity_id='test_domain.world',
                unique_id='1234',
                # Using component.async_add_entities is equal to platform "domain"
                platform='test_platform',
            )
        })
    platform = MockEntityPlatform(hass)
    entity = MockEntity(unique_id='1234')
    await platform.async_add_entities([entity])

    assert hass.states.get('test_domain.world') is not None

    await client.send_json({
        'id': 6,
        'type': 'config/entity_registry/update',
        'entity_id': 'test_domain.world',
        'new_entity_id': 'test_domain.planet',
    })

    msg = await client.receive_json()

    assert msg['result'] == {'entity_id': 'test_domain.planet', 'name': None}

    assert hass.states.get('test_domain.world') is None
    assert hass.states.get('test_domain.planet') is not None
Пример #11
0
async def test_trigger_invalid_entity_id(hass, caplog, client):
    """Test turn on trigger using invalid entity_id."""
    await setup_webostv(hass)

    platform = MockEntityPlatform(hass)

    invalid_entity = f"{DOMAIN}.invalid"
    await platform.async_add_entities([MockEntity(name=invalid_entity)])

    await async_setup_component(
        hass,
        automation.DOMAIN,
        {
            automation.DOMAIN: [
                {
                    "trigger": {
                        "platform": "webostv.turn_on",
                        "entity_id": invalid_entity,
                    },
                    "action": {
                        "service": "test.automation",
                        "data_template": {
                            "some": ENTITY_ID,
                            "id": "{{ trigger.id }}",
                        },
                    },
                },
            ],
        },
    )

    assert (
        f"ValueError: Entity {invalid_entity} is not a valid webostv entity"
        in caplog.text)
Пример #12
0
async def test_entity_disabled_by_device(hass: HomeAssistant):
    """Test entity disabled by device."""

    connections = {(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}
    entity_disabled = MockEntity(
        unique_id="disabled", device_info=DeviceInfo(connections=connections))

    async def async_setup_entry(hass, config_entry, async_add_entities):
        """Mock setup entry method."""
        async_add_entities([entity_disabled])
        return True

    platform = MockPlatform(async_setup_entry=async_setup_entry)
    config_entry = MockConfigEntry(entry_id="super-mock-id", domain=DOMAIN)
    entity_platform = MockEntityPlatform(hass,
                                         platform_name=config_entry.domain,
                                         platform=platform)

    device_registry = dr.async_get(hass)
    device_registry.async_get_or_create(
        config_entry_id=config_entry.entry_id,
        connections=connections,
        disabled_by=dr.DeviceEntryDisabler.USER,
    )

    assert await entity_platform.async_setup_entry(config_entry)
    await hass.async_block_till_done()

    assert entity_disabled.hass is None
    assert entity_disabled.platform is None

    registry = er.async_get(hass)

    entry_disabled = registry.async_get_or_create(DOMAIN, DOMAIN, "disabled")
    assert entry_disabled.disabled_by is er.RegistryEntryDisabler.DEVICE
Пример #13
0
async def test_friendly_name(hass, has_entity_name, entity_name,
                             expected_friendly_name):
    """Test entity_id is influenced by entity name."""
    async def async_setup_entry(hass, config_entry, async_add_entities):
        """Mock setup entry method."""
        async_add_entities([
            MockEntity(
                unique_id="qwer",
                device_info={
                    "identifiers": {("hue", "1234")},
                    "connections": {(dr.CONNECTION_NETWORK_MAC, "abcd")},
                    "name": "Device Bla",
                },
                has_entity_name=has_entity_name,
                name=entity_name,
            ),
        ])
        return True

    platform = MockPlatform(async_setup_entry=async_setup_entry)
    config_entry = MockConfigEntry(entry_id="super-mock-id")
    entity_platform = MockEntityPlatform(hass,
                                         platform_name=config_entry.domain,
                                         platform=platform)

    assert await entity_platform.async_setup_entry(config_entry)
    await hass.async_block_till_done()

    assert len(hass.states.async_entity_ids()) == 1
    state = hass.states.async_all()[0]
    assert state.attributes.get(ATTR_FRIENDLY_NAME) == expected_friendly_name
Пример #14
0
async def test_setup_entry_with_entities_that_block_forever(hass, caplog):
    """Test we cancel adding entities when we reach the timeout."""
    registry = mock_registry(hass)

    async def async_setup_entry(hass, config_entry, async_add_entities):
        """Mock setup entry method."""
        async_add_entities(
            [MockBlockingEntity(name="test1", unique_id="unique")])
        return True

    platform = MockPlatform(async_setup_entry=async_setup_entry)
    config_entry = MockConfigEntry(entry_id="super-mock-id")
    mock_entity_platform = MockEntityPlatform(
        hass, platform_name=config_entry.domain, platform=platform)

    with patch.object(entity_platform, "SLOW_ADD_ENTITY_MAX_WAIT",
                      0.01), patch.object(entity_platform,
                                          "SLOW_ADD_MIN_TIMEOUT", 0.01):
        assert await mock_entity_platform.async_setup_entry(config_entry)
        await hass.async_block_till_done()
    full_name = f"{mock_entity_platform.domain}.{config_entry.domain}"
    assert full_name in hass.config.components
    assert len(hass.states.async_entity_ids()) == 0
    assert len(registry.entities) == 1
    assert "Timed out adding entities" in caplog.text
    assert "test_domain.test1" in caplog.text
    assert "test_domain" in caplog.text
    assert "test" in caplog.text
Пример #15
0
async def test_update_entity_no_changes(hass, client):
    """Test get entry."""
    mock_registry(
        hass,
        {
            'test_domain.world':
            RegistryEntry(
                entity_id='test_domain.world',
                unique_id='1234',
                # Using component.async_add_entities is equal to platform "domain"
                platform='test_platform',
                name='name of entity')
        })
    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 == 'name of entity'

    resp = await client.post('/api/config/entity_registry/test_domain.world',
                             json={'name': 'name of entity'})
    assert resp.status == 200
    data = await resp.json()
    assert data == {'entity_id': 'test_domain.world', 'name': 'name of entity'}

    state = hass.states.get('test_domain.world')
    assert state.name == 'name of entity'
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
async def test_registry_respect_entity_namespace(hass):
    """Test that the registry respects entity namespace."""
    mock_registry(hass)
    platform = MockEntityPlatform(hass, entity_namespace="ns")
    entity = MockEntity(unique_id="1234", name="Device Name")
    await platform.async_add_entities([entity])
    assert entity.entity_id == "test_domain.ns_device_name"
Пример #18
0
async def test_invalid_entity_id(opp):
    """Test specifying an invalid entity id."""
    platform = MockEntityPlatform(opp)
    entity = MockEntity(entity_id="invalid_entity_id")
    with pytest.raises(OpenPeerPowerError):
        await platform.async_add_entities([entity])
    assert entity.opp is None
    assert entity.platform is None
Пример #19
0
async def test_invalid_entity_id(hass):
    """Test specifying an invalid entity id."""
    platform = MockEntityPlatform(hass)
    entity = MockEntity(entity_id="invalid_entity_id")
    with pytest.raises(HomeAssistantError):
        await platform.async_add_entities([entity])
    assert entity.hass is None
    assert entity.platform is None
Пример #20
0
async def test_update_entity_no_changes(hass, client):
    """Test update entity with no changes."""
    mock_registry(
        hass,
        {
            "test_domain.world": RegistryEntry(
                entity_id="test_domain.world",
                unique_id="1234",
                # Using component.async_add_entities is equal to platform "domain"
                platform="test_platform",
                name="name of entity",
            )
        },
    )
    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 == "name of entity"

    await client.send_json(
        {
            "id": 6,
            "type": "config/entity_registry/update",
            "entity_id": "test_domain.world",
            "name": "name of entity",
        }
    )

    msg = await client.receive_json()

    assert msg["result"] == {
        "entity_entry": {
            "area_id": None,
            "capabilities": None,
            "config_entry_id": None,
            "device_class": None,
            "device_id": None,
            "disabled_by": None,
            "entity_category": None,
            "entity_id": "test_domain.world",
            "hidden_by": None,
            "icon": None,
            "has_entity_name": False,
            "name": "name of entity",
            "options": {},
            "original_device_class": None,
            "original_icon": None,
            "original_name": None,
            "platform": "test_platform",
            "unique_id": "1234",
        }
    }

    state = hass.states.get("test_domain.world")
    assert state.name == "name of entity"
Пример #21
0
async def test_polling_disabled_by_config_entry(hass):
    """Test the polling of only updated entities."""
    entity_platform = MockEntityPlatform(hass)
    entity_platform.config_entry = MockConfigEntry(pref_disable_polling=True)

    poll_ent = MockEntity(should_poll=True)

    await entity_platform.async_add_entities([poll_ent])
    assert entity_platform._async_unsub_polling is None
Пример #22
0
async def test_update_entity_require_restart(hass, client):
    """Test updating entity."""
    config_entry = MockConfigEntry(domain="test_platform")
    config_entry.add_to_hass(hass)
    mock_registry(
        hass,
        {
            "test_domain.world": RegistryEntry(
                config_entry_id=config_entry.entry_id,
                entity_id="test_domain.world",
                unique_id="1234",
                # Using component.async_add_entities is equal to platform "domain"
                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

    # UPDATE DISABLED_BY TO NONE
    await client.send_json(
        {
            "id": 8,
            "type": "config/entity_registry/update",
            "entity_id": "test_domain.world",
            "disabled_by": None,
        }
    )

    msg = await client.receive_json()

    assert msg["result"] == {
        "entity_entry": {
            "area_id": None,
            "capabilities": None,
            "config_entry_id": config_entry.entry_id,
            "device_class": None,
            "device_id": None,
            "disabled_by": None,
            "entity_category": None,
            "entity_id": "test_domain.world",
            "icon": None,
            "hidden_by": None,
            "name": None,
            "options": {},
            "original_device_class": None,
            "original_icon": None,
            "original_name": None,
            "platform": "test_platform",
            "unique_id": "1234",
        },
        "require_restart": True,
    }
Пример #23
0
async def test_device_info_called(hass):
    """Test device info is forwarded correctly."""
    registry = await hass.helpers.device_registry.async_get_registry()
    via = registry.async_get_or_create(
        config_entry_id="123",
        connections=set(),
        identifiers={("hue", "via-id")},
        manufacturer="manufacturer",
        model="via",
    )

    async def async_setup_entry(hass, config_entry, async_add_entities):
        """Mock setup entry method."""
        async_add_entities(
            [
                # Invalid device info
                MockEntity(unique_id="abcd", device_info={}),
                # Valid device info
                MockEntity(
                    unique_id="qwer",
                    device_info={
                        "identifiers": {("hue", "1234")},
                        "connections": {("mac", "abcd")},
                        "manufacturer": "test-manuf",
                        "model": "test-model",
                        "name": "test-name",
                        "sw_version": "test-sw",
                        "entry_type": "service",
                        "via_device": ("hue", "via-id"),
                    },
                ),
            ]
        )
        return True

    platform = MockPlatform(async_setup_entry=async_setup_entry)
    config_entry = MockConfigEntry(entry_id="super-mock-id")
    entity_platform = MockEntityPlatform(
        hass, platform_name=config_entry.domain, platform=platform
    )

    assert await entity_platform.async_setup_entry(config_entry)
    await hass.async_block_till_done()

    assert len(hass.states.async_entity_ids()) == 2

    device = registry.async_get_device({("hue", "1234")}, set())
    assert device is not None
    assert device.identifiers == {("hue", "1234")}
    assert device.connections == {("mac", "abcd")}
    assert device.manufacturer == "test-manuf"
    assert device.model == "test-model"
    assert device.name == "test-name"
    assert device.sw_version == "test-sw"
    assert device.entry_type == "service"
    assert device.via_device_id == via.id
Пример #24
0
async def test_platforms_sharing_services(hass):
    """Test platforms share services."""
    entity_platform1 = MockEntityPlatform(hass,
                                          domain="mock_integration",
                                          platform_name="mock_platform",
                                          platform=None)
    entity1 = MockEntity(entity_id="mock_integration.entity_1")
    await entity_platform1.async_add_entities([entity1])

    entity_platform2 = MockEntityPlatform(hass,
                                          domain="mock_integration",
                                          platform_name="mock_platform",
                                          platform=None)
    entity2 = MockEntity(entity_id="mock_integration.entity_2")
    await entity_platform2.async_add_entities([entity2])

    entity_platform3 = MockEntityPlatform(
        hass,
        domain="different_integration",
        platform_name="mock_platform",
        platform=None,
    )
    entity3 = MockEntity(entity_id="different_integration.entity_3")
    await entity_platform3.async_add_entities([entity3])

    entities = []

    @callback
    def handle_service(entity, data):
        entities.append(entity)

    entity_platform1.async_register_entity_service("hello", {}, handle_service)
    entity_platform2.async_register_entity_service(
        "hello", {}, Mock(side_effect=AssertionError("Should not be called")))

    await hass.services.async_call("mock_platform",
                                   "hello", {"entity_id": "all"},
                                   blocking=True)

    assert len(entities) == 2
    assert entity1 in entities
    assert entity2 in entities
Пример #25
0
async def test_platform_with_no_setup(hass, caplog):
    """Test setting up a platform that does not support setup."""
    entity_platform = MockEntityPlatform(hass,
                                         domain="mock-integration",
                                         platform_name="mock-platform",
                                         platform=None)

    await entity_platform.async_setup(None)

    assert (
        "The mock-platform platform for the mock-integration integration does not support platform setup."
        in caplog.text)
async def test_device_info_called(hass):
    """Test device info is forwarded correctly."""
    registry = await hass.helpers.device_registry.async_get_registry()
    hub = registry.async_get_or_create(
        config_entry_id='123',
        connections=set(),
        identifiers={('hue', 'hub-id')},
        manufacturer='manufacturer', model='hub'
    )

    async def async_setup_entry(hass, config_entry, async_add_entities):
        """Mock setup entry method."""
        async_add_entities([
            # Invalid device info
            MockEntity(unique_id='abcd', device_info={}),
            # Valid device info
            MockEntity(unique_id='qwer', device_info={
                'identifiers': {('hue', '1234')},
                'connections': {('mac', 'abcd')},
                'manufacturer': 'test-manuf',
                'model': 'test-model',
                'name': 'test-name',
                'sw_version': 'test-sw',
                'via_hub': ('hue', 'hub-id'),
            }),
        ])
        return True

    platform = MockPlatform(
        async_setup_entry=async_setup_entry
    )
    config_entry = MockConfigEntry(entry_id='super-mock-id')
    entity_platform = MockEntityPlatform(
        hass,
        platform_name=config_entry.domain,
        platform=platform
    )

    assert await entity_platform.async_setup_entry(config_entry)
    await hass.async_block_till_done()

    assert len(hass.states.async_entity_ids()) == 2

    device = registry.async_get_device({('hue', '1234')}, set())
    assert device is not None
    assert device.identifiers == {('hue', '1234')}
    assert device.connections == {('mac', 'abcd')}
    assert device.manufacturer == 'test-manuf'
    assert device.model == 'test-model'
    assert device.name == 'test-name'
    assert device.sw_version == 'test-sw'
    assert device.hub_device_id == hub.id
async def test_update_entity_id(hass, client):
    """Test update entity id."""
    mock_registry(
        hass,
        {
            "test_domain.world":
            RegistryEntry(
                entity_id="test_domain.world",
                unique_id="1234",
                # Using component.async_add_entities is equal to platform "domain"
                platform="test_platform",
            )
        },
    )
    platform = MockEntityPlatform(hass)
    entity = MockEntity(unique_id="1234")
    await platform.async_add_entities([entity])

    assert hass.states.get("test_domain.world") is not None

    await client.send_json({
        "id": 6,
        "type": "config/entity_registry/update",
        "entity_id": "test_domain.world",
        "new_entity_id": "test_domain.planet",
    })

    msg = await client.receive_json()

    assert msg["result"] == {
        "entity_entry": {
            "area_id": None,
            "capabilities": None,
            "config_entry_id": None,
            "device_class": None,
            "device_id": None,
            "disabled_by": None,
            "entity_category": None,
            "entity_id": "test_domain.planet",
            "icon": None,
            "name": None,
            "original_device_class": None,
            "original_icon": None,
            "original_name": None,
            "platform": "test_platform",
            "unique_id": "1234",
        }
    }

    assert hass.states.get("test_domain.world") is None
    assert hass.states.get("test_domain.planet") is not None
async def test_setup_entry(hass):
    """Test we can setup an entry."""
    async_setup_entry = Mock(return_value=mock_coro(True))
    platform = MockPlatform(async_setup_entry=async_setup_entry)
    config_entry = MockConfigEntry()
    entity_platform = MockEntityPlatform(hass,
                                         platform_name=config_entry.domain,
                                         platform=platform)

    assert await entity_platform.async_setup_entry(config_entry)

    full_name = '{}.{}'.format(entity_platform.domain, config_entry.domain)
    assert full_name in hass.config.components
    assert len(async_setup_entry.mock_calls) == 1
Пример #29
0
async def test_two_platforms_add_same_entity(hass):
    """Test two platforms in the same domain adding an entity with the same name."""
    entity_platform1 = MockEntityPlatform(hass,
                                          domain="mock_integration",
                                          platform_name="mock_platform",
                                          platform=None)
    entity1 = SlowEntity(name="entity_1")

    entity_platform2 = MockEntityPlatform(hass,
                                          domain="mock_integration",
                                          platform_name="mock_platform",
                                          platform=None)
    entity2 = SlowEntity(name="entity_1")

    await asyncio.gather(
        entity_platform1.async_add_entities([entity1]),
        entity_platform2.async_add_entities([entity2]),
    )

    entities = []

    @callback
    def handle_service(entity, *_):
        entities.append(entity)

    entity_platform1.async_register_entity_service("hello", {}, handle_service)
    await hass.services.async_call("mock_platform",
                                   "hello", {"entity_id": "all"},
                                   blocking=True)

    assert len(entities) == 2
    assert {entity1.entity_id, entity2.entity_id} == {
        "mock_integration.entity_1",
        "mock_integration.entity_1_2",
    }
    assert entity1 in entities
    assert entity2 in entities
Пример #30
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