Created
December 6, 2017 23:15
-
-
Save tylergets/e8d68c835c43c72043a6be151dbe4d80 to your computer and use it in GitHub Desktop.
VeSync Home Assistant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Support for Etekcity Wifi Smart Switches. | |
For more details about this platform, please refer to the documentation at | |
https://home-assistant.io/components/switch.vesync/ | |
""" | |
import logging | |
import voluptuous as vol | |
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA) | |
from homeassistant.const import CONF_NAME, CONF_SWITCHES, CONF_USERNAME, CONF_PASSWORD | |
import homeassistant.helpers.config_validation as cv | |
REQUIREMENTS = ['vesync==1.0.1'] | |
_LOGGER = logging.getLogger(__name__) | |
DEFAULT_NAME = 'Etekcity Vesync Switch' | |
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | |
vol.Required(CONF_USERNAME): cv.string, | |
vol.Required(CONF_PASSWORD): cv.string | |
}) | |
# pylint: disable=unused-argument | |
def setup_platform(hass, config, add_devices_callback, discovery_info=None): | |
"""Set up Vesync switches.""" | |
from vesync.api import VesyncApi | |
switch_data = {} | |
switches = [] | |
try: | |
api = VesyncApi(config.get(CONF_USERNAME),config.get(CONF_PASSWORD)) | |
_LOGGER.info("Connected to VeSync API") | |
except RuntimeException: | |
_LOGGER.error("Failed to connect to VeSync API") | |
try: | |
devices = api.get_devices() | |
_LOGGER.info("Retrieved devices from VeSync API") | |
except RuntimeException: | |
_LOGGER.error("Failed to retrieve devices from VeSync API") | |
for switch in switch_data.items(): | |
switches.append(VesyncSwitch(switch,api)) | |
add_devices_callback(switches) | |
class VesyncSwitch(SwitchDevice): | |
"""Representation of an EtekCity Veysnc switch.""" | |
def __init__(self, switch, api): | |
"""Initialize the Veysnc device.""" | |
self._switch = switch | |
self._api = api | |
@property | |
def should_poll(self): | |
"""Return the polling state.""" | |
return True | |
@property | |
def name(self): | |
"""Return the name of the switch.""" | |
return self._switch["deviceName"] | |
@property | |
def is_on(self): | |
"""Return true if device is on.""" | |
if (self._switch["deviceStatus"] == "on"): | |
return True | |
else: | |
return False | |
def update(self): | |
"""Update device state.""" | |
try: | |
devices = self._api.get_devices() | |
for device in devices: | |
if (device["cid"] == self._switch["cid"]): | |
self._switch = device | |
break | |
except RuntimeException: | |
_LOGGER.exception("Error while fetching VeSync state") | |
def turn_on(self, **kwargs): | |
"""Turn the device on.""" | |
try: | |
self._api.turn_on(self._switch["cid"]) | |
except RuntimeException: | |
_LOGGER.exception("Error while turning on VeSync") | |
def turn_off(self, **kwargs): | |
"""Turn the device on.""" | |
try: | |
self._api.turn_off(self._switch["cid"]) | |
except RuntimeException: | |
_LOGGER.exception("Error while turning off VeSync") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment