1
mirror of https://github.com/home-assistant/core synced 2024-09-09 12:51:22 +02:00
ha-core/homeassistant/components/ads/__init__.py

319 lines
9.6 KiB
Python
Raw Normal View History

"""Support for Automation Device Specification (ADS)."""
import asyncio
from collections import namedtuple
import ctypes
import logging
import struct
import threading
import async_timeout
2019-10-12 22:03:42 +02:00
import pyads
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
import voluptuous as vol
from homeassistant.const import (
2019-07-31 21:25:30 +02:00
CONF_DEVICE,
CONF_IP_ADDRESS,
CONF_PORT,
EVENT_HOMEASSISTANT_STOP,
)
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 21:25:30 +02:00
DATA_ADS = "data_ads"
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
# Supported Types
2019-07-31 21:25:30 +02:00
ADSTYPE_BOOL = "bool"
ADSTYPE_BYTE = "byte"
ADSTYPE_DINT = "dint"
ADSTYPE_INT = "int"
ADSTYPE_UDINT = "udint"
ADSTYPE_UINT = "uint"
CONF_ADS_FACTOR = "factor"
CONF_ADS_TYPE = "adstype"
CONF_ADS_VALUE = "value"
CONF_ADS_VAR = "adsvar"
CONF_ADS_VAR_BRIGHTNESS = "adsvar_brightness"
CONF_ADS_VAR_POSITION = "adsvar_position"
STATE_KEY_STATE = "state"
STATE_KEY_BRIGHTNESS = "brightness"
STATE_KEY_POSITION = "position"
DOMAIN = "ads"
SERVICE_WRITE_DATA_BY_NAME = "write_data_by_name"
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_DEVICE): cv.string,
vol.Required(CONF_PORT): cv.port,
vol.Optional(CONF_IP_ADDRESS): cv.string,
}
)
},
extra=vol.ALLOW_EXTRA,
)
SCHEMA_SERVICE_WRITE_DATA_BY_NAME = vol.Schema(
{
vol.Required(CONF_ADS_TYPE): vol.In(
[
ADSTYPE_INT,
ADSTYPE_UINT,
ADSTYPE_BYTE,
ADSTYPE_BOOL,
ADSTYPE_DINT,
ADSTYPE_UDINT,
]
),
vol.Required(CONF_ADS_VALUE): vol.Coerce(int),
vol.Required(CONF_ADS_VAR): cv.string,
}
)
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
def setup(hass, config):
"""Set up the ADS component."""
2019-07-31 21:25:30 +02:00
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
conf = config[DOMAIN]
net_id = conf[CONF_DEVICE]
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
ip_address = conf.get(CONF_IP_ADDRESS)
port = conf[CONF_PORT]
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
client = pyads.Connection(net_id, port, ip_address)
AdsHub.ADS_TYPEMAP = {
ADSTYPE_BOOL: pyads.PLCTYPE_BOOL,
ADSTYPE_BYTE: pyads.PLCTYPE_BYTE,
ADSTYPE_DINT: pyads.PLCTYPE_DINT,
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
ADSTYPE_INT: pyads.PLCTYPE_INT,
ADSTYPE_UDINT: pyads.PLCTYPE_UDINT,
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
ADSTYPE_UINT: pyads.PLCTYPE_UINT,
}
AdsHub.ADSError = pyads.ADSError
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
AdsHub.PLCTYPE_BOOL = pyads.PLCTYPE_BOOL
AdsHub.PLCTYPE_BYTE = pyads.PLCTYPE_BYTE
AdsHub.PLCTYPE_DINT = pyads.PLCTYPE_DINT
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
AdsHub.PLCTYPE_INT = pyads.PLCTYPE_INT
AdsHub.PLCTYPE_UDINT = pyads.PLCTYPE_UDINT
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
AdsHub.PLCTYPE_UINT = pyads.PLCTYPE_UINT
try:
ads = AdsHub(client)
except pyads.ADSError:
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
_LOGGER.error(
"Could not connect to ADS host (netid=%s, ip=%s, port=%s)",
2019-07-31 21:25:30 +02:00
net_id,
ip_address,
port,
)
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
return False
hass.data[DATA_ADS] = ads
hass.bus.listen(EVENT_HOMEASSISTANT_STOP, ads.shutdown)
def handle_write_data_by_name(call):
"""Write a value to the connected ADS device."""
ads_var = call.data.get(CONF_ADS_VAR)
ads_type = call.data.get(CONF_ADS_TYPE)
value = call.data.get(CONF_ADS_VALUE)
try:
ads.write_by_name(ads_var, value, ads.ADS_TYPEMAP[ads_type])
except pyads.ADSError as err:
_LOGGER.error(err)
hass.services.register(
2019-07-31 21:25:30 +02:00
DOMAIN,
SERVICE_WRITE_DATA_BY_NAME,
handle_write_data_by_name,
schema=SCHEMA_SERVICE_WRITE_DATA_BY_NAME,
)
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
return True
# Tuple to hold data needed for notification
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
NotificationItem = namedtuple(
2019-07-31 21:25:30 +02:00
"NotificationItem", "hnotify huser name plc_datatype callback"
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
)
class AdsHub:
"""Representation of an ADS connection."""
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
def __init__(self, ads_client):
"""Initialize the ADS hub."""
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
self._client = ads_client
self._client.open()
# All ADS devices are registered here
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
self._devices = []
self._notification_items = {}
self._lock = threading.Lock()
def shutdown(self, *args, **kwargs):
"""Shutdown ADS connection."""
2019-07-31 21:25:30 +02:00
_LOGGER.debug("Shutting down ADS")
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
for notification_item in self._notification_items.values():
_LOGGER.debug(
"Deleting device notification %d, %d",
2019-07-31 21:25:30 +02:00
notification_item.hnotify,
notification_item.huser,
)
try:
self._client.del_device_notification(
2019-07-31 21:25:30 +02:00
notification_item.hnotify, notification_item.huser
)
except pyads.ADSError as err:
_LOGGER.error(err)
try:
self._client.close()
except pyads.ADSError as err:
_LOGGER.error(err)
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
def register_device(self, device):
"""Register a new device."""
self._devices.append(device)
def write_by_name(self, name, value, plc_datatype):
"""Write a value to the device."""
2019-07-31 21:25:30 +02:00
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
with self._lock:
try:
return self._client.write_by_name(name, value, plc_datatype)
except pyads.ADSError as err:
_LOGGER.error("Error writing %s: %s", name, err)
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
def read_by_name(self, name, plc_datatype):
"""Read a value from the device."""
2019-07-31 21:25:30 +02:00
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
with self._lock:
try:
return self._client.read_by_name(name, plc_datatype)
except pyads.ADSError as err:
_LOGGER.error("Error reading %s: %s", name, err)
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
def add_device_notification(self, name, plc_datatype, callback):
"""Add a notification to the ADS devices."""
2019-07-31 21:25:30 +02:00
attr = pyads.NotificationAttrib(ctypes.sizeof(plc_datatype))
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
with self._lock:
try:
hnotify, huser = self._client.add_device_notification(
2019-07-31 21:25:30 +02:00
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(
2019-07-31 21:25:30 +02:00
hnotify, huser, name, plc_datatype, callback
)
_LOGGER.debug(
2019-07-31 21:25:30 +02:00
"Added device notification %d for variable %s", hnotify, name
)
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
def _device_notification_callback(self, notification, name):
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
"""Handle device notifications."""
contents = notification.contents
hnotify = int(contents.hNotification)
_LOGGER.debug("Received notification %d", hnotify)
# get dynamically sized data array
data_size = contents.cbSampleSize
data = (ctypes.c_ubyte * data_size).from_address(
ctypes.addressof(contents)
+ pyads.structs.SAdsNotificationHeader.data.offset
)
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
try:
2019-03-05 11:07:40 +01:00
with self._lock:
notification_item = self._notification_items[hnotify]
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
except KeyError:
2019-03-05 11:07:40 +01:00
_LOGGER.error("Unknown device notification handle: %d", hnotify)
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
return
# Parse data to desired datatype
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
if notification_item.plc_datatype == self.PLCTYPE_BOOL:
value = bool(struct.unpack("<?", bytearray(data))[0])
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
elif notification_item.plc_datatype == self.PLCTYPE_INT:
value = struct.unpack("<h", bytearray(data))[0]
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
elif notification_item.plc_datatype == self.PLCTYPE_BYTE:
value = struct.unpack("<B", bytearray(data))[0]
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
elif notification_item.plc_datatype == self.PLCTYPE_UINT:
value = struct.unpack("<H", bytearray(data))[0]
elif notification_item.plc_datatype == self.PLCTYPE_DINT:
value = struct.unpack("<i", bytearray(data))[0]
elif notification_item.plc_datatype == self.PLCTYPE_UDINT:
value = struct.unpack("<I", bytearray(data))[0]
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
else:
value = bytearray(data)
_LOGGER.warning("No callback available for this datatype")
Add ADS component (#10142) * add ads hub, light and switch * add binary sensor prototype * switch: use adsvar for connection * fix some issues with binary sensor * fix binary sensor * fix all platforms * use latest pyads * fixed error with multiple binary sensors * add sensor * add ads sensor * clean up after shutdown * ads component with platforms switch, binary_sensor, light, sensor add locking poll sensors at startup update state of ads switch and light update ads requirements remove update() from constructors on ads platforms omit ads coverage ads catch read error when polling * add ads service * add default settings for use_notify and poll_interval * fix too long line * Fix style issues * no pydocstyle errors * Send and receive native brightness data to ADS device to prevent issues with math.floor reducing brightness -1 at every switch * Enable non dimmable lights * remove setting of self._state in switch * remove polling * Revert "remove polling" This reverts commit 7da420f82385a4a5c66a929af7025c00ed197e86. * add service schema, add links to documentation * fix naming, cleanup * re-remove polling * use async_added_to_hass for setup of callbacks * fix comment. * add callbacks for changed values * use async_add_job for creating device notifications * set should_poll to False for all platforms * change should_poll to property * add service description to services.yaml * add for brigthness not being None * put ads component in package * Remove whitespace * omit ads package
2017-12-05 09:44:22 +01:00
notification_item.callback(notification_item.name, value)
class AdsEntity(Entity):
"""Representation of ADS entity."""
_attr_should_poll = False
def __init__(self, ads_hub, name, ads_var):
"""Initialize ADS binary sensor."""
self._state_dict = {}
self._state_dict[STATE_KEY_STATE] = None
self._ads_hub = ads_hub
self._ads_var = ads_var
self._event = None
self._attr_unique_id = ads_var
self._attr_name = name
async def async_initialize_device(
2019-07-31 21:25:30 +02:00
self, ads_var, plctype, state_key=STATE_KEY_STATE, factor=None
):
"""Register device notification."""
2019-07-31 21:25:30 +02:00
def update(name, value):
"""Handle device notifications."""
2019-07-31 21:25:30 +02:00
_LOGGER.debug("Variable %s changed its value to %d", name, value)
if factor is None:
self._state_dict[state_key] = value
else:
self._state_dict[state_key] = value / factor
asyncio.run_coroutine_threadsafe(async_event_set(), self.hass.loop)
self.schedule_update_ha_state()
async def async_event_set():
"""Set event in async context."""
self._event.set()
self._event = asyncio.Event()
await self.hass.async_add_executor_job(
2019-07-31 21:25:30 +02:00
self._ads_hub.add_device_notification, ads_var, plctype, update
)
try:
with async_timeout.timeout(10):
await self._event.wait()
except asyncio.TimeoutError:
2019-07-31 21:25:30 +02:00
_LOGGER.debug("Variable %s: Timeout during first update", ads_var)
@property
def available(self) -> bool:
"""Return False if state has not been updated yet."""
return self._state_dict[STATE_KEY_STATE] is not None