From aa6ca8afd149441402b25973e6a199b62fc98e83 Mon Sep 17 00:00:00 2001 From: Markus Breitenberger Date: Sun, 29 Mar 2020 14:37:39 +0200 Subject: [PATCH] Mark PulseAudio switch not available Until the first response from the PulseAudio server is received, all switches are reported "off", which is not necessarily the correct representation. This change marks the switch as "not available" until the first valid list of loaded modules is received and solves #32016. --- .../components/pulseaudio_loopback/switch.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/homeassistant/components/pulseaudio_loopback/switch.py b/homeassistant/components/pulseaudio_loopback/switch.py index ec1adc7641bb21..38b90c2d348a09 100644 --- a/homeassistant/components/pulseaudio_loopback/switch.py +++ b/homeassistant/components/pulseaudio_loopback/switch.py @@ -84,6 +84,11 @@ def __init__(self, host, port, buff_sz, tcp_timeout): self._buffer_size = int(buff_sz) self._tcp_timeout = int(tcp_timeout) + @property + def available(self): + """Return true if module state was updated.""" + return len(self._current_module_state) + def _send_command(self, cmd, response_expected): """Send a command to the pa server using a socket.""" sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) @@ -147,6 +152,7 @@ def __init__(self, hass, name, pa_server, sink_name, source_name): self._name = name self._sink_name = sink_name self._source_name = source_name + self._available = False self._pa_svr = pa_server @property @@ -154,6 +160,11 @@ def name(self): """Return the name of the switch.""" return self._name + @property + def available(self): + """Return true if Pulse is available and connected.""" + return self._available + @property def is_on(self): """Return true if device is on.""" @@ -186,6 +197,11 @@ def turn_off(self, **kwargs): def update(self): """Refresh state in case an alternate process modified this data.""" self._pa_svr.update_module_state() + + if not self._pa_svr.available: + return + self._module_idx = self._pa_svr.get_module_idx( self._sink_name, self._source_name ) + self._available = True