1 """The syncthing integration."""
13 EVENT_HOMEASSISTANT_STOP,
28 PLATFORMS = [Platform.SENSOR]
30 _LOGGER = logging.getLogger(__name__)
34 """Set up syncthing from a config entry."""
37 if DOMAIN
not in hass.data:
38 hass.data[DOMAIN] = {}
40 client = aiosyncthing.Syncthing(
43 verify_ssl=data[CONF_VERIFY_SSL],
47 status = await client.system.status()
48 except aiosyncthing.exceptions.SyncthingError
as exception:
50 raise ConfigEntryNotReady
from exception
52 server_id = status[
"myID"]
56 hass.data[DOMAIN][entry.entry_id] = syncthing
58 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
60 async
def cancel_listen_task(_):
61 await syncthing.unsubscribe()
63 entry.async_on_unload(
64 hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, cancel_listen_task)
71 """Unload a config entry."""
72 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
74 syncthing = hass.data[DOMAIN].pop(entry.entry_id)
75 await syncthing.unsubscribe()
81 """A Syncthing client."""
84 """Initialize the client."""
102 """Get database namespace client."""
103 return self.
_client_client.database
107 """Get system namespace client."""
108 return self.
_client_client.system
111 """Start event listener coroutine."""
115 """Stop event listener coroutine."""
118 await self.
_client_client.close()
121 """Listen to Syncthing events."""
122 events = self.
_client_client.events
123 server_was_unavailable =
False
126 if server_was_unavailable:
128 "The syncthing server '%s' is back online", self.
_client_client.url
131 self.
_hass_hass, f
"{SERVER_AVAILABLE}-{self._server_id}"
133 server_was_unavailable =
False
135 await asyncio.sleep(RECONNECT_INTERVAL.total_seconds())
138 async
for event
in events.listen():
139 if events.last_seen_id == 0:
141 if event[
"type"]
not in EVENTS:
144 signal_name = EVENTS[event[
"type"]]
146 if "folder" in event[
"data"]:
147 folder = event[
"data"][
"folder"]
149 folder = event[
"data"][
"id"]
152 f
"{signal_name}-{self._server_id}-{folder}",
155 except aiosyncthing.exceptions.SyncthingError:
158 "The syncthing server '%s' is not available. Sleeping %i"
159 " seconds and retrying"
162 RECONNECT_INTERVAL.total_seconds(),
165 self.
_hass_hass, f
"{SERVER_UNAVAILABLE}-{self._server_id}"
167 await asyncio.sleep(RECONNECT_INTERVAL.total_seconds())
168 server_was_unavailable =
True
173 await self.
_client_client.system.ping()
174 except aiosyncthing.exceptions.SyncthingError:
def _server_available(self)
def __init__(self, hass, client, server_id)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)