Added lumitek/ankuoo recswitch component (#15764)

* Added lumitek/ankuoo recswitch component

* cosmetics

* remove callback

* cosmetics

* update requirements pyrecswitch==1.0.2

* add in .coveragerc
This commit is contained in:
marcolertora 2018-10-09 10:13:03 +02:00 committed by Paulus Schoutsen
parent 757ba3b60e
commit 0c34c50d2f
3 changed files with 105 additions and 0 deletions

View File

@ -820,6 +820,7 @@ omit =
homeassistant/components/switch/pulseaudio_loopback.py
homeassistant/components/switch/rainbird.py
homeassistant/components/switch/rest.py
homeassistant/components/switch/recswitch.py
homeassistant/components/switch/rpi_rf.py
homeassistant/components/switch/snmp.py
homeassistant/components/switch/switchbot.py

View File

@ -0,0 +1,101 @@
"""
Support for Ankuoo RecSwitch MS6126 devices.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/switch.recswitch/
"""
import logging
import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['pyrecswitch==1.0.2']
DEFAULT_NAME = 'RecSwitch {0}'
DATA_RSN = 'RSN'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Required(CONF_MAC): vol.All(cv.string, vol.Upper),
vol.Optional(CONF_NAME): cv.string,
})
async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up the device."""
from pyrecswitch import RSNetwork
host = config[CONF_HOST]
mac_address = config[CONF_MAC]
device_name = config.get(CONF_NAME)
if not hass.data.get(DATA_RSN):
hass.data[DATA_RSN] = RSNetwork()
job = hass.data[DATA_RSN].create_datagram_endpoint(loop=hass.loop)
hass.async_create_task(job)
device = hass.data[DATA_RSN].register_device(mac_address, host)
async_add_entities([RecSwitchSwitch(device, device_name, mac_address)])
class RecSwitchSwitch(SwitchDevice):
"""Representation of a recswitch device."""
def __init__(self, device, device_name, mac_address):
"""Initialize a recswitch device."""
self.gpio_state = False
self.device = device
self.device_name = device_name
self.mac_address = mac_address
if not self.device_name:
self.device_name = DEFAULT_NAME.format(self.mac_address)
@property
def unique_id(self):
"""Return the switch unique ID."""
return self.mac_address
@property
def name(self):
"""Return the switch name."""
return self.device_name
@property
def is_on(self):
"""Return true if switch is on."""
return self.gpio_state
async def async_turn_on(self, **kwargs):
"""Turn on the switch."""
await self.async_set_gpio_status(True)
async def async_turn_off(self, **kwargs):
"""Turn off the switch."""
await self.async_set_gpio_status(False)
async def async_set_gpio_status(self, status):
"""Set the switch status."""
from pyrecswitch import RSNetworkError
try:
ret = await self.device.set_gpio_status(status)
self.gpio_state = ret.state
except RSNetworkError as error:
_LOGGER.error('Setting status to %s: %r', self.name, error)
async def async_update(self):
"""Update the current switch status."""
from pyrecswitch import RSNetworkError
try:
ret = await self.device.get_gpio_status()
self.gpio_state = ret.state
except RSNetworkError as error:
_LOGGER.error('Reading status from %s: %r', self.name, error)

View File

@ -1051,6 +1051,9 @@ pyqwikswitch==0.8
# homeassistant.components.rainbird
pyrainbird==0.1.6
# homeassistant.components.switch.recswitch
pyrecswitch==1.0.2
# homeassistant.components.sabnzbd
pysabnzbd==1.0.1