core/homeassistant/components/switch/mystrom.py

98 lines
2.8 KiB
Python
Raw Normal View History

2015-11-25 16:21:11 +00:00
"""
Support for myStrom switches.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/switch.mystrom/
"""
import logging
2016-02-19 05:27:50 +00:00
import voluptuous as vol
2015-11-25 16:21:11 +00:00
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
from homeassistant.const import (CONF_NAME, CONF_HOST)
import homeassistant.helpers.config_validation as cv
2015-11-25 16:21:11 +00:00
REQUIREMENTS = ['python-mystrom==0.3.6']
2015-11-25 16:21:11 +00:00
DEFAULT_NAME = 'myStrom Switch'
_LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})
2015-11-25 16:21:11 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
2016-03-08 12:35:39 +00:00
"""Find and return myStrom switch."""
from pymystrom import MyStromPlug, exceptions
2015-11-25 16:21:11 +00:00
name = config.get(CONF_NAME)
host = config.get(CONF_HOST)
2015-11-25 16:21:11 +00:00
try:
MyStromPlug(host).get_status()
except exceptions.MyStromConnectionError:
_LOGGER.error("No route to device '%s'", host)
2015-11-25 16:21:11 +00:00
return False
add_devices([MyStromSwitch(name, host)])
2015-11-25 16:21:11 +00:00
class MyStromSwitch(SwitchDevice):
2016-03-08 12:35:39 +00:00
"""Representation of a myStrom switch."""
2015-11-25 16:21:11 +00:00
def __init__(self, name, resource):
2016-03-08 12:35:39 +00:00
"""Initialize the myStrom switch."""
from pymystrom import MyStromPlug
2015-11-25 16:21:11 +00:00
self._name = name
self._resource = resource
self.data = {}
self.plug = MyStromPlug(self._resource)
self.update()
2015-11-25 16:21:11 +00:00
@property
def name(self):
2016-03-08 12:35:39 +00:00
"""Return the name of the switch."""
2015-11-25 16:21:11 +00:00
return self._name
@property
def is_on(self):
2016-03-08 12:35:39 +00:00
"""Return true if switch is on."""
return bool(self.data['relay'])
2015-11-25 16:21:11 +00:00
@property
def current_power_mwh(self):
"""Return the current power consumption in mWh."""
return round(self.data['power'], 2)
2015-11-25 16:21:11 +00:00
def turn_on(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the switch on."""
from pymystrom import exceptions
2015-11-25 17:07:29 +00:00
try:
self.plug.set_relay_on()
except exceptions.MyStromConnectionError:
_LOGGER.error("No route to device '%s'. Is device offline?",
2015-11-25 16:21:11 +00:00
self._resource)
def turn_off(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the switch off."""
from pymystrom import exceptions
2015-11-25 17:07:29 +00:00
try:
self.plug.set_relay_off()
except exceptions.MyStromConnectionError:
_LOGGER.error("No route to device '%s'. Is device offline?",
2015-11-25 16:21:11 +00:00
self._resource)
def update(self):
"""Get the latest data from the device and update the data."""
from pymystrom import exceptions
2015-11-25 16:21:11 +00:00
try:
self.data = self.plug.get_status()
except exceptions.MyStromConnectionError:
self.data = {'power': 0, 'relay': False}
2015-11-25 16:21:11 +00:00
_LOGGER.error("No route to device '%s'. Is device offline?",
self._resource)