hass-core/homeassistant/components/switch/eufy.py
Matthew Garrett 8d48164f25 Add support for Eufy bulbs and switches (#13773)
* Add support for Eufy bulbs and switches

Add support for driving bulbs and switches from the Eufy range.

* Fix hound checks

* Satisfy pylint

* Handle review comments

* Review updates and test fixes

* PyLint is a bit too aggressive
2018-04-10 21:38:23 -04:00

73 lines
1.9 KiB
Python

"""
Support for Eufy switches.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/switch.eufy/
"""
import logging
from homeassistant.components.switch import SwitchDevice
DEPENDENCIES = ['eufy']
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up Eufy switches."""
if discovery_info is None:
return
add_devices([EufySwitch(discovery_info)], True)
class EufySwitch(SwitchDevice):
"""Representation of a Eufy switch."""
def __init__(self, device):
"""Initialize the light."""
# pylint: disable=import-error
import lakeside
self._state = None
self._name = device['name']
self._address = device['address']
self._code = device['code']
self._type = device['type']
self._switch = lakeside.switch(self._address, self._code, self._type)
self._switch.connect()
def update(self):
"""Synchronise state from the switch."""
self._switch.update()
self._state = self._switch.power
@property
def unique_id(self):
"""Return the ID of this light."""
return self._address
@property
def name(self):
"""Return the name of the device if any."""
return self._name
@property
def is_on(self):
"""Return true if device is on."""
return self._state
def turn_on(self, **kwargs):
"""Turn the specified switch on."""
try:
self._switch.set_state(True)
except BrokenPipeError:
self._switch.connect()
self._switch.set_state(power=True)
def turn_off(self, **kwargs):
"""Turn the specified switch off."""
try:
self._switch.set_state(False)
except BrokenPipeError:
self._switch.connect()
self._switch.set_state(False)