1
mirror of https://github.com/home-assistant/core synced 2024-09-15 17:29:45 +02:00
ha-core/homeassistant/components/ads/switch.py

50 lines
1.5 KiB
Python
Raw Normal View History

"""Support for ADS switch platform."""
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 logging
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.components.switch import PLATFORM_SCHEMA, SwitchDevice
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
from homeassistant.const import CONF_NAME
import homeassistant.helpers.config_validation as cv
from . import CONF_ADS_VAR, DATA_ADS, STATE_KEY_STATE, AdsEntity
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
DEFAULT_NAME = "ADS Switch"
2019-07-31 21:25:30 +02:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Required(CONF_ADS_VAR): cv.string, vol.Optional(CONF_NAME): 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_platform(hass, config, add_entities, discovery_info=None):
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
"""Set up switch platform for ADS."""
ads_hub = hass.data.get(DATA_ADS)
name = config.get(CONF_NAME)
ads_var = config.get(CONF_ADS_VAR)
add_entities([AdsSwitch(ads_hub, name, ads_var)])
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 AdsSwitch(AdsEntity, SwitchDevice):
"""Representation of an ADS switch 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
async def async_added_to_hass(self):
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
"""Register device notification."""
2019-07-31 21:25:30 +02:00
await self.async_initialize_device(self._ads_var, self._ads_hub.PLCTYPE_BOOL)
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
@property
def is_on(self):
"""Return True if the entity is on."""
return self._state_dict[STATE_KEY_STATE]
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 turn_on(self, **kwargs):
"""Turn the switch on."""
2019-07-31 21:25:30 +02:00
self._ads_hub.write_by_name(self._ads_var, True, self._ads_hub.PLCTYPE_BOOL)
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 turn_off(self, **kwargs):
"""Turn the switch off."""
2019-07-31 21:25:30 +02:00
self._ads_hub.write_by_name(self._ads_var, False, self._ads_hub.PLCTYPE_BOOL)