Move imports to top for mystrom (#33629)

This commit is contained in:
Franck Nijhof 2020-04-04 16:09:49 +02:00 committed by GitHub
parent 29df8e546f
commit 9dfcae3dc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,8 @@
"""Support for myStrom switches.""" """Support for myStrom switches."""
import logging import logging
from pymystrom.exceptions import MyStromConnectionError
from pymystrom.switch import MyStromPlug
import voluptuous as vol import voluptuous as vol
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchDevice
@ -22,14 +24,12 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
def setup_platform(hass, config, add_entities, discovery_info=None): def setup_platform(hass, config, add_entities, discovery_info=None):
"""Find and return myStrom switch.""" """Find and return myStrom switch."""
from pymystrom.switch import MyStromPlug, exceptions
name = config.get(CONF_NAME) name = config.get(CONF_NAME)
host = config.get(CONF_HOST) host = config.get(CONF_HOST)
try: try:
MyStromPlug(host).get_status() MyStromPlug(host).get_status()
except exceptions.MyStromConnectionError: except MyStromConnectionError:
_LOGGER.error("No route to device: %s", host) _LOGGER.error("No route to device: %s", host)
raise PlatformNotReady() raise PlatformNotReady()
@ -41,8 +41,6 @@ class MyStromSwitch(SwitchDevice):
def __init__(self, name, resource): def __init__(self, name, resource):
"""Initialize the myStrom switch.""" """Initialize the myStrom switch."""
from pymystrom.switch import MyStromPlug
self._name = name self._name = name
self._resource = resource self._resource = resource
self.data = {} self.data = {}
@ -71,30 +69,24 @@ class MyStromSwitch(SwitchDevice):
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
"""Turn the switch on.""" """Turn the switch on."""
from pymystrom import exceptions
try: try:
self.plug.set_relay_on() self.plug.set_relay_on()
except exceptions.MyStromConnectionError: except MyStromConnectionError:
_LOGGER.error("No route to device: %s", self._resource) _LOGGER.error("No route to device: %s", self._resource)
def turn_off(self, **kwargs): def turn_off(self, **kwargs):
"""Turn the switch off.""" """Turn the switch off."""
from pymystrom import exceptions
try: try:
self.plug.set_relay_off() self.plug.set_relay_off()
except exceptions.MyStromConnectionError: except MyStromConnectionError:
_LOGGER.error("No route to device: %s", self._resource) _LOGGER.error("No route to device: %s", self._resource)
def update(self): def update(self):
"""Get the latest data from the device and update the data.""" """Get the latest data from the device and update the data."""
from pymystrom import exceptions
try: try:
self.data = self.plug.get_status() self.data = self.plug.get_status()
self._available = True self._available = True
except exceptions.MyStromConnectionError: except MyStromConnectionError:
self.data = {"power": 0, "relay": False} self.data = {"power": 0, "relay": False}
self._available = False self._available = False
_LOGGER.error("No route to device: %s", self._resource) _LOGGER.error("No route to device: %s", self._resource)