Apply recommendations to synology_dsm (#47178)

This commit is contained in:
Michael 2021-02-28 19:19:50 +01:00 committed by GitHub
parent da5902e4f8
commit 261d86f06b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -191,7 +191,7 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
try: try:
await api.async_setup() await api.async_setup()
except (SynologyDSMLoginFailedException, SynologyDSMRequestException) as err: except (SynologyDSMLoginFailedException, SynologyDSMRequestException) as err:
_LOGGER.debug("async_setup_entry() - Unable to connect to DSM: %s", err) _LOGGER.debug("Unable to connect to DSM during setup: %s", err)
raise ConfigEntryNotReady from err raise ConfigEntryNotReady from err
hass.data.setdefault(DOMAIN, {}) hass.data.setdefault(DOMAIN, {})
@ -225,9 +225,6 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
async with async_timeout.timeout(10): async with async_timeout.timeout(10):
await hass.async_add_executor_job(surveillance_station.update) await hass.async_add_executor_job(surveillance_station.update)
except SynologyDSMAPIErrorException as err: except SynologyDSMAPIErrorException as err:
_LOGGER.debug(
"async_coordinator_update_data_cameras() - exception: %s", err
)
raise UpdateFailed(f"Error communicating with API: {err}") from err raise UpdateFailed(f"Error communicating with API: {err}") from err
return { return {
@ -241,9 +238,6 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
try: try:
await api.async_update() await api.async_update()
except Exception as err: except Exception as err:
_LOGGER.debug(
"async_coordinator_update_data_central() - exception: %s", err
)
raise UpdateFailed(f"Error communicating with API: {err}") from err raise UpdateFailed(f"Error communicating with API: {err}") from err
return None return None
@ -338,15 +332,13 @@ async def _async_setup_services(hass: HomeAssistantType):
serial = next(iter(dsm_devices)) serial = next(iter(dsm_devices))
else: else:
_LOGGER.error( _LOGGER.error(
"service_handler - more than one DSM configured, must specify one of serials %s", "More than one DSM configured, must specify one of serials %s",
sorted(dsm_devices), sorted(dsm_devices),
) )
return return
if not dsm_device: if not dsm_device:
_LOGGER.error( _LOGGER.error("DSM with specified serial %s not found", serial)
"service_handler - DSM with specified serial %s not found", serial
)
return return
_LOGGER.debug("%s DSM with serial %s", call.service, serial) _LOGGER.debug("%s DSM with serial %s", call.service, serial)
@ -409,11 +401,11 @@ class SynoApi:
self.dsm.apis.get(SynoSurveillanceStation.CAMERA_API_KEY) self.dsm.apis.get(SynoSurveillanceStation.CAMERA_API_KEY)
) )
_LOGGER.debug( _LOGGER.debug(
"SynoAPI.async_setup() - self._with_surveillance_station:%s", "State of Surveillance_station during setup:%s",
self._with_surveillance_station, self._with_surveillance_station,
) )
self._setup_api_requests() self._async_setup_api_requests()
await self._hass.async_add_executor_job(self._fetch_device_configuration) await self._hass.async_add_executor_job(self._fetch_device_configuration)
await self.async_update() await self.async_update()
@ -422,7 +414,7 @@ class SynoApi:
def subscribe(self, api_key, unique_id): def subscribe(self, api_key, unique_id):
"""Subscribe an entity to API fetches.""" """Subscribe an entity to API fetches."""
_LOGGER.debug( _LOGGER.debug(
"SynoAPI.subscribe() - api_key:%s, unique_id:%s", api_key, unique_id "Subscribe new entity - api_key:%s, unique_id:%s", api_key, unique_id
) )
if api_key not in self._fetching_entities: if api_key not in self._fetching_entities:
self._fetching_entities[api_key] = set() self._fetching_entities[api_key] = set()
@ -432,7 +424,7 @@ class SynoApi:
def unsubscribe() -> None: def unsubscribe() -> None:
"""Unsubscribe an entity from API fetches (when disable).""" """Unsubscribe an entity from API fetches (when disable)."""
_LOGGER.debug( _LOGGER.debug(
"SynoAPI.unsubscribe() - api_key:%s, unique_id:%s", api_key, unique_id "Unsubscribe new entity - api_key:%s, unique_id:%s", api_key, unique_id
) )
self._fetching_entities[api_key].remove(unique_id) self._fetching_entities[api_key].remove(unique_id)
if len(self._fetching_entities[api_key]) == 0: if len(self._fetching_entities[api_key]) == 0:
@ -441,13 +433,11 @@ class SynoApi:
return unsubscribe return unsubscribe
@callback @callback
def _setup_api_requests(self): def _async_setup_api_requests(self):
"""Determine if we should fetch each API, if one entity needs it.""" """Determine if we should fetch each API, if one entity needs it."""
# Entities not added yet, fetch all # Entities not added yet, fetch all
if not self._fetching_entities: if not self._fetching_entities:
_LOGGER.debug( _LOGGER.debug("Entities not added yet, fetch all")
"SynoAPI._setup_api_requests() - Entities not added yet, fetch all"
)
return return
# Determine if we should fetch an API # Determine if we should fetch an API
@ -470,34 +460,32 @@ class SynoApi:
# Reset not used API, information is not reset since it's used in device_info # Reset not used API, information is not reset since it's used in device_info
if not self._with_security: if not self._with_security:
_LOGGER.debug("SynoAPI._setup_api_requests() - disable security") _LOGGER.debug("Disable security api from being updated")
self.dsm.reset(self.security) self.dsm.reset(self.security)
self.security = None self.security = None
if not self._with_storage: if not self._with_storage:
_LOGGER.debug("SynoAPI._setup_api_requests() - disable storage") _LOGGER.debug("Disable storage api from being updated")
self.dsm.reset(self.storage) self.dsm.reset(self.storage)
self.storage = None self.storage = None
if not self._with_system: if not self._with_system:
_LOGGER.debug("SynoAPI._setup_api_requests() - disable system") _LOGGER.debug("Disable system api from being updated")
self.dsm.reset(self.system) self.dsm.reset(self.system)
self.system = None self.system = None
if not self._with_upgrade: if not self._with_upgrade:
_LOGGER.debug("SynoAPI._setup_api_requests() - disable upgrade") _LOGGER.debug("Disable upgrade api from being updated")
self.dsm.reset(self.upgrade) self.dsm.reset(self.upgrade)
self.upgrade = None self.upgrade = None
if not self._with_utilisation: if not self._with_utilisation:
_LOGGER.debug("SynoAPI._setup_api_requests() - disable utilisation") _LOGGER.debug("Disable utilisation api from being updated")
self.dsm.reset(self.utilisation) self.dsm.reset(self.utilisation)
self.utilisation = None self.utilisation = None
if not self._with_surveillance_station: if not self._with_surveillance_station:
_LOGGER.debug( _LOGGER.debug("Disable surveillance_station api from being updated")
"SynoAPI._setup_api_requests() - disable surveillance_station"
)
self.dsm.reset(self.surveillance_station) self.dsm.reset(self.surveillance_station)
self.surveillance_station = None self.surveillance_station = None
@ -508,29 +496,27 @@ class SynoApi:
self.network.update() self.network.update()
if self._with_security: if self._with_security:
_LOGGER.debug("SynoAPI._fetch_device_configuration() - fetch security") _LOGGER.debug("Enable security api for updates")
self.security = self.dsm.security self.security = self.dsm.security
if self._with_storage: if self._with_storage:
_LOGGER.debug("SynoAPI._fetch_device_configuration() - fetch storage") _LOGGER.debug("Enable storage api for updates")
self.storage = self.dsm.storage self.storage = self.dsm.storage
if self._with_upgrade: if self._with_upgrade:
_LOGGER.debug("SynoAPI._fetch_device_configuration() - fetch upgrade") _LOGGER.debug("Enable upgrade api for updates")
self.upgrade = self.dsm.upgrade self.upgrade = self.dsm.upgrade
if self._with_system: if self._with_system:
_LOGGER.debug("SynoAPI._fetch_device_configuration() - fetch system") _LOGGER.debug("Enable system api for updates")
self.system = self.dsm.system self.system = self.dsm.system
if self._with_utilisation: if self._with_utilisation:
_LOGGER.debug("SynoAPI._fetch_device_configuration() - fetch utilisation") _LOGGER.debug("Enable utilisation api for updates")
self.utilisation = self.dsm.utilisation self.utilisation = self.dsm.utilisation
if self._with_surveillance_station: if self._with_surveillance_station:
_LOGGER.debug( _LOGGER.debug("Enable surveillance_station api for updates")
"SynoAPI._fetch_device_configuration() - fetch surveillance_station"
)
self.surveillance_station = self.dsm.surveillance_station self.surveillance_station = self.dsm.surveillance_station
async def async_reboot(self): async def async_reboot(self):
@ -558,17 +544,17 @@ class SynoApi:
async def async_update(self, now=None): async def async_update(self, now=None):
"""Update function for updating API information.""" """Update function for updating API information."""
_LOGGER.debug("SynoAPI.async_update()") _LOGGER.debug("Start data update")
self._setup_api_requests() self._async_setup_api_requests()
try: try:
await self._hass.async_add_executor_job( await self._hass.async_add_executor_job(
self.dsm.update, self._with_information self.dsm.update, self._with_information
) )
except (SynologyDSMLoginFailedException, SynologyDSMRequestException) as err: except (SynologyDSMLoginFailedException, SynologyDSMRequestException) as err:
_LOGGER.warning( _LOGGER.warning(
"async_update - connection error during update, fallback by reloading the entry" "Connection error during update, fallback by reloading the entry"
) )
_LOGGER.debug("SynoAPI.async_update() - exception: %s", err) _LOGGER.debug("Connection error during update with exception: %s", err)
await self._hass.config_entries.async_reload(self._entry.entry_id) await self._hass.config_entries.async_reload(self._entry.entry_id)
return return