From 261d86f06b2ced3ce6ce3401354f95c0bff0708b Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Sun, 28 Feb 2021 19:19:50 +0100 Subject: [PATCH] Apply recommendations to synology_dsm (#47178) --- .../components/synology_dsm/__init__.py | 64 ++++++++----------- 1 file changed, 25 insertions(+), 39 deletions(-) diff --git a/homeassistant/components/synology_dsm/__init__.py b/homeassistant/components/synology_dsm/__init__.py index 6f0476b403c..50921944a6d 100644 --- a/homeassistant/components/synology_dsm/__init__.py +++ b/homeassistant/components/synology_dsm/__init__.py @@ -191,7 +191,7 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry): try: await api.async_setup() 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 hass.data.setdefault(DOMAIN, {}) @@ -225,9 +225,6 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry): async with async_timeout.timeout(10): await hass.async_add_executor_job(surveillance_station.update) except SynologyDSMAPIErrorException as err: - _LOGGER.debug( - "async_coordinator_update_data_cameras() - exception: %s", err - ) raise UpdateFailed(f"Error communicating with API: {err}") from err return { @@ -241,9 +238,6 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry): try: await api.async_update() except Exception as err: - _LOGGER.debug( - "async_coordinator_update_data_central() - exception: %s", err - ) raise UpdateFailed(f"Error communicating with API: {err}") from err return None @@ -338,15 +332,13 @@ async def _async_setup_services(hass: HomeAssistantType): serial = next(iter(dsm_devices)) else: _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), ) return if not dsm_device: - _LOGGER.error( - "service_handler - DSM with specified serial %s not found", serial - ) + _LOGGER.error("DSM with specified serial %s not found", serial) return _LOGGER.debug("%s DSM with serial %s", call.service, serial) @@ -409,11 +401,11 @@ class SynoApi: self.dsm.apis.get(SynoSurveillanceStation.CAMERA_API_KEY) ) _LOGGER.debug( - "SynoAPI.async_setup() - self._with_surveillance_station:%s", + "State of Surveillance_station during setup:%s", 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.async_update() @@ -422,7 +414,7 @@ class SynoApi: def subscribe(self, api_key, unique_id): """Subscribe an entity to API fetches.""" _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: self._fetching_entities[api_key] = set() @@ -432,7 +424,7 @@ class SynoApi: def unsubscribe() -> None: """Unsubscribe an entity from API fetches (when disable).""" _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) if len(self._fetching_entities[api_key]) == 0: @@ -441,13 +433,11 @@ class SynoApi: return unsubscribe @callback - def _setup_api_requests(self): + def _async_setup_api_requests(self): """Determine if we should fetch each API, if one entity needs it.""" # Entities not added yet, fetch all if not self._fetching_entities: - _LOGGER.debug( - "SynoAPI._setup_api_requests() - Entities not added yet, fetch all" - ) + _LOGGER.debug("Entities not added yet, fetch all") return # 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 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.security = None 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.storage = None 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.system = None 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.upgrade = None 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.utilisation = None if not self._with_surveillance_station: - _LOGGER.debug( - "SynoAPI._setup_api_requests() - disable surveillance_station" - ) + _LOGGER.debug("Disable surveillance_station api from being updated") self.dsm.reset(self.surveillance_station) self.surveillance_station = None @@ -508,29 +496,27 @@ class SynoApi: self.network.update() if self._with_security: - _LOGGER.debug("SynoAPI._fetch_device_configuration() - fetch security") + _LOGGER.debug("Enable security api for updates") self.security = self.dsm.security if self._with_storage: - _LOGGER.debug("SynoAPI._fetch_device_configuration() - fetch storage") + _LOGGER.debug("Enable storage api for updates") self.storage = self.dsm.storage if self._with_upgrade: - _LOGGER.debug("SynoAPI._fetch_device_configuration() - fetch upgrade") + _LOGGER.debug("Enable upgrade api for updates") self.upgrade = self.dsm.upgrade if self._with_system: - _LOGGER.debug("SynoAPI._fetch_device_configuration() - fetch system") + _LOGGER.debug("Enable system api for updates") self.system = self.dsm.system if self._with_utilisation: - _LOGGER.debug("SynoAPI._fetch_device_configuration() - fetch utilisation") + _LOGGER.debug("Enable utilisation api for updates") self.utilisation = self.dsm.utilisation if self._with_surveillance_station: - _LOGGER.debug( - "SynoAPI._fetch_device_configuration() - fetch surveillance_station" - ) + _LOGGER.debug("Enable surveillance_station api for updates") self.surveillance_station = self.dsm.surveillance_station async def async_reboot(self): @@ -558,17 +544,17 @@ class SynoApi: async def async_update(self, now=None): """Update function for updating API information.""" - _LOGGER.debug("SynoAPI.async_update()") - self._setup_api_requests() + _LOGGER.debug("Start data update") + self._async_setup_api_requests() try: await self._hass.async_add_executor_job( self.dsm.update, self._with_information ) except (SynologyDSMLoginFailedException, SynologyDSMRequestException) as err: _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) return