Ensure logbook entries appear when the logbook.log (#37388)

service without a domain or entity_id
This commit is contained in:
J. Nick Koston 2020-07-02 19:53:28 -05:00 committed by GitHub
parent 91799e2b52
commit 7e664fbb3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 7 deletions

View File

@ -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)

View File

@ -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."""