2019-03-17 03:44:05 +00:00
|
|
|
"""The syncthru component."""
|
2020-07-08 23:38:16 +00:00
|
|
|
|
2020-09-02 09:50:57 +00:00
|
|
|
import logging
|
|
|
|
from typing import Set, Tuple
|
|
|
|
|
|
|
|
from pysyncthru import SyncThru
|
|
|
|
|
2020-07-08 23:38:16 +00:00
|
|
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-09-02 09:50:57 +00:00
|
|
|
from homeassistant.const import CONF_URL
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
|
|
from homeassistant.helpers import aiohttp_client, device_registry as dr
|
2020-07-08 23:38:16 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
|
|
|
|
2020-09-02 09:50:57 +00:00
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2020-07-08 23:38:16 +00:00
|
|
|
|
|
|
|
async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
|
|
|
|
"""Set up."""
|
2020-09-02 09:50:57 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2020-07-08 23:38:16 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up config entry."""
|
2020-09-02 09:50:57 +00:00
|
|
|
|
|
|
|
session = aiohttp_client.async_get_clientsession(hass)
|
|
|
|
printer = hass.data[DOMAIN][entry.entry_id] = SyncThru(
|
|
|
|
entry.data[CONF_URL], session
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
await printer.update()
|
|
|
|
except ValueError:
|
|
|
|
_LOGGER.error(
|
|
|
|
"Device at %s not appear to be a SyncThru printer, aborting setup",
|
|
|
|
printer.url,
|
|
|
|
)
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if printer.is_unknown_state():
|
|
|
|
raise ConfigEntryNotReady
|
|
|
|
|
|
|
|
device_registry = await dr.async_get_registry(hass)
|
|
|
|
device_registry.async_get_or_create(
|
|
|
|
config_entry_id=entry.entry_id,
|
|
|
|
connections=device_connections(printer),
|
|
|
|
identifiers=device_identifiers(printer),
|
|
|
|
model=printer.model(),
|
|
|
|
name=printer.hostname(),
|
|
|
|
)
|
|
|
|
|
2020-07-08 23:38:16 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(entry, SENSOR_DOMAIN)
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-09-02 09:50:57 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
|
2020-07-08 23:38:16 +00:00
|
|
|
"""Unload the config entry."""
|
2020-09-02 09:50:57 +00:00
|
|
|
await hass.config_entries.async_forward_entry_unload(entry, SENSOR_DOMAIN)
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id, None)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def device_identifiers(printer: SyncThru) -> Set[Tuple[str, str]]:
|
|
|
|
"""Get device identifiers for device registry."""
|
|
|
|
return {(DOMAIN, printer.serial_number())}
|
|
|
|
|
|
|
|
|
|
|
|
def device_connections(printer: SyncThru) -> Set[Tuple[str, str]]:
|
|
|
|
"""Get device connections for device registry."""
|
|
|
|
connections = set()
|
|
|
|
try:
|
|
|
|
mac = printer.raw()["identity"]["mac_addr"]
|
|
|
|
if mac:
|
|
|
|
connections.add((dr.CONNECTION_NETWORK_MAC, mac))
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
return connections
|