diff --git a/homeassistant/components/logbook/__init__.py b/homeassistant/components/logbook/__init__.py index 9c521d3fd909bfea523456161e8e7728ba8f00dd..4bfbc65413c84656e7f213f4c01d811664ec10ec 100644 --- a/homeassistant/components/logbook/__init__.py +++ b/homeassistant/components/logbook/__init__.py @@ -112,6 +112,12 @@ async def async_setup(hass, config): domain = service.data.get(ATTR_DOMAIN) entity_id = service.data.get(ATTR_ENTITY_ID) + if entity_id is None and domain is None: + # If there is no entity_id or + # domain, the event will get filtered + # away so we use the "logbook" domain + domain = DOMAIN + message.hass = hass message = message.async_render() async_log_entry(hass, name, message, domain, entity_id) diff --git a/tests/components/logbook/test_init.py b/tests/components/logbook/test_init.py index ac1e17d8f336def44a615d79b75de1ac23c93d7c..2507fede46dc0ac4c8b450af5689703c94f85b99 100644 --- a/tests/components/logbook/test_init.py +++ b/tests/components/logbook/test_init.py @@ -79,7 +79,15 @@ class TestComponentLogbook(unittest.TestCase): }, True, ) - + self.hass.services.call( + logbook.DOMAIN, + "log", + { + logbook.ATTR_NAME: "This entry", + logbook.ATTR_MESSAGE: "has no domain or entity_id", + }, + True, + ) # Logbook entry service call results in firing an event. # Our service call will unblock when the event listeners have been # scheduled. This means that they may not have been processed yet. @@ -94,15 +102,21 @@ class TestComponentLogbook(unittest.TestCase): dt_util.utcnow() + timedelta(hours=1), ) ) - assert len(events) == 1 + assert len(events) == 2 + + assert len(calls) == 2 + first_call = calls[-2] + + assert first_call.data.get(logbook.ATTR_NAME) == "Alarm" + assert first_call.data.get(logbook.ATTR_MESSAGE) == "is triggered" + assert first_call.data.get(logbook.ATTR_DOMAIN) == "switch" + assert first_call.data.get(logbook.ATTR_ENTITY_ID) == "switch.test_switch" - assert len(calls) == 1 last_call = calls[-1] - assert last_call.data.get(logbook.ATTR_NAME) == "Alarm" - assert last_call.data.get(logbook.ATTR_MESSAGE) == "is triggered" - assert last_call.data.get(logbook.ATTR_DOMAIN) == "switch" - assert last_call.data.get(logbook.ATTR_ENTITY_ID) == "switch.test_switch" + assert last_call.data.get(logbook.ATTR_NAME) == "This entry" + assert last_call.data.get(logbook.ATTR_MESSAGE) == "has no domain or entity_id" + assert last_call.data.get(logbook.ATTR_DOMAIN) == "logbook" def test_service_call_create_log_book_entry_no_message(self): """Test if service call create log book entry without message."""