2020-04-23 23:00:17 +00:00
|
|
|
"""Support for myStrom switches/plugs."""
|
2015-11-25 16:21:11 +00:00
|
|
|
import logging
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2020-04-04 14:09:49 +00:00
|
|
|
from pymystrom.exceptions import MyStromConnectionError
|
2020-04-23 23:00:17 +00:00
|
|
|
from pymystrom.switch import MyStromSwitch as _MyStromSwitch
|
2016-07-15 16:02:20 +00:00
|
|
|
import voluptuous as vol
|
2015-11-25 16:21:11 +00:00
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
|
2019-12-03 23:44:50 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_NAME
|
2020-02-01 22:04:42 +00:00
|
|
|
from homeassistant.exceptions import PlatformNotReady
|
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
|
|
|
|
2020-04-23 23:00:17 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
|
|
|
"""Set up the myStrom switch/plug integration."""
|
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:
|
2020-04-23 23:00:17 +00:00
|
|
|
plug = _MyStromSwitch(host)
|
|
|
|
await plug.get_state()
|
2020-08-28 11:50:32 +00:00
|
|
|
except MyStromConnectionError as err:
|
2020-04-23 23:00:17 +00:00
|
|
|
_LOGGER.error("No route to myStrom plug: %s", host)
|
2020-08-28 11:50:32 +00:00
|
|
|
raise PlatformNotReady() from err
|
2015-11-25 16:21:11 +00:00
|
|
|
|
2020-04-23 23:00:17 +00:00
|
|
|
async_add_entities([MyStromSwitch(plug, name)])
|
2015-11-25 16:21:11 +00:00
|
|
|
|
|
|
|
|
2020-04-26 16:50:37 +00:00
|
|
|
class MyStromSwitch(SwitchEntity):
|
2020-04-23 23:00:17 +00:00
|
|
|
"""Representation of a myStrom switch/plug."""
|
2016-03-08 12:35:39 +00:00
|
|
|
|
2020-04-23 23:00:17 +00:00
|
|
|
def __init__(self, plug, name):
|
|
|
|
"""Initialize the myStrom switch/plug."""
|
2015-11-25 16:21:11 +00:00
|
|
|
self._name = name
|
2020-04-23 23:00:17 +00:00
|
|
|
self.plug = plug
|
2020-02-01 22:04:42 +00:00
|
|
|
self._available = True
|
2020-04-23 23:00:17 +00:00
|
|
|
self.relay = None
|
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."""
|
2020-04-23 23:00:17 +00:00
|
|
|
return bool(self.relay)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return self.plug._mac # pylint: disable=protected-access
|
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."""
|
2020-04-23 23:00:17 +00:00
|
|
|
return self.plug.consumption
|
2015-11-25 16:21:11 +00:00
|
|
|
|
2020-02-01 22:04:42 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Could the device be accessed during the last update call."""
|
|
|
|
return self._available
|
|
|
|
|
2020-04-23 23:00:17 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the switch on."""
|
2015-11-25 17:07:29 +00:00
|
|
|
try:
|
2020-04-23 23:00:17 +00:00
|
|
|
await self.plug.turn_on()
|
2020-04-04 14:09:49 +00:00
|
|
|
except MyStromConnectionError:
|
2020-04-23 23:00:17 +00:00
|
|
|
_LOGGER.error("No route to myStrom plug")
|
2015-11-25 16:21:11 +00:00
|
|
|
|
2020-04-23 23:00:17 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the switch off."""
|
2015-11-25 17:07:29 +00:00
|
|
|
try:
|
2020-04-23 23:00:17 +00:00
|
|
|
await self.plug.turn_off()
|
2020-04-04 14:09:49 +00:00
|
|
|
except MyStromConnectionError:
|
2020-04-23 23:00:17 +00:00
|
|
|
_LOGGER.error("No route to myStrom plug")
|
2015-11-25 16:21:11 +00:00
|
|
|
|
2020-04-23 23:00:17 +00:00
|
|
|
async def async_update(self):
|
2016-07-15 16:02:20 +00:00
|
|
|
"""Get the latest data from the device and update the data."""
|
2015-11-25 16:21:11 +00:00
|
|
|
try:
|
2020-04-23 23:00:17 +00:00
|
|
|
await self.plug.get_state()
|
|
|
|
self.relay = self.plug.relay
|
2020-02-01 22:04:42 +00:00
|
|
|
self._available = True
|
2020-04-04 14:09:49 +00:00
|
|
|
except MyStromConnectionError:
|
2020-02-01 22:04:42 +00:00
|
|
|
self._available = False
|
2020-04-23 23:00:17 +00:00
|
|
|
_LOGGER.error("No route to myStrom plug")
|