コード例 #1
0
async def test_get_entity(hass, client):
    """Test get entry."""
    mock_registry(
        hass, {
            'test_domain.name':
            RegistryEntry(entity_id='test_domain.name',
                          unique_id='1234',
                          platform='test_platform',
                          name='Hello World'),
            'test_domain.no_name':
            RegistryEntry(
                entity_id='test_domain.no_name',
                unique_id='6789',
                platform='test_platform',
            ),
        })

    resp = await client.get('/api/config/entity_registry/test_domain.name')
    assert resp.status == 200
    data = await resp.json()
    assert data == {'entity_id': 'test_domain.name', 'name': 'Hello World'}

    resp = await client.get('/api/config/entity_registry/test_domain.no_name')
    assert resp.status == 200
    data = await resp.json()
    assert data == {'entity_id': 'test_domain.no_name', 'name': None}
コード例 #2
0
async def test_update_existing_entity_id(hass, client):
    """Test update entity id to an already registered 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",
            ),
            "test_domain.planet": RegistryEntry(
                entity_id="test_domain.planet",
                unique_id="2345",
                # 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": "test_domain.planet",
        }
    )

    msg = await client.receive_json()

    assert not msg["success"]
コード例 #3
0
def test_entities_device_id_boolean(hass):
    """Test entity ID policy applying control on device id."""
    entity_registry = mock_registry(hass, {
        'test_domain.allowed': RegistryEntry(
            entity_id='test_domain.allowed',
            unique_id='1234',
            platform='test_platform',
            device_id='mock-allowed-dev-id'
        ),
        'test_domain.not_allowed': RegistryEntry(
            entity_id='test_domain.not_allowed',
            unique_id='5678',
            platform='test_platform',
            device_id='mock-not-allowed-dev-id'
        ),
    })
    device_registry = mock_device_registry(hass)

    policy = {
        'device_ids': {
            'mock-allowed-dev-id': {
                'read': True,
            }
        }
    }
    ENTITY_POLICY_SCHEMA(policy)
    compiled = compile_entities(policy, PermissionLookup(
        entity_registry, device_registry
    ))
    assert compiled('test_domain.allowed', 'read') is True
    assert compiled('test_domain.allowed', 'control') is False
    assert compiled('test_domain.not_allowed', 'read') is False
    assert compiled('test_domain.not_allowed', 'control') is False
コード例 #4
0
def test_entities_device_id_boolean(hass):
    """Test entity ID policy applying control on device id."""
    entity_registry = mock_registry(
        hass,
        {
            "test_domain.allowed":
            RegistryEntry(
                entity_id="test_domain.allowed",
                unique_id="1234",
                platform="test_platform",
                device_id="mock-allowed-dev-id",
            ),
            "test_domain.not_allowed":
            RegistryEntry(
                entity_id="test_domain.not_allowed",
                unique_id="5678",
                platform="test_platform",
                device_id="mock-not-allowed-dev-id",
            ),
        },
    )
    device_registry = mock_device_registry(hass)

    policy = {"device_ids": {"mock-allowed-dev-id": {"read": True}}}
    ENTITY_POLICY_SCHEMA(policy)
    compiled = compile_entities(
        policy, PermissionLookup(entity_registry, device_registry))
    assert compiled("test_domain.allowed", "read") is True
    assert compiled("test_domain.allowed", "control") is False
    assert compiled("test_domain.not_allowed", "read") is False
    assert compiled("test_domain.not_allowed", "control") is False
コード例 #5
0
async def test_get_entity(hass, client):
    """Test get entry."""
    mock_registry(
        hass,
        {
            "test_domain.name": RegistryEntry(
                entity_id="test_domain.name",
                unique_id="1234",
                platform="test_platform",
                name="Hello World",
            ),
            "test_domain.no_name": RegistryEntry(
                entity_id="test_domain.no_name",
                unique_id="6789",
                platform="test_platform",
            ),
        },
    )

    await client.send_json(
        {"id": 5, "type": "config/entity_registry/get", "entity_id": "test_domain.name"}
    )
    msg = await client.receive_json()

    assert msg["result"] == {
        "config_entry_id": None,
        "device_id": None,
        "disabled_by": None,
        "platform": "test_platform",
        "entity_id": "test_domain.name",
        "name": "Hello World",
        "icon": None,
        "original_name": None,
        "original_icon": None,
    }

    await client.send_json(
        {
            "id": 6,
            "type": "config/entity_registry/get",
            "entity_id": "test_domain.no_name",
        }
    )
    msg = await client.receive_json()

    assert msg["result"] == {
        "config_entry_id": None,
        "device_id": None,
        "disabled_by": None,
        "platform": "test_platform",
        "entity_id": "test_domain.no_name",
        "name": None,
        "icon": None,
        "original_name": None,
        "original_icon": None,
    }
コード例 #6
0
async def test_get_entity(hass, client):
    """Test get entry."""
    mock_registry(
        hass, {
            'test_domain.name':
            RegistryEntry(entity_id='test_domain.name',
                          unique_id='1234',
                          platform='test_platform',
                          name='Hello World'),
            'test_domain.no_name':
            RegistryEntry(
                entity_id='test_domain.no_name',
                unique_id='6789',
                platform='test_platform',
            ),
        })

    await client.send_json({
        'id': 5,
        'type': 'config/entity_registry/get',
        'entity_id': 'test_domain.name',
    })
    msg = await client.receive_json()

    assert msg['result'] == {
        'config_entry_id': None,
        'device_id': None,
        'disabled_by': None,
        'platform': 'test_platform',
        'entity_id': 'test_domain.name',
        'name': 'Hello World'
    }

    await client.send_json({
        'id': 6,
        'type': 'config/entity_registry/get',
        'entity_id': 'test_domain.no_name',
    })
    msg = await client.receive_json()

    assert msg['result'] == {
        'config_entry_id': None,
        'device_id': None,
        'disabled_by': None,
        'platform': 'test_platform',
        'entity_id': 'test_domain.no_name',
        'name': None
    }
コード例 #7
0
async def test_list_entities(hass, client):
    """Test list entries."""
    mock_registry(
        hass,
        {
            "test_domain.name": RegistryEntry(
                entity_id="test_domain.name",
                unique_id="1234",
                platform="test_platform",
                name="Hello World",
            ),
            "test_domain.no_name": RegistryEntry(
                entity_id="test_domain.no_name",
                unique_id="6789",
                platform="test_platform",
            ),
        },
    )

    await client.send_json({"id": 5, "type": "config/entity_registry/list"})
    msg = await client.receive_json()

    assert msg["result"] == [
        {
            "config_entry_id": None,
            "device_id": None,
            "area_id": None,
            "disabled_by": None,
            "entity_id": "test_domain.name",
            "hidden_by": None,
            "name": "Hello World",
            "icon": None,
            "platform": "test_platform",
            "entity_category": None,
        },
        {
            "config_entry_id": None,
            "device_id": None,
            "area_id": None,
            "disabled_by": None,
            "entity_id": "test_domain.no_name",
            "hidden_by": None,
            "name": None,
            "icon": None,
            "platform": "test_platform",
            "entity_category": None,
        },
    ]
コード例 #8
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'
コード例 #9
0
def test_entities_areas_area_true(hass):
    """Test entity ID policy for areas with specific area."""
    entity_registry = mock_registry(
        hass,
        {
            "light.kitchen":
            RegistryEntry(
                entity_id="light.kitchen",
                unique_id="1234",
                platform="test_platform",
                device_id="mock-dev-id",
            )
        },
    )
    device_registry = mock_device_registry(
        hass,
        {"mock-dev-id": DeviceEntry(id="mock-dev-id", area_id="mock-area-id")})

    policy = {"area_ids": {"mock-area-id": {"read": True, "control": True}}}
    ENTITY_POLICY_SCHEMA(policy)
    compiled = compile_entities(
        policy, PermissionLookup(entity_registry, device_registry))
    assert compiled("light.kitchen", "read") is True
    assert compiled("light.kitchen", "control") is True
    assert compiled("light.kitchen", "edit") is False
    assert compiled("switch.kitchen", "read") is False
コード例 #10
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'

    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'
コード例 #11
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
コード例 #12
0
async def test_remove_entity(hass, client):
    """Test removing entity."""
    registry = 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="before update",
            )
        },
    )

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

    msg = await client.receive_json()

    assert msg["success"]
    assert len(registry.entities) == 0
コード例 #13
0
def test_entities_areas_area_true(hass):
    """Test entity ID policy for areas with specific area."""
    entity_registry = mock_registry(hass, {
        'light.kitchen': RegistryEntry(
            entity_id='light.kitchen',
            unique_id='1234',
            platform='test_platform',
            device_id='mock-dev-id'
        ),
    })
    device_registry = mock_device_registry(hass, {
        'mock-dev-id': DeviceEntry(
            id='mock-dev-id',
            area_id='mock-area-id'
        )
    })

    policy = {
        'area_ids': {
            'mock-area-id': {
                'read': True,
                'control': True,
            }
        }
    }
    ENTITY_POLICY_SCHEMA(policy)
    compiled = compile_entities(policy, PermissionLookup(
        entity_registry, device_registry
    ))
    assert compiled('light.kitchen', 'read') is True
    assert compiled('light.kitchen', 'control') is True
    assert compiled('light.kitchen', 'edit') is False
    assert compiled('switch.kitchen', 'read') is False
コード例 #14
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"
コード例 #15
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,
    }
コード例 #16
0
async def test_list_entities(hass, client):
    """Test list entries."""
    entities = OrderedDict()
    entities["test_domain.name"] = RegistryEntry(
        entity_id="test_domain.name",
        unique_id="1234",
        platform="test_platform",
        name="Hello World",
    )
    entities["test_domain.no_name"] = RegistryEntry(
        entity_id="test_domain.no_name", unique_id="6789", platform="test_platform"
    )

    mock_registry(hass, entities)

    await client.send_json({"id": 5, "type": "config/entity_registry/list"})
    msg = await client.receive_json()

    assert msg["result"] == [
        {
            "config_entry_id": None,
            "device_id": None,
            "disabled_by": None,
            "entity_id": "test_domain.name",
            "name": "Hello World",
            "icon": None,
            "original_name": None,
            "original_icon": None,
            "platform": "test_platform",
        },
        {
            "config_entry_id": None,
            "device_id": None,
            "disabled_by": None,
            "entity_id": "test_domain.no_name",
            "name": None,
            "icon": None,
            "original_name": None,
            "original_icon": None,
            "platform": "test_platform",
        },
    ]
コード例 #17
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_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
コード例 #18
0
async def test_list_entities(hass, client):
    """Test list entries."""
    entities = OrderedDict()
    entities['test_domain.name'] = RegistryEntry(entity_id='test_domain.name',
                                                 unique_id='1234',
                                                 platform='test_platform',
                                                 name='Hello World')
    entities['test_domain.no_name'] = RegistryEntry(
        entity_id='test_domain.no_name',
        unique_id='6789',
        platform='test_platform',
    )

    mock_registry(hass, entities)

    await client.send_json({
        'id': 5,
        'type': 'config/entity_registry/list',
    })
    msg = await client.receive_json()

    assert msg['result'] == [{
        'config_entry_id': None,
        'device_id': None,
        'disabled_by': None,
        'entity_id': 'test_domain.name',
        'name': 'Hello World',
        'platform': 'test_platform',
    }, {
        'config_entry_id': None,
        'device_id': None,
        'disabled_by': None,
        'entity_id': 'test_domain.no_name',
        'name': None,
        'platform': 'test_platform',
    }]
コード例 #19
0
async def test_enable_entity_disabled_device(hass, client, device_registry):
    """Test enabling entity of disabled device."""
    config_entry = MockConfigEntry(domain="test_platform")
    config_entry.add_to_hass(hass)

    device = device_registry.async_get_or_create(
        config_entry_id="1234",
        connections={("ethernet", "12:34:56:78:90:AB:CD:EF")},
        identifiers={("bridgeid", "0123")},
        manufacturer="manufacturer",
        model="model",
        disabled_by=DISABLED_USER,
    )

    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",
                device_id=device.id,
            )
        },
    )
    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 not msg["success"]
コード例 #20
0
def assert_expected_properties(
    hass: HomeAssistant,
    registry: er.RegistryEntry,
    name,
    unique_id,
    state_value,
    attributes: dict,
):
    """Assert expected properties from a dict."""

    entry = registry.async_get(name)
    assert entry.unique_id == unique_id
    state = hass.states.get(name)
    assert state
    assert state.state == state_value
    for attr, value in attributes.items():
        assert state.attributes.get(attr) == value
コード例 #21
0
async def test_update_entity_name(hass, client):
    """Test updating entity name."""
    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="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"

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

    msg = await client.receive_json()

    assert msg["result"] == {
        "config_entry_id": None,
        "device_id": None,
        "disabled_by": None,
        "platform": "test_platform",
        "entity_id": "test_domain.world",
        "name": "after update",
    }

    state = hass.states.get("test_domain.world")
    assert state.name == "after update"
コード例 #22
0
async def test_update_entity_name(hass, client):
    """Test updating entity name."""
    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='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'

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

    msg = await client.receive_json()

    assert msg['result'] == {
        'config_entry_id': None,
        'device_id': None,
        'disabled_by': None,
        'platform': 'test_platform',
        'entity_id': 'test_domain.world',
        'name': 'after update'
    }

    state = hass.states.get('test_domain.world')
    assert state.name == 'after update'
コード例 #23
0
async def test_remove_entity(hass, client):
    """Test removing entity."""
    registry = 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='before update')
        })

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

    msg = await client.receive_json()

    assert msg['success']
    assert len(registry.entities) == 0
コード例 #24
0
async def test_update_entity(hass, client):
    """Test updating entity."""
    registry = 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="before update",
                icon="icon: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"
    assert state.attributes[ATTR_ICON] == "icon:before update"

    # UPDATE NAME & ICON & AREA
    await client.send_json(
        {
            "id": 6,
            "type": "config/entity_registry/update",
            "entity_id": "test_domain.world",
            "name": "after update",
            "icon": "icon:after update",
            "area_id": "mock-area-id",
        }
    )

    msg = await client.receive_json()

    assert msg["result"] == {
        "entity_entry": {
            "config_entry_id": None,
            "device_id": None,
            "area_id": "mock-area-id",
            "disabled_by": None,
            "platform": "test_platform",
            "entity_id": "test_domain.world",
            "name": "after update",
            "icon": "icon:after update",
            "original_name": None,
            "original_icon": None,
            "capabilities": None,
            "unique_id": "1234",
        }
    }

    state = hass.states.get("test_domain.world")
    assert state.name == "after update"
    assert state.attributes[ATTR_ICON] == "icon:after update"

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

    msg = await client.receive_json()

    assert hass.states.get("test_domain.world") is None
    assert registry.entities["test_domain.world"].disabled_by == DISABLED_USER

    # 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": {
            "config_entry_id": None,
            "device_id": None,
            "area_id": "mock-area-id",
            "disabled_by": None,
            "platform": "test_platform",
            "entity_id": "test_domain.world",
            "name": "after update",
            "icon": "icon:after update",
            "original_name": None,
            "original_icon": None,
            "capabilities": None,
            "unique_id": "1234",
        },
        "reload_delay": 30,
    }
コード例 #25
0
async def test_update_entity(hass, client):
    """Test updating entity."""
    registry = 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="before update",
                icon="icon: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"
    assert state.attributes[ATTR_ICON] == "icon:before update"

    # UPDATE AREA, DEVICE_CLASS, HIDDEN_BY, ICON AND NAME
    await client.send_json(
        {
            "id": 6,
            "type": "config/entity_registry/update",
            "entity_id": "test_domain.world",
            "area_id": "mock-area-id",
            "device_class": "custom_device_class",
            "hidden_by": "user",  # We exchange strings over the WS API, not enums
            "icon": "icon:after update",
            "name": "after update",
        }
    )

    msg = await client.receive_json()

    assert msg["result"] == {
        "entity_entry": {
            "area_id": "mock-area-id",
            "capabilities": None,
            "config_entry_id": None,
            "device_class": "custom_device_class",
            "device_id": None,
            "disabled_by": None,
            "entity_category": None,
            "entity_id": "test_domain.world",
            "hidden_by": "user",  # We exchange strings over the WS API, not enums
            "icon": "icon:after update",
            "name": "after update",
            "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 == "after update"
    assert state.attributes[ATTR_ICON] == "icon:after update"

    # UPDATE HIDDEN_BY TO ILLEGAL VALUE
    await client.send_json(
        {
            "id": 7,
            "type": "config/entity_registry/update",
            "entity_id": "test_domain.world",
            "hidden_by": "ivy",
        }
    )

    msg = await client.receive_json()
    assert not msg["success"]

    assert registry.entities["test_domain.world"].hidden_by is RegistryEntryHider.USER

    # UPDATE DISABLED_BY TO USER
    await client.send_json(
        {
            "id": 8,
            "type": "config/entity_registry/update",
            "entity_id": "test_domain.world",
            "disabled_by": "user",  # We exchange strings over the WS API, not enums
        }
    )

    msg = await client.receive_json()
    assert msg["success"]

    assert hass.states.get("test_domain.world") is None
    assert (
        registry.entities["test_domain.world"].disabled_by is RegistryEntryDisabler.USER
    )

    # UPDATE DISABLED_BY TO NONE
    await client.send_json(
        {
            "id": 9,
            "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": "mock-area-id",
            "capabilities": None,
            "config_entry_id": None,
            "device_class": "custom_device_class",
            "device_id": None,
            "disabled_by": None,
            "entity_category": None,
            "entity_id": "test_domain.world",
            "hidden_by": "user",  # We exchange strings over the WS API, not enums
            "icon": "icon:after update",
            "name": "after update",
            "options": {},
            "original_device_class": None,
            "original_icon": None,
            "original_name": None,
            "platform": "test_platform",
            "unique_id": "1234",
        },
        "reload_delay": 30,
    }

    # UPDATE ENTITY OPTION
    await client.send_json(
        {
            "id": 10,
            "type": "config/entity_registry/update",
            "entity_id": "test_domain.world",
            "options_domain": "sensor",
            "options": {"unit_of_measurement": "beard_second"},
        }
    )

    msg = await client.receive_json()

    assert msg["result"] == {
        "entity_entry": {
            "area_id": "mock-area-id",
            "capabilities": None,
            "config_entry_id": None,
            "device_class": "custom_device_class",
            "device_id": None,
            "disabled_by": None,
            "entity_category": None,
            "entity_id": "test_domain.world",
            "hidden_by": "user",  # We exchange strings over the WS API, not enums
            "icon": "icon:after update",
            "name": "after update",
            "options": {"sensor": {"unit_of_measurement": "beard_second"}},
            "original_device_class": None,
            "original_icon": None,
            "original_name": None,
            "platform": "test_platform",
            "unique_id": "1234",
        },
    }
コード例 #26
0
def zwave_migration_data_fixture(hass):
    """Return mock zwave migration data."""
    zwave_source_node_device = DeviceEntry(
        id=ZWAVE_SOURCE_NODE_DEVICE_ID,
        name_by_user=ZWAVE_SOURCE_NODE_DEVICE_NAME,
        area_id=ZWAVE_SOURCE_NODE_DEVICE_AREA,
    )
    zwave_source_node_entry = RegistryEntry(
        entity_id=ZWAVE_SOURCE_ENTITY,
        unique_id=ZWAVE_SOURCE_NODE_UNIQUE_ID,
        platform="zwave",
        name="Z-Wave Source Node",
    )
    zwave_battery_device = DeviceEntry(
        id=ZWAVE_BATTERY_DEVICE_ID,
        name_by_user=ZWAVE_BATTERY_DEVICE_NAME,
        area_id=ZWAVE_BATTERY_DEVICE_AREA,
    )
    zwave_battery_entry = RegistryEntry(
        entity_id=ZWAVE_BATTERY_ENTITY,
        unique_id=ZWAVE_BATTERY_UNIQUE_ID,
        platform="zwave",
        name=ZWAVE_BATTERY_NAME,
        icon=ZWAVE_BATTERY_ICON,
    )
    zwave_power_device = DeviceEntry(
        id=ZWAVE_POWER_DEVICE_ID,
        name_by_user=ZWAVE_POWER_DEVICE_NAME,
        area_id=ZWAVE_POWER_DEVICE_AREA,
    )
    zwave_power_entry = RegistryEntry(
        entity_id=ZWAVE_POWER_ENTITY,
        unique_id=ZWAVE_POWER_UNIQUE_ID,
        platform="zwave",
        name=ZWAVE_POWER_NAME,
        icon=ZWAVE_POWER_ICON,
    )
    zwave_migration_data = {
        ZWAVE_SOURCE_NODE_UNIQUE_ID: {
            "node_id": 10,
            "node_instance": 1,
            "device_id": zwave_source_node_device.id,
            "command_class": 113,
            "command_class_label": "SourceNodeId",
            "value_index": 2,
            "unique_id": ZWAVE_SOURCE_NODE_UNIQUE_ID,
            "entity_entry": zwave_source_node_entry,
        },
        ZWAVE_BATTERY_UNIQUE_ID: {
            "node_id": 36,
            "node_instance": 1,
            "device_id": zwave_battery_device.id,
            "command_class": 128,
            "command_class_label": "Battery Level",
            "value_index": 0,
            "unique_id": ZWAVE_BATTERY_UNIQUE_ID,
            "entity_entry": zwave_battery_entry,
        },
        ZWAVE_POWER_UNIQUE_ID: {
            "node_id": 32,
            "node_instance": 1,
            "device_id": zwave_power_device.id,
            "command_class": 50,
            "command_class_label": "Power",
            "value_index": 8,
            "unique_id": ZWAVE_POWER_UNIQUE_ID,
            "entity_entry": zwave_power_entry,
        },
    }

    mock_device_registry(
        hass,
        {
            zwave_source_node_device.id: zwave_source_node_device,
            zwave_battery_device.id: zwave_battery_device,
            zwave_power_device.id: zwave_power_device,
        },
    )
    mock_registry(
        hass,
        {
            ZWAVE_SOURCE_ENTITY: zwave_source_node_entry,
            ZWAVE_BATTERY_ENTITY: zwave_battery_entry,
            ZWAVE_POWER_ENTITY: zwave_power_entry,
        },
    )

    return zwave_migration_data
コード例 #27
0
async def test_list_entities(hass, client):
    """Test list entries."""
    mock_registry(
        hass,
        {
            "test_domain.name": RegistryEntry(
                entity_id="test_domain.name",
                unique_id="1234",
                platform="test_platform",
                name="Hello World",
            ),
            "test_domain.no_name": RegistryEntry(
                entity_id="test_domain.no_name",
                unique_id="6789",
                platform="test_platform",
            ),
        },
    )

    await client.send_json({"id": 5, "type": "config/entity_registry/list"})
    msg = await client.receive_json()

    assert msg["result"] == [
        {
            "area_id": None,
            "config_entry_id": None,
            "device_id": None,
            "disabled_by": None,
            "entity_category": None,
            "entity_id": "test_domain.name",
            "has_entity_name": False,
            "hidden_by": None,
            "icon": None,
            "name": "Hello World",
            "original_name": None,
            "platform": "test_platform",
        },
        {
            "area_id": None,
            "config_entry_id": None,
            "device_id": None,
            "disabled_by": None,
            "entity_category": None,
            "entity_id": "test_domain.no_name",
            "has_entity_name": False,
            "hidden_by": None,
            "icon": None,
            "name": None,
            "original_name": None,
            "platform": "test_platform",
        },
    ]

    mock_registry(
        hass,
        {
            "test_domain.name": RegistryEntry(
                entity_id="test_domain.name",
                unique_id="1234",
                platform="test_platform",
                name="Hello World",
            ),
        },
    )

    hass.bus.async_fire(
        EVENT_ENTITY_REGISTRY_UPDATED,
        {"action": "create", "entity_id": "test_domain.no_name"},
    )
    await client.send_json({"id": 6, "type": "config/entity_registry/list"})
    msg = await client.receive_json()

    assert msg["result"] == [
        {
            "area_id": None,
            "config_entry_id": None,
            "device_id": None,
            "disabled_by": None,
            "entity_category": None,
            "entity_id": "test_domain.name",
            "has_entity_name": False,
            "hidden_by": None,
            "icon": None,
            "name": "Hello World",
            "original_name": None,
            "platform": "test_platform",
        },
    ]