2019-02-28 12:16:38 +00:00
|
|
|
"""Support for myStrom switches."""
|
2015-11-25 16:21:11 +00:00
|
|
|
import logging
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2016-07-15 16:02:20 +00:00
|
|
|
import voluptuous as vol
|
2015-11-25 16:21:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.switch import SwitchDevice, PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import CONF_NAME, CONF_HOST
|
2016-07-15 16:02:20 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-11-25 16:21:11 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "myStrom Switch"
|
2015-11-25 16:21:11 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-07-15 16:02:20 +00:00
|
|
|
|
2015-11-25 16:21:11 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Find and return myStrom switch."""
|
2018-03-27 11:09:01 +00:00
|
|
|
from pymystrom.switch import MyStromPlug, exceptions
|
2015-11-25 16:21:11 +00:00
|
|
|
|
2016-08-20 22:40:16 +00:00
|
|
|
name = config.get(CONF_NAME)
|
2016-07-15 16:02:20 +00:00
|
|
|
host = config.get(CONF_HOST)
|
2015-11-26 15:37:00 +00:00
|
|
|
|
2015-11-25 16:21:11 +00:00
|
|
|
try:
|
2016-07-15 16:02:20 +00:00
|
|
|
MyStromPlug(host).get_status()
|
|
|
|
except exceptions.MyStromConnectionError:
|
2018-06-10 09:38:23 +00:00
|
|
|
_LOGGER.error("No route to device: %s", host)
|
|
|
|
return
|
2015-11-25 16:21:11 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([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."""
|
2018-03-27 11:09:01 +00:00
|
|
|
from pymystrom.switch import MyStromPlug
|
2016-07-15 16:02:20 +00:00
|
|
|
|
2015-11-25 16:21:11 +00:00
|
|
|
self._name = name
|
|
|
|
self._resource = resource
|
2016-07-15 16:02:20 +00:00
|
|
|
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."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return bool(self.data["relay"])
|
2015-11-25 16:21:11 +00:00
|
|
|
|
|
|
|
@property
|
2017-03-19 21:02:11 +00:00
|
|
|
def current_power_w(self):
|
|
|
|
"""Return the current power consumption in W."""
|
2019-07-31 19:25:30 +00:00
|
|
|
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."""
|
2016-07-15 16:02:20 +00:00
|
|
|
from pymystrom import exceptions
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2015-11-25 17:07:29 +00:00
|
|
|
try:
|
2016-07-15 16:02:20 +00:00
|
|
|
self.plug.set_relay_on()
|
|
|
|
except exceptions.MyStromConnectionError:
|
2018-06-10 09:38:23 +00:00
|
|
|
_LOGGER.error("No route to device: %s", self._resource)
|
2015-11-25 16:21:11 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the switch off."""
|
2016-07-15 16:02:20 +00:00
|
|
|
from pymystrom import exceptions
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2015-11-25 17:07:29 +00:00
|
|
|
try:
|
2016-07-15 16:02:20 +00:00
|
|
|
self.plug.set_relay_off()
|
|
|
|
except exceptions.MyStromConnectionError:
|
2018-06-10 09:38:23 +00:00
|
|
|
_LOGGER.error("No route to device: %s", self._resource)
|
2015-11-25 16:21:11 +00:00
|
|
|
|
|
|
|
def update(self):
|
2016-07-15 16:02:20 +00:00
|
|
|
"""Get the latest data from the device and update the data."""
|
|
|
|
from pymystrom import exceptions
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2015-11-25 16:21:11 +00:00
|
|
|
try:
|
2016-07-15 16:02:20 +00:00
|
|
|
self.data = self.plug.get_status()
|
|
|
|
except exceptions.MyStromConnectionError:
|
2019-07-31 19:25:30 +00:00
|
|
|
self.data = {"power": 0, "relay": False}
|
2018-06-10 09:38:23 +00:00
|
|
|
_LOGGER.error("No route to device: %s", self._resource)
|