Maintenance 2nd (#4106)
* Add link to docs * Fix link * Update line breaks * Update ordering * Align vera platofrm to only use add_devices (instead od add_devices_callback) * Remove line break * Use consts * Update ordering * Update ordering * Use const, create default name, use string formatting * Update ordering * Use const * Update import style * Update ordering and line breaks * update line breaks * Set default port * Set defaults and update ordering * Update ordering * Minor style updates * Update ordering, defaults, line breaks, and readability * Use constants * Add line breaks * use string formatting * Update line breaks * Update logger
This commit is contained in:
1 parent
e6ece4bf6d
commit
9f2aae1357
28 files changed
+272
-271
No files matched your search
@@ -15,18 +15,17 @@ from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
|
||||
from homeassistant.const import (CONF_HOST, CONF_PASSWORD, CONF_USERNAME)
|
||||
from homeassistant.util import Throttle
|
||||
|
||||
|
||||
REQUIREMENTS = ['https://github.com/mweinelt/anel-pwrctrl/archive/'
|
||||
'ed26e8830e28a2bfa4260a9002db23ce3e7e63d7.zip'
|
||||
'#anel_pwrctrl==0.0.1']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CONF_PORT_RECV = "port_recv"
|
||||
CONF_PORT_SEND = "port_send"
|
||||
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=5)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_PORT_RECV): cv.port,
|
||||
vol.Required(CONF_PORT_SEND): cv.port,
|
||||
@@ -48,13 +47,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
from anel_pwrctrl import DeviceMaster
|
||||
|
||||
try:
|
||||
master = DeviceMaster(username=username,
|
||||
password=password,
|
||||
read_port=port_send,
|
||||
write_port=port_recv)
|
||||
master = DeviceMaster(
|
||||
username=username, password=password, read_port=port_send,
|
||||
write_port=port_recv)
|
||||
master.query(ip_addr=host)
|
||||
except socket.error as ex:
|
||||
_LOGGER.error('Unable to discover PwrCtrl device: %s', str(ex))
|
||||
_LOGGER.error("Unable to discover PwrCtrl device: %s", str(ex))
|
||||
return False
|
||||
|
||||
devices = []
|
||||
@@ -84,7 +82,7 @@ class PwrCtrlSwitch(SwitchDevice):
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the unique ID of the device."""
|
||||
return "{device}-{switch_idx}".format(
|
||||
return '{device}-{switch_idx}'.format(
|
||||
device=self._port.device.host,
|
||||
switch_idx=self._port.get_index()
|
||||
)
|
||||
|
||||
@@ -10,8 +10,7 @@ import requests
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
|
||||
from homeassistant.const import (
|
||||
CONF_NAME, CONF_RESOURCE, CONF_TIMEOUT)
|
||||
from homeassistant.const import (CONF_NAME, CONF_RESOURCE, CONF_TIMEOUT)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
CONF_BODY_OFF = 'body_off'
|
||||
@@ -35,8 +34,8 @@ _LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# pylint: disable=unused-argument,
|
||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
"""Setup the RESTful switch."""
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up the RESTful switch."""
|
||||
name = config.get(CONF_NAME)
|
||||
resource = config.get(CONF_RESOURCE)
|
||||
body_on = config.get(CONF_BODY_ON)
|
||||
@@ -61,9 +60,9 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
_LOGGER.error("No route to resource/endpoint: %s", resource)
|
||||
return False
|
||||
|
||||
add_devices_callback(
|
||||
[RestSwitch(hass, name, resource,
|
||||
body_on, body_off, is_on_template, timeout)])
|
||||
add_devices(
|
||||
[RestSwitch(
|
||||
hass, name, resource, body_on, body_off, is_on_template, timeout)])
|
||||
|
||||
|
||||
# pylint: disable=too-many-arguments
|
||||
@@ -71,8 +70,8 @@ class RestSwitch(SwitchDevice):
|
||||
"""Representation of a switch that can be toggled using REST."""
|
||||
|
||||
# pylint: disable=too-many-instance-attributes
|
||||
def __init__(self, hass, name, resource, body_on, body_off,
|
||||
is_on_template, timeout):
|
||||
def __init__(self, hass, name, resource, body_on, body_off, is_on_template,
|
||||
timeout):
|
||||
"""Initialize the REST switch."""
|
||||
self._state = None
|
||||
self._hass = hass
|
||||
@@ -96,9 +95,8 @@ class RestSwitch(SwitchDevice):
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn the device on."""
|
||||
body_on_t = self._body_on.render()
|
||||
request = requests.post(self._resource,
|
||||
data=body_on_t,
|
||||
timeout=self._timeout)
|
||||
request = requests.post(
|
||||
self._resource, data=body_on_t, timeout=self._timeout)
|
||||
if request.status_code == 200:
|
||||
self._state = True
|
||||
else:
|
||||
@@ -108,9 +106,8 @@ class RestSwitch(SwitchDevice):
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the device off."""
|
||||
body_off_t = self._body_off.render()
|
||||
request = requests.post(self._resource,
|
||||
data=body_off_t,
|
||||
timeout=self._timeout)
|
||||
request = requests.post(
|
||||
self._resource, data=body_off_t, timeout=self._timeout)
|
||||
if request.status_code == 200:
|
||||
self._state = False
|
||||
else:
|
||||
|
||||
@@ -8,8 +8,7 @@ import logging
|
||||
|
||||
from homeassistant.util import convert
|
||||
from homeassistant.components.switch import SwitchDevice
|
||||
from homeassistant.const import (
|
||||
STATE_OFF, STATE_ON)
|
||||
from homeassistant.const import (STATE_OFF, STATE_ON)
|
||||
from homeassistant.components.vera import (
|
||||
VeraDevice, VERA_DEVICES, VERA_CONTROLLER)
|
||||
|
||||
@@ -18,9 +17,9 @@ DEPENDENCIES = ['vera']
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Find and return Vera switches."""
|
||||
add_devices_callback(
|
||||
add_devices(
|
||||
VeraSwitch(device, VERA_CONTROLLER) for
|
||||
device in VERA_DEVICES['switch'])
|
||||
|
||||
|
||||
@@ -30,13 +30,13 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
})
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
"""Add wake on lan switch."""
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Set up a wake on lan switch."""
|
||||
name = config.get(CONF_NAME)
|
||||
host = config.get(CONF_HOST)
|
||||
mac_address = config.get(CONF_MAC_ADDRESS)
|
||||
|
||||
add_devices_callback([WOLSwitch(hass, name, host, mac_address)])
|
||||
add_devices([WOLSwitch(hass, name, host, mac_address)])
|
||||
|
||||
|
||||
class WOLSwitch(SwitchDevice):
|
||||
@@ -79,13 +79,12 @@ class WOLSwitch(SwitchDevice):
|
||||
|
||||
def update(self):
|
||||
"""Check if device is on and update the state."""
|
||||
if platform.system().lower() == "windows":
|
||||
ping_cmd = "ping -n 1 -w {} {}"\
|
||||
.format(DEFAULT_PING_TIMEOUT * 1000, self._host)
|
||||
if platform.system().lower() == 'windows':
|
||||
ping_cmd = 'ping -n 1 -w {} {}'.format(
|
||||
DEFAULT_PING_TIMEOUT * 1000, self._host)
|
||||
else:
|
||||
ping_cmd = "ping -c 1 -W {} {}"\
|
||||
.format(DEFAULT_PING_TIMEOUT, self._host)
|
||||
ping_cmd = 'ping -c 1 -W {} {}'.format(
|
||||
DEFAULT_PING_TIMEOUT, self._host)
|
||||
|
||||
status = sp.getstatusoutput(ping_cmd)[0]
|
||||
|
||||
self._state = not bool(status)
|
||||
Reference in new issue
Block a user