core/homeassistant/components/orvibo/switch.py

111 lines
2.9 KiB
Python
Raw Normal View History

"""Support for Orvibo S20 Wifi Smart Switches."""
2015-11-14 19:14:02 +00:00
import logging
from orvibo.s20 import S20, S20Exception, discover
2016-09-02 04:37:09 +00:00
import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
2016-09-02 22:09:14 +00:00
from homeassistant.const import (
CONF_DISCOVERY,
2019-07-31 19:25:30 +00:00
CONF_HOST,
CONF_MAC,
2019-07-31 19:25:30 +00:00
CONF_NAME,
CONF_SWITCHES,
)
2016-09-02 04:37:09 +00:00
import homeassistant.helpers.config_validation as cv
2015-11-14 19:14:02 +00:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "Orvibo S20 Switch"
2016-09-02 04:37:09 +00:00
DEFAULT_DISCOVERY = True
2019-07-31 19:25:30 +00:00
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,
}
],
),
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_entities_callback, discovery_info=None):
"""Set up S20 switches."""
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:
2019-07-31 19:25:30 +00:00
switches.append(
S20Switch(data.get(CONF_NAME), 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_entities_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
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")