From 89972ed940e9afbcf5154d550d3b8ba4c7b69f79 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Fri, 15 Jul 2016 18:02:20 +0200 Subject: [PATCH] Add validation and switch python-mystrom (#2529) --- homeassistant/components/switch/mystrom.py | 79 +++++++++++----------- requirements_all.txt | 3 + 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/homeassistant/components/switch/mystrom.py b/homeassistant/components/switch/mystrom.py index c2e819bd223..867392e4661 100644 --- a/homeassistant/components/switch/mystrom.py +++ b/homeassistant/components/switch/mystrom.py @@ -6,36 +6,38 @@ https://home-assistant.io/components/switch.mystrom/ """ import logging -import requests +import voluptuous as vol +from homeassistant.const import (CONF_PLATFORM, CONF_NAME, CONF_HOST) +import homeassistant.helpers.config_validation as cv from homeassistant.components.switch import SwitchDevice +REQUIREMENTS = ['python-mystrom==0.3.6'] + DEFAULT_NAME = 'myStrom Switch' _LOGGER = logging.getLogger(__name__) +PLATFORM_SCHEMA = vol.Schema({ + vol.Required(CONF_PLATFORM): 'mystrom', + vol.Optional(CONF_NAME): cv.string, + vol.Required(CONF_HOST): cv.string, +}) + def setup_platform(hass, config, add_devices, discovery_info=None): """Find and return myStrom switch.""" - host = config.get('host') + from pymystrom import MyStromPlug, exceptions - if host is None: - _LOGGER.error('Missing required variable: host') - return False - - resource = 'http://{}'.format(host) + host = config.get(CONF_HOST) try: - requests.get(resource, timeout=10) - except requests.exceptions.ConnectionError: - _LOGGER.error("No route to device %s. " - "Please check the IP address in the configuration file", - host) + MyStromPlug(host).get_status() + except exceptions.MyStromConnectionError: + _LOGGER.error("No route to device '%s'", host) return False - add_devices([MyStromSwitch( - config.get('name', DEFAULT_NAME), - resource)]) + add_devices([MyStromSwitch(config.get('name', DEFAULT_NAME), host)]) class MyStromSwitch(SwitchDevice): @@ -43,10 +45,13 @@ class MyStromSwitch(SwitchDevice): def __init__(self, name, resource): """Initialize the myStrom switch.""" - self._state = False + from pymystrom import MyStromPlug + self._name = name self._resource = resource - self.consumption = 0 + self.data = {} + self.plug = MyStromPlug(self._resource) + self.update() @property def name(self): @@ -56,45 +61,37 @@ class MyStromSwitch(SwitchDevice): @property def is_on(self): """Return true if switch is on.""" - return self._state + return bool(self.data['relay']) @property def current_power_mwh(self): - """Return the urrent power consumption in mWh.""" - return self.consumption + """Return the current power consumption in mWh.""" + return round(self.data['power'], 2) def turn_on(self, **kwargs): """Turn the switch on.""" + from pymystrom import exceptions try: - request = requests.get('{}/relay'.format(self._resource), - params={'state': '1'}, - timeout=10) - if request.status_code == 200: - self._state = True - except requests.exceptions.ConnectionError: - _LOGGER.error("Can't turn on %s. Is device offline?", + self.plug.set_relay_on() + except exceptions.MyStromConnectionError: + _LOGGER.error("No route to device '%s'. Is device offline?", self._resource) def turn_off(self, **kwargs): """Turn the switch off.""" + from pymystrom import exceptions try: - request = requests.get('{}/relay'.format(self._resource), - params={'state': '0'}, - timeout=10) - if request.status_code == 200: - self._state = False - except requests.exceptions.ConnectionError: - _LOGGER.error("Can't turn on %s. Is device offline?", + self.plug.set_relay_off() + except exceptions.MyStromConnectionError: + _LOGGER.error("No route to device '%s'. Is device offline?", self._resource) def update(self): - """Get the latest data from REST API and update the state.""" + """Get the latest data from the device and update the data.""" + from pymystrom import exceptions try: - request = requests.get('{}/report'.format(self._resource), - timeout=10) - data = request.json() - self._state = bool(data['relay']) - self.consumption = data['power'] - except requests.exceptions.ConnectionError: + self.data = self.plug.get_status() + except exceptions.MyStromConnectionError: + self.data = {'power': 0, 'relay': False} _LOGGER.error("No route to device '%s'. Is device offline?", self._resource) diff --git a/requirements_all.txt b/requirements_all.txt index 61485b4d617..62c5991d51a 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -317,6 +317,9 @@ python-forecastio==1.3.4 # homeassistant.components.media_player.mpd python-mpd2==0.5.5 +# homeassistant.components.switch.mystrom +python-mystrom==0.3.6 + # homeassistant.components.nest python-nest==2.9.2