Improve exception handling in ADS integration (#22627)

* add exception handling

* fix hound findings

* improve logging

* improve logging II

* fix try..except to large
This commit is contained in:
carstenschroeder 2019-04-05 17:14:44 +02:00 committed by Martin Hjelmare
parent 5e7fdb479b
commit 323dc5b78a

View File

@ -160,28 +160,41 @@ class AdsHub:
def write_by_name(self, name, value, plc_datatype): def write_by_name(self, name, value, plc_datatype):
"""Write a value to the device.""" """Write a value to the device."""
import pyads
with self._lock: 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): def read_by_name(self, name, plc_datatype):
"""Read a value from the device.""" """Read a value from the device."""
import pyads
with self._lock: 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): def add_device_notification(self, name, plc_datatype, callback):
"""Add a notification to the ADS devices.""" """Add a notification to the ADS devices."""
from pyads import NotificationAttrib import pyads
attr = NotificationAttrib(ctypes.sizeof(plc_datatype)) attr = pyads.NotificationAttrib(ctypes.sizeof(plc_datatype))
with self._lock: with self._lock:
hnotify, huser = self._client.add_device_notification( try:
name, attr, self._device_notification_callback) hnotify, huser = self._client.add_device_notification(
hnotify = int(hnotify) name, attr, self._device_notification_callback)
self._notification_items[hnotify] = NotificationItem( except pyads.ADSError as err:
hnotify, huser, name, plc_datatype, callback) _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( _LOGGER.debug(
"Added device notification %d for variable %s", hnotify, name) "Added device notification %d for variable %s",
hnotify, name)
def _device_notification_callback(self, notification, name): def _device_notification_callback(self, notification, name):
"""Handle device notifications.""" """Handle device notifications."""