mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
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:
committed by
Paulus Schoutsen
parent
2c7060896b
commit
bd335e1ac1
155
tests/components/config/test_area_registry.py
Normal file
155
tests/components/config/test_area_registry.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user