2019-02-14 15:01:46 +00:00
|
|
|
"""Support for HLK-SW16 switches."""
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.components.switch import ToggleEntity
|
2018-12-03 08:31:53 +00:00
|
|
|
|
2019-04-12 17:13:30 +00:00
|
|
|
from . import DATA_DEVICE_REGISTER, SW16Device
|
2020-08-03 03:52:53 +00:00
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
|
|
PARALLEL_UPDATES = 0
|
2018-12-03 08:31:53 +00:00
|
|
|
|
|
|
|
|
2020-08-03 03:52:53 +00:00
|
|
|
def devices_from_entities(hass, entry):
|
2018-12-03 08:31:53 +00:00
|
|
|
"""Parse configuration and add HLK-SW16 switch devices."""
|
2020-08-03 03:52:53 +00:00
|
|
|
device_client = hass.data[DOMAIN][entry.entry_id][DATA_DEVICE_REGISTER]
|
2018-12-03 08:31:53 +00:00
|
|
|
devices = []
|
2020-08-03 03:52:53 +00:00
|
|
|
for i in range(16):
|
|
|
|
device_port = f"{i:01x}"
|
|
|
|
device = SW16Switch(device_port, entry.entry_id, device_client)
|
2018-12-03 08:31:53 +00:00
|
|
|
devices.append(device)
|
|
|
|
return devices
|
|
|
|
|
|
|
|
|
2020-08-03 03:52:53 +00:00
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
2018-12-03 08:31:53 +00:00
|
|
|
"""Set up the HLK-SW16 platform."""
|
2020-08-03 03:52:53 +00:00
|
|
|
async_add_entities(devices_from_entities(hass, entry))
|
2018-12-03 08:31:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|