1 """The syncthru component."""
3 from __future__
import annotations
6 from datetime
import timedelta
9 from pysyncthru
import ConnectionMode, SyncThru, SyncThruAPINotSupported
17 from .const
import DOMAIN
19 _LOGGER = logging.getLogger(__name__)
21 PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
25 """Set up config entry."""
27 session = aiohttp_client.async_get_clientsession(hass)
28 hass.data.setdefault(DOMAIN, {})
30 entry.data[CONF_URL], session, connection_mode=ConnectionMode.API
33 async
def async_update_data() -> SyncThru:
34 """Fetch data from the printer."""
36 async
with asyncio.timeout(10):
37 await printer.update()
38 except SyncThruAPINotSupported
as api_error:
41 "Configured printer at %s does not provide SyncThru JSON API",
48 if printer.is_unknown_state():
49 raise UpdateFailed(f
"Configured printer at {printer.url} does not respond.")
52 coordinator = DataUpdateCoordinator[SyncThru](
57 update_method=async_update_data,
60 await coordinator.async_config_entry_first_refresh()
61 hass.data[DOMAIN][entry.entry_id] = coordinator
62 if isinstance(coordinator.last_exception, SyncThruAPINotSupported):
67 device_registry = dr.async_get(hass)
68 device_registry.async_get_or_create(
69 config_entry_id=entry.entry_id,
70 configuration_url=printer.url,
72 manufacturer=
"Samsung",
74 model=printer.model(),
75 name=printer.hostname(),
78 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
83 """Unload the config entry."""
84 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
85 hass.data[DOMAIN].pop(entry.entry_id,
None)
90 """Get device identifiers for device registry."""
91 serial = printer.serial_number()
94 return {(DOMAIN, serial)}
98 """Get device connections for device registry."""
99 if mac := printer.raw().
get(
"identity", {}).
get(
"mac_addr"):
100 return {(dr.CONNECTION_NETWORK_MAC, mac)}
web.Response get(self, web.Request request, str config_key)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
set[tuple[str, str]]|None device_identifiers(SyncThru printer)
set[tuple[str, str]] device_connections(SyncThru printer)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)