1 """The Synology DSM component."""
3 from __future__
import annotations
7 from synology_dsm.exceptions
import SynologyDSMException
11 from .const
import CONF_SERIAL, DOMAIN, SERVICE_REBOOT, SERVICE_SHUTDOWN, SERVICES
12 from .models
import SynologyDSMData
14 LOGGER = logging.getLogger(__name__)
18 """Service handler setup."""
20 async
def service_handler(call: ServiceCall) ->
None:
21 """Handle service call."""
22 serial = call.data.get(CONF_SERIAL)
23 dsm_devices = hass.data[DOMAIN]
26 dsm_device: SynologyDSMData = hass.data[DOMAIN][serial]
27 elif len(dsm_devices) == 1:
28 dsm_device = next(iter(dsm_devices.values()))
29 serial = next(iter(dsm_devices))
32 "More than one DSM configured, must specify one of serials %s",
38 LOGGER.error(
"DSM with specified serial %s not found", serial)
41 if call.service
in [SERVICE_REBOOT, SERVICE_SHUTDOWN]:
42 if serial
not in hass.data[DOMAIN]:
43 LOGGER.error(
"DSM with specified serial %s not found", serial)
45 LOGGER.debug(
"%s DSM with serial %s", call.service, serial)
48 "The %s service is deprecated and will be removed in future"
49 " release. Please use the corresponding button entity"
53 dsm_device = hass.data[DOMAIN][serial]
54 dsm_api = dsm_device.api
56 await getattr(dsm_api, f
"async_{call.service}")()
57 except SynologyDSMException
as ex:
59 "%s of DSM with serial %s not possible, because of %s",
66 for service
in SERVICES:
67 hass.services.async_register(DOMAIN, service, service_handler)
None async_setup_services(HomeAssistant hass)