add ads service

This commit is contained in:
Stefan Lehmann
2017-10-23 20:51:16 +02:00
parent 41bbcf060c
commit 866cbb2ae2

View File

@@ -2,6 +2,7 @@
ADS Component. ADS Component.
For more details about this component, please refer to the documentation. For more details about this component, please refer to the documentation.
https://home-assistant.io/components/ads/
""" """
import threading import threading
@@ -45,9 +46,6 @@ CONFIG_SCHEMA = vol.Schema({
}) })
}, extra=vol.ALLOW_EXTRA) }, extra=vol.ALLOW_EXTRA)
MAX_RETRIES = 5
RETRY_SLEEPTIME_S = 0.1
def setup(hass, config): def setup(hass, config):
import pyads import pyads
@@ -55,12 +53,15 @@ def setup(hass, config):
_LOGGER.info('created ADS client') _LOGGER.info('created ADS client')
conf = config[DOMAIN] conf = config[DOMAIN]
# get ads connection parameters from config
net_id = conf.get(CONF_DEVICE) net_id = conf.get(CONF_DEVICE)
ip_address = conf.get(CONF_IP_ADDRESS) ip_address = conf.get(CONF_IP_ADDRESS)
port = conf.get(CONF_PORT) port = conf.get(CONF_PORT)
# create a new ads connection
client = pyads.Connection(net_id, port, ip_address) client = pyads.Connection(net_id, port, ip_address)
# connect to ads client and try to connect
try: try:
ads = AdsHub(client) ads = AdsHub(client)
except pyads.pyads.ADSError as e: except pyads.pyads.ADSError as e:
@@ -68,12 +69,30 @@ def setup(hass, config):
.format(net_id, port)) .format(net_id, port))
return False return False
# add ads hub to hass data collection, listen to shutdown
hass.data[DATA_ADS] = ads hass.data[DATA_ADS] = ads
hass.bus.listen(EVENT_HOMEASSISTANT_STOP, ads.shutdown) hass.bus.listen(EVENT_HOMEASSISTANT_STOP, ads.shutdown)
def handle_write_data_by_name(call):
""" Write a value to the connected ADS device. """
adsvar = call.data.get('adsvar')
adstype = call.data.get('adstype')
value = call.data.get('value')
assert adstype in ads.ADS_TYPEMAP
try:
ads.write_by_name(adsvar, value, ads.ADS_TYPEMAP[adstype])
except pyads.ADSError as e:
_LOGGER.error(e)
hass.services.register(DOMAIN, 'write_data_by_name',
handle_write_data_by_name)
return True return True
# tuple to hold data needed for notification
NotificationItem = namedtuple( NotificationItem = namedtuple(
'NotificationItem', 'hnotify huser name plc_datatype callback' 'NotificationItem', 'hnotify huser name plc_datatype callback'
) )