core/homeassistant/components/switch/orvibo.py

87 lines
2.3 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
from homeassistant.components.switch import SwitchDevice
2015-11-14 19:25:53 +00:00
DEFAULT_NAME = "Orvibo S20 Switch"
2016-01-25 21:40:27 +00:00
REQUIREMENTS = ['orvibo==1.1.1']
2015-11-14 19:14:02 +00:00
_LOGGER = logging.getLogger(__name__)
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
2016-03-08 12:35:39 +00:00
"""Find and return S20 switches."""
2015-11-17 08:18:42 +00:00
from orvibo.s20 import S20, S20Exception
switches = []
switch_conf = config.get('switches', [config])
for switch in switch_conf:
if switch.get('host') is None:
_LOGGER.error("Missing required variable: host")
continue
host = switch.get('host')
try:
switches.append(S20Switch(switch.get('name', DEFAULT_NAME),
S20(host)))
_LOGGER.info("Initialized S20 at %s", host)
except S20Exception:
_LOGGER.exception("S20 at %s couldn't be initialized",
host)
add_devices_callback(switches)
2015-11-14 19:14:02 +00:00
class S20Switch(SwitchDevice):
2016-03-08 12:35:39 +00:00
"""Representsation of an S20 switch."""
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):
2016-03-08 12:35:39 +00:00
"""Polling is needed."""
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")