core/homeassistant/components/switch/orvibo.py

107 lines
3.0 KiB
Python
Raw Normal View History

2015-11-14 19:14:02 +00:00
"""
Support for Orvibo S20 Wifi Smart Switches.
For more details about this platform, please refer to the documentation at
2015-11-15 17:50:06 +00:00
https://home-assistant.io/components/switch.orvibo/
2015-11-14 19:14:02 +00:00
"""
import logging
2016-09-02 04:37:09 +00:00
import voluptuous as vol
2016-09-02 22:09:14 +00:00
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
from homeassistant.const import (
CONF_HOST, CONF_NAME, CONF_SWITCHES, CONF_MAC, CONF_DISCOVERY)
2016-09-02 04:37:09 +00:00
import homeassistant.helpers.config_validation as cv
2015-11-14 19:14:02 +00:00
2016-01-25 21:40:27 +00:00
REQUIREMENTS = ['orvibo==1.1.1']
2016-09-02 22:09:14 +00:00
2015-11-14 19:14:02 +00:00
_LOGGER = logging.getLogger(__name__)
2016-09-02 04:37:09 +00:00
DEFAULT_NAME = 'Orvibo S20 Switch'
DEFAULT_DISCOVERY = True
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_SWITCHES, default=[]):
vol.All(cv.ensure_list, [{
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_MAC): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string
2016-09-02 22:09:14 +00:00
}]),
vol.Optional(CONF_DISCOVERY, default=DEFAULT_DISCOVERY): cv.boolean,
2016-09-02 04:37:09 +00:00
})
2015-11-14 19:14:02 +00:00
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Set up S20 switches."""
2016-09-02 04:37:09 +00:00
from orvibo.s20 import discover, S20, S20Exception
2015-11-17 08:18:42 +00:00
2016-09-02 04:37:09 +00:00
switch_data = {}
switches = []
2016-09-02 04:37:09 +00:00
switch_conf = config.get(CONF_SWITCHES, [config])
if config.get(CONF_DISCOVERY):
_LOGGER.info("Discovering S20 switches ...")
switch_data.update(discover())
for switch in switch_conf:
2016-09-02 04:37:09 +00:00
switch_data[switch.get(CONF_HOST)] = switch
for host, data in switch_data.items():
try:
2016-09-02 22:09:14 +00:00
switches.append(S20Switch(data.get(CONF_NAME),
2016-09-02 04:37:09 +00:00
S20(host, mac=data.get(CONF_MAC))))
_LOGGER.info("Initialized S20 at %s", host)
except S20Exception:
2016-09-02 04:37:09 +00:00
_LOGGER.error("S20 at %s couldn't be initialized", host)
add_devices_callback(switches)
2015-11-14 19:14:02 +00:00
class S20Switch(SwitchDevice):
2016-09-02 04:37:09 +00:00
"""Representation of an S20 switch."""
2016-03-08 12:35:39 +00:00
2015-11-14 19:14:02 +00:00
def __init__(self, name, s20):
2016-03-08 12:35:39 +00:00
"""Initialize the S20 device."""
2015-11-17 08:18:42 +00:00
from orvibo.s20 import S20Exception
2015-11-14 19:14:02 +00:00
self._name = name
self._s20 = s20
2015-11-14 21:14:25 +00:00
self._state = False
2015-11-17 08:18:42 +00:00
self._exc = S20Exception
2015-11-14 19:14:02 +00:00
@property
def should_poll(self):
2017-05-03 08:11:39 +00:00
"""Return the polling state."""
2015-11-14 21:14:25 +00:00
return True
2015-11-14 19:14:02 +00:00
@property
def name(self):
2016-03-08 12:35:39 +00:00
"""Return the name of the switch."""
2015-11-14 19:14:02 +00:00
return self._name
@property
def is_on(self):
2016-03-08 12:35:39 +00:00
"""Return true if device is on."""
2015-11-14 21:14:25 +00:00
return self._state
def update(self):
2016-03-08 12:35:39 +00:00
"""Update device state."""
2015-11-14 19:14:02 +00:00
try:
2015-11-14 21:14:25 +00:00
self._state = self._s20.on
2015-11-17 08:18:42 +00:00
except self._exc:
2015-11-14 21:14:25 +00:00
_LOGGER.exception("Error while fetching S20 state")
2015-11-14 19:14:02 +00:00
def turn_on(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the device on."""
2015-11-14 19:14:02 +00:00
try:
self._s20.on = True
2015-11-17 08:18:42 +00:00
except self._exc:
2015-11-14 21:14:25 +00:00
_LOGGER.exception("Error while turning on S20")
2015-11-14 19:14:02 +00:00
def turn_off(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the device off."""
2015-11-14 19:14:02 +00:00
try:
self._s20.on = False
2015-11-17 08:18:42 +00:00
except self._exc:
2015-11-14 21:14:25 +00:00
_LOGGER.exception("Error while turning off S20")