Migrate entity registry to using websocket (#14830)
* Migrate to using websocket * Lint
This commit is contained in:
parent
4bccb0d2a1
commit
a6880c452f
2 changed files with 117 additions and 61 deletions
|
@ -2,48 +2,85 @@
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
from homeassistant.components.http import HomeAssistantView
|
|
||||||
from homeassistant.components.http.data_validator import RequestDataValidator
|
|
||||||
from homeassistant.helpers.entity_registry import async_get_registry
|
from homeassistant.helpers.entity_registry import async_get_registry
|
||||||
|
from homeassistant.components import websocket_api
|
||||||
|
from homeassistant.helpers import config_validation as cv
|
||||||
|
|
||||||
|
DEPENDENCIES = ['websocket_api']
|
||||||
|
|
||||||
|
WS_TYPE_GET = 'config/entity_registry/get'
|
||||||
|
SCHEMA_WS_GET = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({
|
||||||
|
vol.Required('type'): WS_TYPE_GET,
|
||||||
|
vol.Required('entity_id'): cv.entity_id
|
||||||
|
})
|
||||||
|
|
||||||
|
WS_TYPE_UPDATE = 'config/entity_registry/update'
|
||||||
|
SCHEMA_WS_UPDATE = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.extend({
|
||||||
|
vol.Required('type'): WS_TYPE_UPDATE,
|
||||||
|
vol.Required('entity_id'): cv.entity_id,
|
||||||
|
# If passed in, we update value. Passing None will remove old value.
|
||||||
|
vol.Optional('name'): vol.Any(str, None),
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass):
|
async def async_setup(hass):
|
||||||
"""Enable the Entity Registry views."""
|
"""Enable the Entity Registry views."""
|
||||||
hass.http.register_view(ConfigManagerEntityView)
|
hass.components.websocket_api.async_register_command(
|
||||||
|
WS_TYPE_GET, websocket_get_entity,
|
||||||
|
SCHEMA_WS_GET
|
||||||
|
)
|
||||||
|
hass.components.websocket_api.async_register_command(
|
||||||
|
WS_TYPE_UPDATE, websocket_update_entity,
|
||||||
|
SCHEMA_WS_UPDATE
|
||||||
|
)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
class ConfigManagerEntityView(HomeAssistantView):
|
@callback
|
||||||
"""View to interact with an entity registry entry."""
|
def websocket_get_entity(hass, connection, msg):
|
||||||
|
"""Handle get entity registry entry command.
|
||||||
|
|
||||||
url = '/api/config/entity_registry/{entity_id}'
|
Async friendly.
|
||||||
name = 'api:config:entity_registry:entity'
|
"""
|
||||||
|
async def retrieve_entity():
|
||||||
async def get(self, request, entity_id):
|
"""Get entity from registry."""
|
||||||
"""Get the entity registry settings for an entity."""
|
|
||||||
hass = request.app['hass']
|
|
||||||
registry = await async_get_registry(hass)
|
registry = await async_get_registry(hass)
|
||||||
entry = registry.entities.get(entity_id)
|
entry = registry.entities.get(msg['entity_id'])
|
||||||
|
|
||||||
if entry is None:
|
if entry is None:
|
||||||
return self.json_message('Entry not found', 404)
|
connection.send_message_outside(websocket_api.error_message(
|
||||||
|
msg['id'], websocket_api.ERR_NOT_FOUND, 'Entity not found'))
|
||||||
|
return
|
||||||
|
|
||||||
return self.json(_entry_dict(entry))
|
connection.send_message_outside(websocket_api.result_message(
|
||||||
|
msg['id'], _entry_dict(entry)
|
||||||
|
))
|
||||||
|
|
||||||
@RequestDataValidator(vol.Schema({
|
hass.async_add_job(retrieve_entity())
|
||||||
# If passed in, we update value. Passing None will remove old value.
|
|
||||||
vol.Optional('name'): vol.Any(str, None),
|
|
||||||
}))
|
@callback
|
||||||
async def post(self, request, entity_id, data):
|
def websocket_update_entity(hass, connection, msg):
|
||||||
"""Update the entity registry settings for an entity."""
|
"""Handle get camera thumbnail websocket command.
|
||||||
hass = request.app['hass']
|
|
||||||
|
Async friendly.
|
||||||
|
"""
|
||||||
|
async def update_entity():
|
||||||
|
"""Get entity from registry."""
|
||||||
registry = await async_get_registry(hass)
|
registry = await async_get_registry(hass)
|
||||||
|
|
||||||
if entity_id not in registry.entities:
|
if msg['entity_id'] not in registry.entities:
|
||||||
return self.json_message('Entry not found', 404)
|
connection.send_message_outside(websocket_api.error_message(
|
||||||
|
msg['id'], websocket_api.ERR_NOT_FOUND, 'Entity not found'))
|
||||||
|
return
|
||||||
|
|
||||||
entry = registry.async_update_entity(entity_id, **data)
|
entry = registry.async_update_entity(
|
||||||
return self.json(_entry_dict(entry))
|
msg['entity_id'], name=msg['name'])
|
||||||
|
connection.send_message_outside(websocket_api.result_message(
|
||||||
|
msg['id'], _entry_dict(entry)
|
||||||
|
))
|
||||||
|
|
||||||
|
hass.async_add_job(update_entity())
|
||||||
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
|
|
|
@ -1,18 +1,16 @@
|
||||||
"""Test entity_registry API."""
|
"""Test entity_registry API."""
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.setup import async_setup_component
|
|
||||||
from homeassistant.helpers.entity_registry import RegistryEntry
|
from homeassistant.helpers.entity_registry import RegistryEntry
|
||||||
from homeassistant.components.config import entity_registry
|
from homeassistant.components.config import entity_registry
|
||||||
from tests.common import mock_registry, MockEntity, MockEntityPlatform
|
from tests.common import mock_registry, MockEntity, MockEntityPlatform
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def client(hass, aiohttp_client):
|
def client(hass, hass_ws_client):
|
||||||
"""Fixture that can interact with the config manager API."""
|
"""Fixture that can interact with the config manager API."""
|
||||||
hass.loop.run_until_complete(async_setup_component(hass, 'http', {}))
|
|
||||||
hass.loop.run_until_complete(entity_registry.async_setup(hass))
|
hass.loop.run_until_complete(entity_registry.async_setup(hass))
|
||||||
yield hass.loop.run_until_complete(aiohttp_client(hass.http.app))
|
yield hass.loop.run_until_complete(hass_ws_client(hass))
|
||||||
|
|
||||||
|
|
||||||
async def test_get_entity(hass, client):
|
async def test_get_entity(hass, client):
|
||||||
|
@ -31,20 +29,26 @@ async def test_get_entity(hass, client):
|
||||||
),
|
),
|
||||||
})
|
})
|
||||||
|
|
||||||
resp = await client.get(
|
await client.send_json({
|
||||||
'/api/config/entity_registry/test_domain.name')
|
'id': 5,
|
||||||
assert resp.status == 200
|
'type': 'config/entity_registry/get',
|
||||||
data = await resp.json()
|
'entity_id': 'test_domain.name',
|
||||||
assert data == {
|
})
|
||||||
|
msg = await client.receive_json()
|
||||||
|
|
||||||
|
assert msg['result'] == {
|
||||||
'entity_id': 'test_domain.name',
|
'entity_id': 'test_domain.name',
|
||||||
'name': 'Hello World'
|
'name': 'Hello World'
|
||||||
}
|
}
|
||||||
|
|
||||||
resp = await client.get(
|
await client.send_json({
|
||||||
'/api/config/entity_registry/test_domain.no_name')
|
'id': 6,
|
||||||
assert resp.status == 200
|
'type': 'config/entity_registry/get',
|
||||||
data = await resp.json()
|
'entity_id': 'test_domain.no_name',
|
||||||
assert data == {
|
})
|
||||||
|
msg = await client.receive_json()
|
||||||
|
|
||||||
|
assert msg['result'] == {
|
||||||
'entity_id': 'test_domain.no_name',
|
'entity_id': 'test_domain.no_name',
|
||||||
'name': None
|
'name': None
|
||||||
}
|
}
|
||||||
|
@ -69,13 +73,16 @@ async def test_update_entity(hass, client):
|
||||||
assert state is not None
|
assert state is not None
|
||||||
assert state.name == 'before update'
|
assert state.name == 'before update'
|
||||||
|
|
||||||
resp = await client.post(
|
await client.send_json({
|
||||||
'/api/config/entity_registry/test_domain.world', json={
|
'id': 6,
|
||||||
'name': 'after update'
|
'type': 'config/entity_registry/update',
|
||||||
|
'entity_id': 'test_domain.world',
|
||||||
|
'name': 'after update',
|
||||||
})
|
})
|
||||||
assert resp.status == 200
|
|
||||||
data = await resp.json()
|
msg = await client.receive_json()
|
||||||
assert data == {
|
|
||||||
|
assert msg['result'] == {
|
||||||
'entity_id': 'test_domain.world',
|
'entity_id': 'test_domain.world',
|
||||||
'name': 'after update'
|
'name': 'after update'
|
||||||
}
|
}
|
||||||
|
@ -103,13 +110,16 @@ async def test_update_entity_no_changes(hass, client):
|
||||||
assert state is not None
|
assert state is not None
|
||||||
assert state.name == 'name of entity'
|
assert state.name == 'name of entity'
|
||||||
|
|
||||||
resp = await client.post(
|
await client.send_json({
|
||||||
'/api/config/entity_registry/test_domain.world', json={
|
'id': 6,
|
||||||
'name': 'name of entity'
|
'type': 'config/entity_registry/update',
|
||||||
|
'entity_id': 'test_domain.world',
|
||||||
|
'name': 'name of entity',
|
||||||
})
|
})
|
||||||
assert resp.status == 200
|
|
||||||
data = await resp.json()
|
msg = await client.receive_json()
|
||||||
assert data == {
|
|
||||||
|
assert msg['result'] == {
|
||||||
'entity_id': 'test_domain.world',
|
'entity_id': 'test_domain.world',
|
||||||
'name': 'name of entity'
|
'name': 'name of entity'
|
||||||
}
|
}
|
||||||
|
@ -120,15 +130,24 @@ async def test_update_entity_no_changes(hass, client):
|
||||||
|
|
||||||
async def test_get_nonexisting_entity(client):
|
async def test_get_nonexisting_entity(client):
|
||||||
"""Test get entry."""
|
"""Test get entry."""
|
||||||
resp = await client.get(
|
await client.send_json({
|
||||||
'/api/config/entity_registry/test_domain.non_existing')
|
'id': 6,
|
||||||
assert resp.status == 404
|
'type': 'config/entity_registry/get',
|
||||||
|
'entity_id': 'test_domain.no_name',
|
||||||
|
})
|
||||||
|
msg = await client.receive_json()
|
||||||
|
|
||||||
|
assert not msg['success']
|
||||||
|
|
||||||
|
|
||||||
async def test_update_nonexisting_entity(client):
|
async def test_update_nonexisting_entity(client):
|
||||||
"""Test get entry."""
|
"""Test get entry."""
|
||||||
resp = await client.post(
|
await client.send_json({
|
||||||
'/api/config/entity_registry/test_domain.non_existing', json={
|
'id': 6,
|
||||||
'name': 'some name'
|
'type': 'config/entity_registry/update',
|
||||||
|
'entity_id': 'test_domain.no_name',
|
||||||
|
'name': 'new-name'
|
||||||
})
|
})
|
||||||
assert resp.status == 404
|
msg = await client.receive_json()
|
||||||
|
|
||||||
|
assert not msg['success']
|
||||||
|
|
Loading…
Add table
Reference in a new issue