From 73c52e668e7ef239a20f0287c66f368a41f65a94 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 26 Mar 2020 20:44:44 -0700 Subject: [PATCH] Fix dispatcher logging (#33299) --- homeassistant/helpers/dispatcher.py | 5 ++++- tests/helpers/test_dispatcher.py | 9 +++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/homeassistant/helpers/dispatcher.py b/homeassistant/helpers/dispatcher.py index a4e624f119f..bb6fa3a735d 100644 --- a/homeassistant/helpers/dispatcher.py +++ b/homeassistant/helpers/dispatcher.py @@ -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, ), ) diff --git a/tests/helpers/test_dispatcher.py b/tests/helpers/test_dispatcher.py index 4cf266e88a2..539d9ad1651 100644 --- a/tests/helpers/test_dispatcher.py +++ b/tests/helpers/test_dispatcher.py @@ -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 + )