* Remove unnecessary exception re-wraps * Preserve exception chains on re-raise We slap "from cause" to almost all possible cases here. In some cases it could conceivably be better to do "from None" if we really want to hide the cause. However those should be in the minority, and "from cause" should be an improvement over the corresponding raise without a "from" in all cases anyway. The only case where we raise from None here is in plex, where the exception for an original invalid SSL cert is not the root cause for failure to validate a newly fetched one. Follow local convention on exception variable names if there is a consistent one, otherwise `err` to match with majority of codebase. * Fix mistaken re-wrap in homematicip_cloud/hap.py Missed the difference between HmipConnectionError and HmipcConnectionError. * Do not hide original error on plex new cert validation error Original is not the cause for the new one, but showing old in the traceback is useful nevertheless.
97 lines
2.8 KiB
Python
97 lines
2.8 KiB
Python
"""Support for myStrom switches/plugs."""
|
|
import logging
|
|
|
|
from pymystrom.exceptions import MyStromConnectionError
|
|
from pymystrom.switch import MyStromSwitch as _MyStromSwitch
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
|
|
from homeassistant.const import CONF_HOST, CONF_NAME
|
|
from homeassistant.exceptions import PlatformNotReady
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
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,
|
|
}
|
|
)
|
|
|
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
|
"""Set up the myStrom switch/plug integration."""
|
|
name = config.get(CONF_NAME)
|
|
host = config.get(CONF_HOST)
|
|
|
|
try:
|
|
plug = _MyStromSwitch(host)
|
|
await plug.get_state()
|
|
except MyStromConnectionError as err:
|
|
_LOGGER.error("No route to myStrom plug: %s", host)
|
|
raise PlatformNotReady() from err
|
|
|
|
async_add_entities([MyStromSwitch(plug, name)])
|
|
|
|
|
|
class MyStromSwitch(SwitchEntity):
|
|
"""Representation of a myStrom switch/plug."""
|
|
|
|
def __init__(self, plug, name):
|
|
"""Initialize the myStrom switch/plug."""
|
|
self._name = name
|
|
self.plug = plug
|
|
self._available = True
|
|
self.relay = None
|
|
|
|
@property
|
|
def name(self):
|
|
"""Return the name of the switch."""
|
|
return self._name
|
|
|
|
@property
|
|
def is_on(self):
|
|
"""Return true if switch is on."""
|
|
return bool(self.relay)
|
|
|
|
@property
|
|
def unique_id(self):
|
|
"""Return a unique ID."""
|
|
return self.plug._mac # pylint: disable=protected-access
|
|
|
|
@property
|
|
def current_power_w(self):
|
|
"""Return the current power consumption in W."""
|
|
return self.plug.consumption
|
|
|
|
@property
|
|
def available(self):
|
|
"""Could the device be accessed during the last update call."""
|
|
return self._available
|
|
|
|
async def async_turn_on(self, **kwargs):
|
|
"""Turn the switch on."""
|
|
try:
|
|
await self.plug.turn_on()
|
|
except MyStromConnectionError:
|
|
_LOGGER.error("No route to myStrom plug")
|
|
|
|
async def async_turn_off(self, **kwargs):
|
|
"""Turn the switch off."""
|
|
try:
|
|
await self.plug.turn_off()
|
|
except MyStromConnectionError:
|
|
_LOGGER.error("No route to myStrom plug")
|
|
|
|
async def async_update(self):
|
|
"""Get the latest data from the device and update the data."""
|
|
try:
|
|
await self.plug.get_state()
|
|
self.relay = self.plug.relay
|
|
self._available = True
|
|
except MyStromConnectionError:
|
|
self._available = False
|
|
_LOGGER.error("No route to myStrom plug")
|