diff --git a/homeassistant/components/ads.py b/homeassistant/components/ads.py index bd0c0f29be7..d8fe1ab1e21 100644 --- a/homeassistant/components/ads.py +++ b/homeassistant/components/ads.py @@ -50,9 +50,8 @@ CONFIG_SCHEMA = vol.Schema({ def setup(hass, config): + """Set up the ADS component.""" import pyads - """ Set up the ADS component. """ - _LOGGER.info('created ADS client') conf = config[DOMAIN] # get ads connection parameters from config @@ -65,13 +64,28 @@ def setup(hass, config): # create a new ads connection client = pyads.Connection(net_id, port, ip_address) + # add some constants to AdsHub + AdsHub.ADS_TYPEMAP = { + ADSTYPE_BOOL: pyads.PLCTYPE_BOOL, + ADSTYPE_BYTE: pyads.PLCTYPE_BYTE, + ADSTYPE_INT: pyads.PLCTYPE_INT, + ADSTYPE_UINT: pyads.PLCTYPE_UINT, + } + + AdsHub.PLCTYPE_BOOL = pyads.PLCTYPE_BOOL + AdsHub.PLCTYPE_BYTE = pyads.PLCTYPE_BYTE + AdsHub.PLCTYPE_INT = pyads.PLCTYPE_INT + AdsHub.PLCTYPE_UINT = pyads.PLCTYPE_UINT + AdsHub.ADSError = pyads.ADSError + # connect to ads client and try to connect try: ads = AdsHub(client, poll_interval=poll_interval, use_notify=use_notify) - except pyads.pyads.ADSError as e: - _LOGGER.error('Could not connect to ADS host (netid={}, port={})' - .format(net_id, port)) + except pyads.pyads.ADSError: + _LOGGER.error( + 'Could not connect to ADS host (netid=%s, port=%s)', net_id, port + ) return False # add ads hub to hass data collection, listen to shutdown @@ -79,7 +93,7 @@ def setup(hass, config): hass.bus.listen(EVENT_HOMEASSISTANT_STOP, ads.shutdown) def handle_write_data_by_name(call): - """ Write a value to the connected ADS device. """ + """Write a value to the connected ADS device.""" adsvar = call.data.get('adsvar') adstype = call.data.get('adstype') value = call.data.get('value') @@ -88,8 +102,8 @@ def setup(hass, config): try: ads.write_by_name(adsvar, value, ads.ADS_TYPEMAP[adstype]) - except pyads.ADSError as e: - _LOGGER.error(e) + except pyads.ADSError as err: + _LOGGER.error(err) hass.services.register(DOMAIN, 'write_data_by_name', handle_write_data_by_name) @@ -104,24 +118,9 @@ NotificationItem = namedtuple( class AdsHub: - """ Representation of a PyADS connection. """ + """Representation of a PyADS connection.""" def __init__(self, ads_client, poll_interval, use_notify): - from pyads import PLCTYPE_BOOL, PLCTYPE_BYTE, PLCTYPE_INT, \ - PLCTYPE_UINT, ADSError - - self.ADS_TYPEMAP = { - ADSTYPE_BOOL: PLCTYPE_BOOL, - ADSTYPE_BYTE: PLCTYPE_BYTE, - ADSTYPE_INT: PLCTYPE_INT, - ADSTYPE_UINT: PLCTYPE_UINT, - } - - self.PLCTYPE_BOOL = PLCTYPE_BOOL - self.PLCTYPE_BYTE = PLCTYPE_BYTE - self.PLCTYPE_INT = PLCTYPE_INT - self.PLCTYPE_UINT = PLCTYPE_UINT - self.ADSError = ADSError self.poll_interval = poll_interval self.use_notify = use_notify @@ -134,32 +133,36 @@ class AdsHub: self._lock = threading.Lock() def shutdown(self, *args, **kwargs): + """Shutdown ADS connection.""" _LOGGER.debug('Shutting down ADS') - for key, notification_item in self._notification_items.items(): + for _, notification_item in self._notification_items.items(): self._client.del_device_notification( notification_item.hnotify, notification_item.huser ) - _LOGGER.debug('Deleting device notification {0}, {1}' - .format(notification_item.hnotify, - notification_item.huser)) + _LOGGER.debug( + 'Deleting device notification %d, %d', + notification_item.hnotify, notification_item.huser + ) self._client.close() def register_device(self, device): - """ Register a new device. """ + """Register a new device.""" self._devices.append(device) def write_by_name(self, name, value, plc_datatype): + """Write a value to the device.""" with self._lock: return self._client.write_by_name(name, value, plc_datatype) def read_by_name(self, name, plc_datatype): + """Read a value from the device.""" with self._lock: return self._client.read_by_name(name, plc_datatype) def add_device_notification(self, name, plc_datatype, callback): + """Add a notification to the ADS devices.""" from pyads import NotificationAttrib - """ Add a notification to the ADS devices. """ attr = NotificationAttrib(ctypes.sizeof(plc_datatype)) with self._lock: @@ -168,26 +171,27 @@ class AdsHub: ) hnotify = int(hnotify) - _LOGGER.debug('Added Device Notification {0} for variable {1}' - .format(hnotify, name)) + _LOGGER.debug( + 'Added Device Notification %d for variable %s', hnotify, name + ) self._notification_items[hnotify] = NotificationItem( hnotify, huser, name, plc_datatype, callback ) def _device_notification_callback(self, addr, notification, huser): + """Callback for device notifications.""" from pyads import PLCTYPE_BOOL, PLCTYPE_INT, PLCTYPE_BYTE, PLCTYPE_UINT contents = notification.contents hnotify = int(contents.hNotification) - _LOGGER.debug('Received Notification {0}'.format(hnotify)) + _LOGGER.debug('Received Notification %d', hnotify) data = contents.data try: notification_item = self._notification_items[hnotify] except KeyError: - _LOGGER.debug('Unknown Device Notification handle: {0}' - .format(hnotify)) + _LOGGER.debug('Unknown Device Notification handle: %d', hnotify) return # parse data to desired datatype diff --git a/homeassistant/components/binary_sensor/ads.py b/homeassistant/components/binary_sensor/ads.py index 0091f7fb533..015bfc75431 100644 --- a/homeassistant/components/binary_sensor/ads.py +++ b/homeassistant/components/binary_sensor/ads.py @@ -56,7 +56,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class AdsBinarySensor(BinarySensorDevice): - """ Representation of ADS binary sensors. """ + """Representation of ADS binary sensors.""" def __init__(self, ads_hub, name, adsvar, device_class, use_notify, poll_interval): @@ -74,22 +74,23 @@ class AdsBinarySensor(BinarySensorDevice): @property def name(self): - """ Return the default name of the binary sensor. """ + """Return the default name of the binary sensor.""" return self._name @property def device_class(self): - """ Return the device class. """ + """Return the device class.""" return self._device_class @property def is_on(self): - """ Return if the binary sensor is on. """ + """Return if the binary sensor is on.""" return self._state def callback(self, name, value): - _LOGGER.debug('Variable "{0}" changed its value to "{1}"' - .format(name, value)) + """Handle device notifications.""" + _LOGGER.debug('Variable %s changed its value to %d', + name, value) self._state = value try: self.schedule_update_ha_state() @@ -97,14 +98,15 @@ class AdsBinarySensor(BinarySensorDevice): pass def poll(self, now): + """Handle polling.""" try: self._state = self._ads_hub.read_by_name( self.adsvar, self._ads_hub.PLCTYPE_BOOL ) - _LOGGER.debug('Polled value for bool variable {0}: {1}' - .format(self.adsvar, self._state)) - except self._ads_hub.ADSError as e: - _LOGGER.error(e) + _LOGGER.debug('Polled value for bool variable %s: %d', + self.adsvar, self._state) + except self._ads_hub.ADSError as err: + _LOGGER.error(err) try: self.schedule_update_ha_state() diff --git a/homeassistant/components/light/ads.py b/homeassistant/components/light/ads.py index a4ce58d7947..54662f80424 100644 --- a/homeassistant/components/light/ads.py +++ b/homeassistant/components/light/ads.py @@ -25,6 +25,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ def setup_platform(hass, config, add_devices, discovery_info=None): + """Set up the light platform for ADS.""" ads_hub = hass.data.get(DATA_ADS) if not ads_hub: return False @@ -38,6 +39,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class AdsLight(Light): + """Representation of ADS light.""" def __init__(self, ads_hub, varname_enable, varname_brightness, devname): self._ads_hub = ads_hub @@ -75,7 +77,7 @@ class AdsLight(Light): return SUPPORT_ADS def turn_on(self, **kwargs): - """ Turn the light on or set a specific dimmer value. """ + """Turn the light on or set a specific dimmer value.""" brightness = kwargs.get(ATTR_BRIGHTNESS) if brightness is not None: self._brightness = brightness @@ -92,7 +94,7 @@ class AdsLight(Light): self._on_state = True def turn_off(self, **kwargs): - """ Turn the light off. """ + """Turn the light off.""" brightness = kwargs.get(ATTR_BRIGHTNESS) if brightness is not None: self._brightness = brightness @@ -105,11 +107,13 @@ class AdsLight(Light): self._on_state = False def value_changed(self, val): + """Handle value change.""" self._brightness = math.floor(val / 100.0 * 256.0) self._on_state = bool(val != 0) self.schedule_update_ha_state() def update(self): + """Update state of entity.""" self._on_state = self._ads_hub.read_by_name(self.varname_enable, self._ads_hub.PLCTYPE_BOOL) if self.varname_brightness is not None: diff --git a/homeassistant/components/sensor/ads.py b/homeassistant/components/sensor/ads.py index e49ea88b57f..d571adebac1 100644 --- a/homeassistant/components/sensor/ads.py +++ b/homeassistant/components/sensor/ads.py @@ -33,7 +33,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ def setup_platform(hass, config, add_devices, discovery_info=None): - """ Set up an ADS sensor device. """ + """Set up an ADS sensor device.""" ads_hub = hass.data.get(ads.DATA_ADS) if not ads_hub: return False @@ -60,6 +60,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class AdsSensor(Entity): + """Representation of an ADS sensor entity.""" def __init__(self, ads_hub, adsvar, adstype, devname, unit_of_measurement, use_notify, poll_interval, factor): @@ -91,8 +92,8 @@ class AdsSensor(Entity): return self._unit_of_measurement def callback(self, name, value): - _LOGGER.debug('Variable "{0}" changed its value to "{1}"' - .format(name, value)) + """Handle device notifications.""" + _LOGGER.debug('Variable %s changed its value to %d', name, value) # if factor is set use it otherwise not if self.factor is None: @@ -106,6 +107,7 @@ class AdsSensor(Entity): pass def poll(self, now): + """Poll value from ADS device.""" try: val = self._ads_hub.read_by_name( self.adsvar, self._ads_hub.ADS_TYPEMAP[self.adstype] @@ -116,10 +118,11 @@ class AdsSensor(Entity): else: self._value = val / self.factor - _LOGGER.debug('Polled value for bool variable {0}: {1}' - .format(self.adsvar, self._value)) - except self._ads_hub.ADSError as e: - _LOGGER.error(e) + _LOGGER.debug('Polled value for variable %s: %d', + self.adsvar, self._value) + + except self._ads_hub.ADSError as err: + _LOGGER.error(err) try: self.schedule_update_ha_state() diff --git a/homeassistant/components/switch/ads.py b/homeassistant/components/switch/ads.py index 56e62ef2155..edd96273ac0 100644 --- a/homeassistant/components/switch/ads.py +++ b/homeassistant/components/switch/ads.py @@ -23,6 +23,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ def setup_platform(hass, config, add_devices, discovery_info=None): + """Set up switch platform for ADS.""" ads_hub = hass.data.get(DATA_ADS) if not ads_hub: return False @@ -61,10 +62,12 @@ class AdsSwitch(ToggleEntity): self._on_state = False def value_changed(self, val): + """Handle changed state change.""" self._on_state = val self.schedule_update_ha_state() def update(self): + """Update state of entity.""" self._on_state = self._ads_hub.read_by_name( self.ads_var, self._ads_hub.PLCTYPE_BOOL )