From 6f770b78afa9876eb683d3eeb0bfd1efb53c8eb0 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli Date: Mon, 23 Apr 2018 23:30:21 +0200 Subject: [PATCH] add interface dbus class --- hassio/dbus/__init__.py | 8 ++++++++ hassio/dbus/hostname.py | 22 ++++++++++++++++++++++ hassio/dbus/interface.py | 18 ++++++++++++++++++ hassio/dbus/systemd.py | 12 ++---------- 4 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 hassio/dbus/interface.py diff --git a/hassio/dbus/__init__.py b/hassio/dbus/__init__.py index 4a5ae4a4e..8aa4250b2 100644 --- a/hassio/dbus/__init__.py +++ b/hassio/dbus/__init__.py @@ -1,6 +1,7 @@ """DBus interface objects.""" from .systemd import Systemd +from .hostname import Hostname from ..coresys import CoreSysAttributes @@ -11,12 +12,19 @@ class DBusManager(CoreSysAttributes): """Initialize DBus Interface.""" self.coresys = coresys self._systemd = Systemd() + self._hostname = Hostname() @property def systemd(self): """Return Systemd Interface.""" return self._systemd + @property + def hostname(self): + """Return hostname Interface.""" + return self._hostname + async def load(self): """Connect interfaces to dbus.""" await self.systemd.connect() + await self.hostname.connect() diff --git a/hassio/dbus/hostname.py b/hassio/dbus/hostname.py index 1c25f61fa..548c07abe 100644 --- a/hassio/dbus/hostname.py +++ b/hassio/dbus/hostname.py @@ -1 +1,23 @@ """DBus interface for hostname.""" +import logging + +from .interface import DBusInterface +from .utils import dbus_connected +from ..exceptions import DBusError +from ..utils.gdbus import DBus + +_LOGGER = logging.getLogger(__name__) + +DBUS_NAME = 'org.freedesktop.hostname1' +DBUS_OBJECT = '/org/freedesktop/hostname1' + + +class Hostname(DBusInterface): + """Handle DBus interface for hostname/system.""" + + async def connect(self): + """Connect do bus.""" + try: + self.dbus = await DBus.connect(DBUS_NAME, DBUS_OBJECT) + except DBusError: + _LOGGER.warning("Can't connect to hostname") diff --git a/hassio/dbus/interface.py b/hassio/dbus/interface.py new file mode 100644 index 000000000..fcdf8de25 --- /dev/null +++ b/hassio/dbus/interface.py @@ -0,0 +1,18 @@ +"""Interface class for dbus wrappers.""" + + +class DBusInterface: + """Handle DBus interface for hostname/system.""" + + def __init__(self): + """Initialize systemd.""" + self.dbus = None + + @property + def is_connected(self): + """Return True, if they is connected to dbus.""" + return self.dbus is not None + + async def connect(self): + """Connect do bus.""" + raise NotImplementedError() diff --git a/hassio/dbus/systemd.py b/hassio/dbus/systemd.py index dce4e5eae..d1aff0268 100644 --- a/hassio/dbus/systemd.py +++ b/hassio/dbus/systemd.py @@ -1,6 +1,7 @@ """Interface to Systemd over dbus.""" import logging +from .interface import DBusInterface from .utils import dbus_connected from ..exceptions import DBusError from ..utils.gdbus import DBus @@ -11,18 +12,9 @@ DBUS_NAME = 'org.freedesktop.systemd1' DBUS_OBJECT = '/org/freedesktop/systemd1' -class Systemd: +class Systemd(DBusInterface): """Systemd function handler.""" - def __init__(self): - """Initialize systemd.""" - self.dbus = None - - @property - def is_connected(self): - """Return True, if they is connected to dbus.""" - return self.dbus is not None - async def connect(self): """Connect do bus.""" try: