1 """Support for monitoring the Syncthing instance."""
16 FOLDER_PAUSED_RECEIVED,
17 FOLDER_SUMMARY_RECEIVED,
21 STATE_CHANGED_RECEIVED,
27 config_entry: ConfigEntry,
28 async_add_entities: AddEntitiesCallback,
30 """Set up the Syncthing sensors."""
31 syncthing = hass.data[DOMAIN][config_entry.entry_id]
34 config = await syncthing.system.config()
35 version = await syncthing.system.version()
36 except aiosyncthing.exceptions.SyncthingError
as exception:
37 raise PlatformNotReady
from exception
39 server_id = syncthing.server_id
48 for folder
in config[
"folders"]
55 """A Syncthing folder sensor."""
57 _attr_should_poll =
False
58 _attr_translation_key =
"syncthing"
62 "globalBytes":
"global_bytes",
63 "globalDeleted":
"global_deleted",
64 "globalDirectories":
"global_directories",
65 "globalFiles":
"global_files",
66 "globalSymlinks":
"global_symlinks",
67 "globalTotalItems":
"global_total_items",
68 "ignorePatterns":
"ignore_patterns",
69 "inSyncBytes":
"in_sync_bytes",
70 "inSyncFiles":
"in_sync_files",
72 "localBytes":
"local_bytes",
73 "localDeleted":
"local_deleted",
74 "localDirectories":
"local_directories",
75 "localFiles":
"local_files",
76 "localSymlinks":
"local_symlinks",
77 "localTotalItems":
"local_total_items",
78 "needBytes":
"need_bytes",
79 "needDeletes":
"need_deletes",
80 "needDirectories":
"need_directories",
81 "needFiles":
"need_files",
82 "needSymlinks":
"need_symlinks",
83 "needTotalItems":
"need_total_items",
84 "pullErrors":
"pull_errors",
88 def __init__(self, syncthing, server_id, folder_id, folder_label, version):
89 """Initialize the sensor."""
98 self.
_attr_name_attr_name = f
"{self._short_server_id} {folder_id} {folder_label}"
101 entry_type=DeviceEntryType.SERVICE,
102 identifiers={(DOMAIN, self.
_server_id_server_id)},
103 manufacturer=
"Syncthing Team",
104 name=f
"Syncthing ({syncthing.url})",
110 """Return the state of the sensor."""
111 return self.
_state_state[
"state"]
115 """Could the device be accessed during the last update call."""
116 return self.
_state_state
is not None
120 """Return the state attributes."""
124 """Request folder status and update state."""
127 except aiosyncthing.exceptions.SyncthingError:
134 """Start polling syncthing folder status."""
137 async
def refresh(event_time):
138 """Get the latest data from Syncthing."""
142 self.
hasshass, refresh, SCAN_INTERVAL
147 """Stop polling syncthing folder status."""
153 """Handle entity which will be added."""
156 def handle_folder_summary(event):
157 if self.
_state_state
is not None:
164 f
"{FOLDER_SUMMARY_RECEIVED}-{self._server_id}-{self._folder_id}",
165 handle_folder_summary,
170 def handle_state_changed(event):
171 if self.
_state_state
is not None:
172 self.
_state_state[
"state"] = event[
"data"][
"to"]
178 f
"{STATE_CHANGED_RECEIVED}-{self._server_id}-{self._folder_id}",
179 handle_state_changed,
184 def handle_folder_paused(event):
185 if self.
_state_state
is not None:
186 self.
_state_state[
"state"] =
"paused"
192 f
"{FOLDER_PAUSED_RECEIVED}-{self._server_id}-{self._folder_id}",
193 handle_folder_paused,
198 def handle_server_unavailable():
206 f
"{SERVER_UNAVAILABLE}-{self._server_id}",
207 handle_server_unavailable,
211 async
def handle_server_available():
218 f
"{SERVER_AVAILABLE}-{self._server_id}",
219 handle_server_available,
232 for key, value
in state.items()
237 if state[
"state"] ==
"":
238 state[
"state"] =
"paused"
dictionary STATE_ATTRIBUTES
def async_update_status(self)
def __init__(self, syncthing, server_id, folder_id, folder_label, version)
def extra_state_attributes(self)
None async_added_to_hass(self)
def _filter_state(self, state)
None async_write_ha_state(self)
None async_on_remove(self, CALLBACK_TYPE func)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
CALLBACK_TYPE async_track_time_interval(HomeAssistant hass, Callable[[datetime], Coroutine[Any, Any, None]|None] action, timedelta interval, *str|None name=None, bool|None cancel_on_shutdown=None)