From 508fc3fa0e083fdf1f6e05df332de404fc8a41a8 Mon Sep 17 00:00:00 2001 From: Matthew Garrett Date: Tue, 28 Jul 2020 00:55:24 -0700 Subject: [PATCH] Fix lg_soundbar callback (#38259) * Don't schedule an update if the hass instance isn't instantiated If we get a status update packet before self.hass exists, we trip a "assert self.hass is not None" that was added in 0.112 and setup fails. * Fix callback hander properly The right fix is to register the callback after hass is ready for it. * Remove unnecessary check This is now guaranteed by the core code. * Don't request an immediate device update and do an async connect. * Remove unnecessary return --- .../components/lg_soundbar/media_player.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/lg_soundbar/media_player.py b/homeassistant/components/lg_soundbar/media_player.py index a10b46f89ce..0b38bc1ab8d 100644 --- a/homeassistant/components/lg_soundbar/media_player.py +++ b/homeassistant/components/lg_soundbar/media_player.py @@ -25,7 +25,7 @@ SUPPORT_LG = ( def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the LG platform.""" if discovery_info is not None: - add_entities([LGDevice(discovery_info)], True) + add_entities([LGDevice(discovery_info)]) class LGDevice(MediaPlayerEntity): @@ -33,8 +33,8 @@ class LGDevice(MediaPlayerEntity): def __init__(self, discovery_info): """Initialize the LG speakers.""" - host = discovery_info.get("host") - port = discovery_info.get("port") + self._host = discovery_info.get("host") + self._port = discovery_info.get("port") self._name = "" self._volume = 0 @@ -53,8 +53,17 @@ class LGDevice(MediaPlayerEntity): self._woofer_volume_max = 0 self._bass = 0 self._treble = 0 + self._device = None - self._device = temescal.temescal(host, port=port, callback=self.handle_event) + async def async_added_to_hass(self): + """Register the callback after hass is ready for it.""" + await self.hass.async_add_executor_job(self._connect) + + def _connect(self): + """Perform the actual devices setup.""" + self._device = temescal.temescal( + self._host, port=self._port, callback=self.handle_event + ) self.update() def handle_event(self, response):