1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Log warning when entities referenced in service call not found (#31427)

* Raise entities not found error

* Make it a warning, not an error

* Add support for MATCH_ENTITY_NONE

* Fix lint

* Fix tests
This commit is contained in:
Paulus Schoutsen
2020-02-04 14:42:07 -08:00
committed by GitHub
parent 201ea2557e
commit f41623ca64
16 changed files with 339 additions and 89 deletions

View File

@@ -7,8 +7,9 @@ from unittest.mock import Mock, patch
import asynctest
import pytest
import voluptuous as vol
from homeassistant.const import ENTITY_MATCH_ALL
from homeassistant.const import ENTITY_MATCH_ALL, ENTITY_MATCH_NONE
import homeassistant.core as ha
from homeassistant.exceptions import PlatformNotReady
from homeassistant.helpers import discovery
@@ -223,10 +224,21 @@ async def test_extract_from_service_fails_if_no_entity_id(hass):
[MockEntity(name="test_1"), MockEntity(name="test_2")]
)
call = ha.ServiceCall("test", "service")
assert [] == sorted(
ent.entity_id for ent in (await component.async_extract_from_service(call))
assert (
await component.async_extract_from_service(ha.ServiceCall("test", "service"))
== []
)
assert (
await component.async_extract_from_service(
ha.ServiceCall("test", "service", {"entity_id": ENTITY_MATCH_NONE})
)
== []
)
assert (
await component.async_extract_from_service(
ha.ServiceCall("test", "service", {"area_id": ENTITY_MATCH_NONE})
)
== []
)
@@ -429,3 +441,53 @@ async def test_extract_all_use_match_all(hass, caplog):
assert (
"Not passing an entity ID to a service to target all entities is deprecated"
) not in caplog.text
async def test_register_entity_service(hass):
"""Test not expanding a group."""
entity = MockEntity(entity_id=f"{DOMAIN}.entity")
calls = []
@ha.callback
def appender(**kwargs):
calls.append(kwargs)
entity.async_called_by_service = appender
component = EntityComponent(_LOGGER, DOMAIN, hass)
await component.async_add_entities([entity])
component.async_register_entity_service(
"hello", {"some": str}, "async_called_by_service"
)
with pytest.raises(vol.Invalid):
await hass.services.async_call(
DOMAIN,
"hello",
{"entity_id": entity.entity_id, "invalid": "data"},
blocking=True,
)
assert len(calls) == 0
await hass.services.async_call(
DOMAIN, "hello", {"entity_id": entity.entity_id, "some": "data"}, blocking=True
)
assert len(calls) == 1
assert calls[0] == {"some": "data"}
await hass.services.async_call(
DOMAIN, "hello", {"entity_id": ENTITY_MATCH_ALL, "some": "data"}, blocking=True
)
assert len(calls) == 2
assert calls[1] == {"some": "data"}
await hass.services.async_call(
DOMAIN, "hello", {"entity_id": ENTITY_MATCH_NONE, "some": "data"}, blocking=True
)
assert len(calls) == 2
await hass.services.async_call(
DOMAIN, "hello", {"area_id": ENTITY_MATCH_NONE, "some": "data"}, blocking=True
)
assert len(calls) == 2