hass-core/homeassistant/components/switch/hlk_sw16.py
James Hilliard 832fa61477 Initial hlk-sw16 relay switch support (#17855)
* Initial hlk-sw16 relay switch support

* remove entity_id and validate relay id's

* Bump hlk-sw16 library version and cleanup component

* refactor hlk-sw16 switch platform loading

* Use voluptuous to coerce relay id to string

* remove force_update for SW16Switch

* Move to callback based hlk-sw16 relay state changes

* fix hlk-sw16 default port and cleanup some unused variables

* Refactor to allow registration of multiple HLK-SW16 device

* Store protocol in instance variable instead of class variable

* remove is_connected

* flake8 style fix

* Move reconnect logic into HLK-SW16 client library

* Cleanup and improve logging

* Load hlk-sw16 platform entities at same time per device

* scope SIGNAL_AVAILABILITY to device_id

* Fixes for connection resume

* move device_client out of switches loop

* Add timeout for commands and keep alive

* remove unused variables
2018-12-03 09:31:53 +01:00

54 lines
1.6 KiB
Python

"""
Support for HLK-SW16 switches.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/switch.hlk_sw16/
"""
import logging
from homeassistant.components.hlk_sw16 import (
SW16Device, DOMAIN as HLK_SW16,
DATA_DEVICE_REGISTER)
from homeassistant.components.switch import (
ToggleEntity)
from homeassistant.const import CONF_NAME
DEPENDENCIES = [HLK_SW16]
_LOGGER = logging.getLogger(__name__)
def devices_from_config(hass, domain_config):
"""Parse configuration and add HLK-SW16 switch devices."""
switches = domain_config[0]
device_id = domain_config[1]
device_client = hass.data[DATA_DEVICE_REGISTER][device_id]
devices = []
for device_port, device_config in switches.items():
device_name = device_config.get(CONF_NAME, device_port)
device = SW16Switch(device_name, device_port, device_id, device_client)
devices.append(device)
return devices
async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up the HLK-SW16 platform."""
async_add_entities(devices_from_config(hass, discovery_info))
class SW16Switch(SW16Device, ToggleEntity):
"""Representation of a HLK-SW16 switch."""
@property
def is_on(self):
"""Return true if device is on."""
return self._is_on
async def async_turn_on(self, **kwargs):
"""Turn the device on."""
await self._client.turn_on(self._device_port)
async def async_turn_off(self, **kwargs):
"""Turn the device off."""
await self._client.turn_off(self._device_port)