From 6310deb5c20b6e4cd4996582426f486081421359 Mon Sep 17 00:00:00 2001 From: Mark Perdue Date: Wed, 14 Mar 2018 03:10:47 -0400 Subject: [PATCH] Add new platform for VeSync switches (#13000) * Added vesync platform Support for power toggling, current power, and daily energy kWh * Adds vesync to requirements file. * Reorder vesync item in requirements_all.txt from gen_requirements_all * Removes unnecessary global values that are not used in this component * Removes try/catch from setup_platform -no throws. Guard check login() * Remove unnecessary boolean convert * Fix indentation of log messages --- homeassistant/components/switch/vesync.py | 104 ++++++++++++++++++++++ requirements_all.txt | 3 + 2 files changed, 107 insertions(+) create mode 100644 homeassistant/components/switch/vesync.py diff --git a/homeassistant/components/switch/vesync.py b/homeassistant/components/switch/vesync.py new file mode 100644 index 00000000000..fbc73545e19 --- /dev/null +++ b/homeassistant/components/switch/vesync.py @@ -0,0 +1,104 @@ +""" +Support for Etekcity VeSync 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_USERNAME, CONF_PASSWORD) +import homeassistant.helpers.config_validation as cv + + +REQUIREMENTS = ['pyvesync==0.1.1'] + +_LOGGER = logging.getLogger(__name__) + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_USERNAME): cv.string, + vol.Required(CONF_PASSWORD): cv.string, +}) + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Set up the VeSync switch platform.""" + from pyvesync.vesync import VeSync + + switches = [] + + manager = VeSync(config.get(CONF_USERNAME), config.get(CONF_PASSWORD)) + + if not manager.login(): + _LOGGER.error("Unable to login to VeSync") + return + + manager.update() + + if manager.devices is not None and manager.devices: + if len(manager.devices) == 1: + count_string = 'switch' + else: + count_string = 'switches' + + _LOGGER.info("Discovered %d VeSync %s", + len(manager.devices), count_string) + + for switch in manager.devices: + switches.append(VeSyncSwitchHA(switch)) + _LOGGER.info("Added a VeSync switch named '%s'", + switch.device_name) + else: + _LOGGER.info("No VeSync devices found") + + add_devices(switches) + + +class VeSyncSwitchHA(SwitchDevice): + """Representation of a VeSync switch.""" + + def __init__(self, plug): + """Initialize the VeSync switch device.""" + self.smartplug = plug + + @property + def unique_id(self): + """Return the ID of this switch.""" + return self.smartplug.cid + + @property + def name(self): + """Return the name of the switch.""" + return self.smartplug.device_name + + @property + def current_power_w(self): + """Return the current power usage in W.""" + return self.smartplug.get_power() + + @property + def today_energy_kwh(self): + """Return the today total energy usage in kWh.""" + return self.smartplug.get_kwh_today() + + @property + def available(self) -> bool: + """Return True if switch is available.""" + return self.smartplug.connection_status == "online" + + @property + def is_on(self): + """Return True if switch is on.""" + return self.smartplug.device_status == "on" + + def turn_on(self, **kwargs): + """Turn the switch on.""" + self.smartplug.turn_on() + + def turn_off(self, **kwargs): + """Turn the switch off.""" + self.smartplug.turn_off() + + def update(self): + """Handle data changes for node values.""" + self.smartplug.update() diff --git a/requirements_all.txt b/requirements_all.txt index 9ad1be678fa..1733f0d9a7d 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1023,6 +1023,9 @@ pyunifi==2.13 # homeassistant.components.vera pyvera==0.2.42 +# homeassistant.components.switch.vesync +pyvesync==0.1.1 + # homeassistant.components.media_player.vizio pyvizio==0.0.2