mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-08 09:46:29 +00:00
Use dbus-fast unpack_variants option (#3885)
* Use dbus-fast unpack_variants option * More readable log on signals
This commit is contained in:
parent
611963f5dd
commit
c24b811180
@ -22,4 +22,4 @@ ruamel.yaml==0.17.21
|
|||||||
securetar==2022.2.0
|
securetar==2022.2.0
|
||||||
sentry-sdk==1.9.8
|
sentry-sdk==1.9.8
|
||||||
voluptuous==0.13.1
|
voluptuous==0.13.1
|
||||||
dbus-fast==1.6.0
|
dbus-fast==1.7.0
|
||||||
|
@ -130,7 +130,7 @@ class NetworkSetting(DBusInterface):
|
|||||||
async def update(self, settings: Any) -> None:
|
async def update(self, settings: Any) -> None:
|
||||||
"""Update connection settings."""
|
"""Update connection settings."""
|
||||||
new_settings = await self.dbus.Settings.Connection.call_get_settings(
|
new_settings = await self.dbus.Settings.Connection.call_get_settings(
|
||||||
remove_signature=False
|
unpack_variants=False
|
||||||
)
|
)
|
||||||
|
|
||||||
_merge_settings_attribute(new_settings, settings, CONF_ATTR_CONNECTION)
|
_merge_settings_attribute(new_settings, settings, CONF_ATTR_CONNECTION)
|
||||||
@ -165,9 +165,7 @@ class NetworkSetting(DBusInterface):
|
|||||||
await super().connect(bus)
|
await super().connect(bus)
|
||||||
await self.reload()
|
await self.reload()
|
||||||
|
|
||||||
# pylint: disable=unnecessary-lambda
|
self.dbus.Settings.Connection.on_updated(self.reload)
|
||||||
# wrapper created by annotation fails the signature test, varargs not supported
|
|
||||||
self.dbus.Settings.Connection.on_updated(lambda: self.reload())
|
|
||||||
|
|
||||||
def disconnect(self) -> None:
|
def disconnect(self) -> None:
|
||||||
"""Disconnect from D-Bus."""
|
"""Disconnect from D-Bus."""
|
||||||
|
@ -36,7 +36,6 @@ from ..exceptions import (
|
|||||||
from ..jobs.const import JobCondition
|
from ..jobs.const import JobCondition
|
||||||
from ..jobs.decorator import Job
|
from ..jobs.decorator import Job
|
||||||
from ..resolution.checks.network_interface_ipv4 import CheckNetworkInterfaceIPV4
|
from ..resolution.checks.network_interface_ipv4 import CheckNetworkInterfaceIPV4
|
||||||
from ..utils.dbus import DBus
|
|
||||||
from .const import AuthMethod, InterfaceMethod, InterfaceType, WifiMode
|
from .const import AuthMethod, InterfaceMethod, InterfaceType, WifiMode
|
||||||
|
|
||||||
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
_LOGGER: logging.Logger = logging.getLogger(__name__)
|
||||||
@ -148,7 +147,6 @@ class NetworkManager(CoreSysAttributes):
|
|||||||
if interface != DBUS_IFACE_NM:
|
if interface != DBUS_IFACE_NM:
|
||||||
return
|
return
|
||||||
|
|
||||||
changed = DBus.remove_dbus_signature(changed)
|
|
||||||
connectivity_check: bool | None = changed.get(DBUS_ATTR_CONNECTION_ENABLED)
|
connectivity_check: bool | None = changed.get(DBUS_ATTR_CONNECTION_ENABLED)
|
||||||
connectivity: bool | None = changed.get(DBUS_ATTR_CONNECTIVITY)
|
connectivity: bool | None = changed.get(DBUS_ATTR_CONNECTIVITY)
|
||||||
|
|
||||||
|
@ -54,17 +54,6 @@ class DBus:
|
|||||||
_LOGGER.debug("Connect to D-Bus: %s - %s", bus_name, object_path)
|
_LOGGER.debug("Connect to D-Bus: %s - %s", bus_name, object_path)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def remove_dbus_signature(data: Any) -> Any:
|
|
||||||
"""Remove signature info."""
|
|
||||||
if isinstance(data, Variant):
|
|
||||||
return DBus.remove_dbus_signature(data.value)
|
|
||||||
if isinstance(data, dict):
|
|
||||||
return {k: DBus.remove_dbus_signature(v) for k, v in data.items()}
|
|
||||||
if isinstance(data, list):
|
|
||||||
return [DBus.remove_dbus_signature(item) for item in data]
|
|
||||||
return data
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_dbus_error(err: DBusError) -> HassioNotSupportedError | DBusError:
|
def from_dbus_error(err: DBusError) -> HassioNotSupportedError | DBusError:
|
||||||
"""Return correct dbus error based on type."""
|
"""Return correct dbus error based on type."""
|
||||||
@ -91,7 +80,7 @@ class DBus:
|
|||||||
proxy_interface: ProxyInterface,
|
proxy_interface: ProxyInterface,
|
||||||
method: str,
|
method: str,
|
||||||
*args,
|
*args,
|
||||||
remove_signature: bool = True,
|
unpack_variants: bool = True,
|
||||||
) -> Any:
|
) -> Any:
|
||||||
"""Call a dbus method and handle the signature and errors."""
|
"""Call a dbus method and handle the signature and errors."""
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
@ -101,8 +90,9 @@ class DBus:
|
|||||||
proxy_interface.path,
|
proxy_interface.path,
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
body = await getattr(proxy_interface, method)(*args)
|
return await getattr(proxy_interface, method)(
|
||||||
return DBus.remove_dbus_signature(body) if remove_signature else body
|
*args, unpack_variants=unpack_variants
|
||||||
|
)
|
||||||
except DBusError as err:
|
except DBusError as err:
|
||||||
raise DBus.from_dbus_error(err)
|
raise DBus.from_dbus_error(err)
|
||||||
|
|
||||||
@ -178,10 +168,18 @@ class DBus:
|
|||||||
if interface != prop_interface:
|
if interface != prop_interface:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
_LOGGER.debug(
|
||||||
|
"Property change for %s-%s: %s changed & %s invalidated",
|
||||||
|
self.bus_name,
|
||||||
|
self.object_path,
|
||||||
|
changed.keys(),
|
||||||
|
invalidated,
|
||||||
|
)
|
||||||
|
|
||||||
if invalidated:
|
if invalidated:
|
||||||
await update()
|
await update()
|
||||||
else:
|
else:
|
||||||
await update(DBus.remove_dbus_signature(changed))
|
await update(changed)
|
||||||
|
|
||||||
self.properties.on_properties_changed(sync_property_change)
|
self.properties.on_properties_changed(sync_property_change)
|
||||||
return sync_property_change
|
return sync_property_change
|
||||||
@ -195,7 +193,9 @@ class DBus:
|
|||||||
for intr, signals in self._signal_monitors.items():
|
for intr, signals in self._signal_monitors.items():
|
||||||
for name, callbacks in signals.items():
|
for name, callbacks in signals.items():
|
||||||
for callback in callbacks:
|
for callback in callbacks:
|
||||||
getattr(self._proxies[intr], f"off_{name}")(callback)
|
getattr(self._proxies[intr], f"off_{name}")(
|
||||||
|
callback, unpack_variants=True
|
||||||
|
)
|
||||||
|
|
||||||
self._signal_monitors = {}
|
self._signal_monitors = {}
|
||||||
|
|
||||||
@ -254,7 +254,7 @@ class DBusCallWrapper:
|
|||||||
if dbus_type == "on":
|
if dbus_type == "on":
|
||||||
|
|
||||||
def _on_signal(callback: Callable):
|
def _on_signal(callback: Callable):
|
||||||
getattr(self._proxy, name)(callback)
|
getattr(self._proxy, name)(callback, unpack_variants=True)
|
||||||
|
|
||||||
# pylint: disable=protected-access
|
# pylint: disable=protected-access
|
||||||
if self.interface not in self.dbus._signal_monitors:
|
if self.interface not in self.dbus._signal_monitors:
|
||||||
@ -272,7 +272,7 @@ class DBusCallWrapper:
|
|||||||
return _on_signal
|
return _on_signal
|
||||||
|
|
||||||
def _off_signal(callback: Callable):
|
def _off_signal(callback: Callable):
|
||||||
getattr(self._proxy, name)(callback)
|
getattr(self._proxy, name)(callback, unpack_variants=True)
|
||||||
|
|
||||||
# pylint: disable=protected-access
|
# pylint: disable=protected-access
|
||||||
if (
|
if (
|
||||||
@ -298,9 +298,9 @@ class DBusCallWrapper:
|
|||||||
|
|
||||||
if dbus_type in ["call", "get", "set"]:
|
if dbus_type in ["call", "get", "set"]:
|
||||||
|
|
||||||
def _method_wrapper(*args, remove_signature: bool = True) -> Awaitable:
|
def _method_wrapper(*args, unpack_variants: bool = True) -> Awaitable:
|
||||||
return DBus.call_dbus(
|
return DBus.call_dbus(
|
||||||
self._proxy, name, *args, remove_signature=remove_signature
|
self._proxy, name, *args, unpack_variants=unpack_variants
|
||||||
)
|
)
|
||||||
|
|
||||||
return _method_wrapper
|
return _method_wrapper
|
||||||
|
@ -171,7 +171,7 @@ def dbus(dbus_bus: MessageBus) -> DBus:
|
|||||||
proxy_interface: ProxyInterface,
|
proxy_interface: ProxyInterface,
|
||||||
method: str,
|
method: str,
|
||||||
*args,
|
*args,
|
||||||
remove_signature: bool = True,
|
unpack_variants: bool = True,
|
||||||
):
|
):
|
||||||
if (
|
if (
|
||||||
proxy_interface.introspection.name == DBUS_INTERFACE_PROPERTIES
|
proxy_interface.introspection.name == DBUS_INTERFACE_PROPERTIES
|
||||||
|
@ -72,10 +72,10 @@ SETTINGS_WITH_SIGNATURE = {
|
|||||||
|
|
||||||
|
|
||||||
async def mock_call_dbus_get_settings_signature(
|
async def mock_call_dbus_get_settings_signature(
|
||||||
_: ProxyInterface, method: str, *args, remove_signature: bool = True
|
_: ProxyInterface, method: str, *args, unpack_variants: bool = True
|
||||||
) -> list[dict[str, Any]]:
|
) -> list[dict[str, Any]]:
|
||||||
"""Call dbus method mock for get settings that keeps signature."""
|
"""Call dbus method mock for get settings that keeps signature."""
|
||||||
if method == "call_get_settings" and not remove_signature:
|
if method == "call_get_settings" and not unpack_variants:
|
||||||
return SETTINGS_WITH_SIGNATURE
|
return SETTINGS_WITH_SIGNATURE
|
||||||
else:
|
else:
|
||||||
assert method == "call_update"
|
assert method == "call_update"
|
||||||
|
@ -80,7 +80,7 @@ async def test_signal_listener_disconnect(
|
|||||||
device = dev
|
device = dev
|
||||||
|
|
||||||
proxy.obj.dbus.on_device_added(callback)
|
proxy.obj.dbus.on_device_added(callback)
|
||||||
proxy.on_device_added.assert_called_once_with(callback)
|
proxy.on_device_added.assert_called_once_with(callback, unpack_variants=True)
|
||||||
|
|
||||||
fire_watched_signal(
|
fire_watched_signal(
|
||||||
proxy.obj, "org.freedesktop.NetworkManager.DeviceAdded", ["/test/obj/1"]
|
proxy.obj, "org.freedesktop.NetworkManager.DeviceAdded", ["/test/obj/1"]
|
||||||
@ -89,4 +89,4 @@ async def test_signal_listener_disconnect(
|
|||||||
assert device == "/test/obj/1"
|
assert device == "/test/obj/1"
|
||||||
|
|
||||||
proxy.obj.disconnect()
|
proxy.obj.disconnect()
|
||||||
proxy.off_device_added.assert_called_once_with(callback)
|
proxy.off_device_added.assert_called_once_with(callback, unpack_variants=True)
|
||||||
|
@ -140,7 +140,7 @@ async def test_scan_wifi_with_failures(coresys: CoreSys, caplog):
|
|||||||
proxy_interface: ProxyInterface,
|
proxy_interface: ProxyInterface,
|
||||||
method: str,
|
method: str,
|
||||||
*args,
|
*args,
|
||||||
remove_signature: bool = True,
|
unpack_variants: bool = True,
|
||||||
):
|
):
|
||||||
if method == "call_get_all_access_points":
|
if method == "call_get_all_access_points":
|
||||||
return [
|
return [
|
||||||
@ -150,7 +150,7 @@ async def test_scan_wifi_with_failures(coresys: CoreSys, caplog):
|
|||||||
]
|
]
|
||||||
|
|
||||||
return await call_dbus(
|
return await call_dbus(
|
||||||
proxy_interface, method, *args, remove_signature=remove_signature
|
proxy_interface, method, *args, unpack_variants=unpack_variants
|
||||||
)
|
)
|
||||||
|
|
||||||
with patch("supervisor.host.network.asyncio.sleep"), patch(
|
with patch("supervisor.host.network.asyncio.sleep"), patch(
|
||||||
|
@ -1,19 +0,0 @@
|
|||||||
"""Check dbus-next implementation."""
|
|
||||||
from dbus_fast.signature import Variant
|
|
||||||
|
|
||||||
from supervisor.utils.dbus import DBus
|
|
||||||
|
|
||||||
|
|
||||||
def test_remove_dbus_signature():
|
|
||||||
"""Check D-Bus signature clean-up."""
|
|
||||||
test = DBus.remove_dbus_signature(Variant("s", "Value"))
|
|
||||||
assert isinstance(test, str)
|
|
||||||
assert test == "Value"
|
|
||||||
|
|
||||||
test_dict = DBus.remove_dbus_signature({"Key": Variant("s", "Value")})
|
|
||||||
assert isinstance(test_dict["Key"], str)
|
|
||||||
assert test_dict["Key"] == "Value"
|
|
||||||
|
|
||||||
test_dict = DBus.remove_dbus_signature([Variant("s", "Value")])
|
|
||||||
assert isinstance(test_dict[0], str)
|
|
||||||
assert test_dict[0] == "Value"
|
|
Loading…
x
Reference in New Issue
Block a user