Area registry (#20435)
* First draft of area registry * Refactor based on input * Add tests for areas Add tests for updating device * Updating a device shouldn't require area * Fix Martins comment * Require admin * Save after deleting * Rename read to list_areas Fix device entry_dict Remove area id from device when deleting area * Fix tests
This commit is contained in:
1 parent
2c7060896b
commit
bd335e1ac1
9 files changed
+714
-35
No files matched your search
@@ -0,0 +1,155 @@
|
||||
"""Test area_registry API."""
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.config import area_registry
|
||||
from tests.common import mock_area_registry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(hass, hass_ws_client):
|
||||
"""Fixture that can interact with the config manager API."""
|
||||
hass.loop.run_until_complete(area_registry.async_setup(hass))
|
||||
yield hass.loop.run_until_complete(hass_ws_client(hass))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def registry(hass):
|
||||
"""Return an empty, loaded, registry."""
|
||||
return mock_area_registry(hass)
|
||||
|
||||
|
||||
async def test_list_areas(hass, client, registry):
|
||||
"""Test list entries."""
|
||||
registry.async_create('mock 1')
|
||||
registry.async_create('mock 2')
|
||||
|
||||
await client.send_json({
|
||||
'id': 1,
|
||||
'type': 'config/area_registry/list',
|
||||
})
|
||||
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert len(msg['result']) == len(registry.areas)
|
||||
|
||||
|
||||
async def test_create_area(hass, client, registry):
|
||||
"""Test create entry."""
|
||||
await client.send_json({
|
||||
'id': 1,
|
||||
'name': "mock",
|
||||
'type': 'config/area_registry/create',
|
||||
})
|
||||
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert 'mock' in msg['result']['name']
|
||||
assert len(registry.areas) == 1
|
||||
|
||||
|
||||
async def test_create_area_with_name_already_in_use(hass, client, registry):
|
||||
"""Test create entry that should fail."""
|
||||
registry.async_create('mock')
|
||||
|
||||
await client.send_json({
|
||||
'id': 1,
|
||||
'name': "mock",
|
||||
'type': 'config/area_registry/create',
|
||||
})
|
||||
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert not msg['success']
|
||||
assert msg['error']['code'] == 'invalid_info'
|
||||
assert msg['error']['message'] == "Name is already in use"
|
||||
assert len(registry.areas) == 1
|
||||
|
||||
|
||||
async def test_delete_area(hass, client, registry):
|
||||
"""Test delete entry."""
|
||||
area = registry.async_create('mock')
|
||||
|
||||
await client.send_json({
|
||||
'id': 1,
|
||||
'area_id': area.id,
|
||||
'type': 'config/area_registry/delete',
|
||||
})
|
||||
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert msg['success']
|
||||
assert not registry.areas
|
||||
|
||||
|
||||
async def test_delete_non_existing_area(hass, client, registry):
|
||||
"""Test delete entry that should fail."""
|
||||
registry.async_create('mock')
|
||||
|
||||
await client.send_json({
|
||||
'id': 1,
|
||||
'area_id': '',
|
||||
'type': 'config/area_registry/delete',
|
||||
})
|
||||
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert not msg['success']
|
||||
assert msg['error']['code'] == 'invalid_info'
|
||||
assert msg['error']['message'] == "Area ID doesn't exist"
|
||||
assert len(registry.areas) == 1
|
||||
|
||||
|
||||
async def test_update_area(hass, client, registry):
|
||||
"""Test update entry."""
|
||||
area = registry.async_create('mock 1')
|
||||
|
||||
await client.send_json({
|
||||
'id': 1,
|
||||
'area_id': area.id,
|
||||
'name': "mock 2",
|
||||
'type': 'config/area_registry/update',
|
||||
})
|
||||
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert msg['result']['area_id'] == area.id
|
||||
assert msg['result']['name'] == 'mock 2'
|
||||
assert len(registry.areas) == 1
|
||||
|
||||
|
||||
async def test_update_area_with_same_name(hass, client, registry):
|
||||
"""Test update entry."""
|
||||
area = registry.async_create('mock 1')
|
||||
|
||||
await client.send_json({
|
||||
'id': 1,
|
||||
'area_id': area.id,
|
||||
'name': "mock 1",
|
||||
'type': 'config/area_registry/update',
|
||||
})
|
||||
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert msg['result']['area_id'] == area.id
|
||||
assert msg['result']['name'] == 'mock 1'
|
||||
assert len(registry.areas) == 1
|
||||
|
||||
|
||||
async def test_update_area_with_name_already_in_use(hass, client, registry):
|
||||
"""Test update entry."""
|
||||
area = registry.async_create('mock 1')
|
||||
registry.async_create('mock 2')
|
||||
|
||||
await client.send_json({
|
||||
'id': 1,
|
||||
'area_id': area.id,
|
||||
'name': "mock 2",
|
||||
'type': 'config/area_registry/update',
|
||||
})
|
||||
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert not msg['success']
|
||||
assert msg['error']['code'] == 'invalid_info'
|
||||
assert msg['error']['message'] == "Name is already in use"
|
||||
assert len(registry.areas) == 2
|
||||
@@ -1,4 +1,4 @@
|
||||
"""Test entity_registry API."""
|
||||
"""Test device_registry API."""
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.config import device_registry
|
||||
@@ -48,6 +48,7 @@ async def test_list_devices(hass, client, registry):
|
||||
'name': None,
|
||||
'sw_version': None,
|
||||
'hub_device_id': None,
|
||||
'area_id': None,
|
||||
},
|
||||
{
|
||||
'config_entries': ['1234'],
|
||||
@@ -57,5 +58,30 @@ async def test_list_devices(hass, client, registry):
|
||||
'name': None,
|
||||
'sw_version': None,
|
||||
'hub_device_id': dev1,
|
||||
'area_id': None,
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
async def test_update_device(hass, client, registry):
|
||||
"""Test update entry."""
|
||||
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')
|
||||
|
||||
assert not device.area_id
|
||||
|
||||
await client.send_json({
|
||||
'id': 1,
|
||||
'device_id': device.id,
|
||||
'area_id': '12345A',
|
||||
'type': 'config/device_registry/update',
|
||||
})
|
||||
|
||||
msg = await client.receive_json()
|
||||
|
||||
assert msg['result']['id'] == device.id
|
||||
assert msg['result']['area_id'] == '12345A'
|
||||
assert len(registry.devices) == 1
|
||||
Reference in new issue
Block a user