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