From 323dc5b78a03d9121393544579a82d1c481783e2 Mon Sep 17 00:00:00 2001 From: carstenschroeder Date: Fri, 5 Apr 2019 17:14:44 +0200 Subject: [PATCH] Improve exception handling in ADS integration (#22627) * add exception handling * fix hound findings * improve logging * improve logging II * fix try..except to large --- homeassistant/components/ads/__init__.py | 35 ++++++++++++++++-------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/ads/__init__.py b/homeassistant/components/ads/__init__.py index 5ab53e3acd2..92c6ecb3335 100644 --- a/homeassistant/components/ads/__init__.py +++ b/homeassistant/components/ads/__init__.py @@ -160,28 +160,41 @@ class AdsHub: def write_by_name(self, name, value, plc_datatype): """Write a value to the device.""" + import pyads with self._lock: - return self._client.write_by_name(name, value, plc_datatype) + try: + return self._client.write_by_name(name, value, plc_datatype) + except pyads.ADSError as err: + _LOGGER.error("Error writing %s: %s", name, err) def read_by_name(self, name, plc_datatype): """Read a value from the device.""" + import pyads with self._lock: - return self._client.read_by_name(name, plc_datatype) + try: + return self._client.read_by_name(name, plc_datatype) + except pyads.ADSError as err: + _LOGGER.error("Error reading %s: %s", name, err) def add_device_notification(self, name, plc_datatype, callback): """Add a notification to the ADS devices.""" - from pyads import NotificationAttrib - attr = NotificationAttrib(ctypes.sizeof(plc_datatype)) + import pyads + attr = pyads.NotificationAttrib(ctypes.sizeof(plc_datatype)) with self._lock: - hnotify, huser = self._client.add_device_notification( - name, attr, self._device_notification_callback) - hnotify = int(hnotify) - self._notification_items[hnotify] = NotificationItem( - hnotify, huser, name, plc_datatype, callback) + try: + hnotify, huser = self._client.add_device_notification( + name, attr, self._device_notification_callback) + except pyads.ADSError as err: + _LOGGER.error("Error subscribing to %s: %s", name, err) + else: + hnotify = int(hnotify) + self._notification_items[hnotify] = NotificationItem( + hnotify, huser, name, plc_datatype, callback) - _LOGGER.debug( - "Added device notification %d for variable %s", hnotify, name) + _LOGGER.debug( + "Added device notification %d for variable %s", + hnotify, name) def _device_notification_callback(self, notification, name): """Handle device notifications."""