Fix dispatcher logging (#33299)

This commit is contained in:
Paulus Schoutsen 2020-03-26 20:44:44 -07:00 committed by GitHub
parent 6cafc45d2b
commit 73c52e668e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

View File

@ -47,7 +47,10 @@ def async_dispatcher_connect(
wrapped_target = catch_log_exception(
target,
lambda *args: "Exception in {} when dispatching '{}': {}".format(
target.__name__, signal, args
# Functions wrapped in partial do not have a __name__
getattr(target, "__name__", None) or str(target),
signal,
args,
),
)

View File

@ -1,5 +1,6 @@
"""Test dispatcher helpers."""
import asyncio
from functools import partial
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import (
@ -147,9 +148,13 @@ async def test_callback_exception_gets_logged(hass, caplog):
"""Record calls."""
raise Exception("This is a bad message callback")
async_dispatcher_connect(hass, "test", bad_handler)
# wrap in partial to test message logging.
async_dispatcher_connect(hass, "test", partial(bad_handler))
dispatcher_send(hass, "test", "bad")
await hass.async_block_till_done()
await hass.async_block_till_done()
assert "Exception in bad_handler when dispatching 'test': ('bad',)" in caplog.text
assert (
f"Exception in functools.partial({bad_handler}) when dispatching 'test': ('bad',)"
in caplog.text
)