From ca60a69b22967f53e8ea36f8e638db32a7dd0eeb Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Mon, 14 Sep 2020 14:22:38 +0200 Subject: [PATCH] Add timeout handling to gdbus (#2053) * Add timeout handling to gdbus * fix bus name * make it silent * fix tests * add more wraper --- supervisor/utils/gdbus.py | 28 ++++++++++++++-------------- tests/conftest.py | 5 ++++- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/supervisor/utils/gdbus.py b/supervisor/utils/gdbus.py index 89d0b48ad..f00057192 100644 --- a/supervisor/utils/gdbus.py +++ b/supervisor/utils/gdbus.py @@ -52,18 +52,15 @@ RE_MONITOR_OUTPUT: re.Pattern[Any] = re.compile(r".+?: (?P[^ ].+) (?P None: """Read interface data.""" - command = shlex.split( + # Wait for dbus object to be available after restart + command_wait = shlex.split(WAIT.format(bus=self.bus_name)) + await self._send(command_wait, silent=True) + + # Introspect object & Parse XML + command_introspect = shlex.split( INTROSPECT.format(bus=self.bus_name, object=self.object_path) ) - - # Parse XML - data = await self._send(command) + data = await self._send(command_introspect) try: xml = ET.fromstring(data) except ET.ParseError as err: @@ -219,7 +219,7 @@ class DBus: _LOGGER.error("No attributes returned for %s", interface) raise DBusFatalError() from err - async def _send(self, command: List[str]) -> str: + async def _send(self, command: List[str], silent=False) -> str: """Send command over dbus.""" # Run command _LOGGER.debug("Send dbus command: %s", command) @@ -237,7 +237,7 @@ class DBus: raise DBusFatalError() from err # Success? - if proc.returncode == 0: + if proc.returncode == 0 or silent: return data.decode() # Filter error @@ -248,7 +248,7 @@ class DBus: raise exception() # General - _LOGGER.error("DBus return error: %s", error.strip()) + _LOGGER.error("DBus return: %s", error.strip()) raise DBusFatalError() def attach_signals(self, filters=None): diff --git a/tests/conftest.py b/tests/conftest.py index 4f4615e8d..4e941860e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -44,7 +44,10 @@ def dbus() -> DBus: async def mock_get_properties(_, interface): return load_json_fixture(f"{interface.replace('.', '_')}.json") - async def mock_send(_, command): + async def mock_send(_, command, silent=False): + if silent: + return "" + filetype = "xml" if "--xml" in command else "fixture" fixture = f"{command[6].replace('/', '_')[1:]}.{filetype}" return load_fixture(fixture)