Minor changes (switch.hook) (#4553)

* Use string formatting, add link to docs, and pylint

* Extent platform for validation
This commit is contained in:
Fabian Affolter 2016-11-24 00:21:48 +01:00 committed by GitHub
parent c04a002c55
commit 475c412ae4

View file

@ -1,43 +1,41 @@
""" """
Support Hook, available at hooksmarthome.com. Support Hook, available at hooksmarthome.com.
Controls RF switches like these: For more details about this platform, please refer to the documentation at
https://www.amazon.com/Etekcity-Wireless-Electrical-Household-Appliances/dp/B00DQELHBS https://home-assistant.io/components/switch.hook/
There is no way to query for state or success of commands.
""" """
import logging import logging
import asyncio import asyncio
import voluptuous as vol import voluptuous as vol
import async_timeout import async_timeout
import aiohttp import aiohttp
from homeassistant.components.switch import SwitchDevice from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
HOOK_ENDPOINT = "https://api.gethook.io/v1/" HOOK_ENDPOINT = 'https://api.gethook.io/v1/'
TIMEOUT = 10 TIMEOUT = 10
SWITCH_SCHEMA = vol.Schema({ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_USERNAME): cv.string, vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string vol.Required(CONF_PASSWORD): cv.string,
}) })
@asyncio.coroutine @asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None): def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Setup Hook by getting the access token and list of actions.""" """Set up Hook by getting the access token and list of actions."""
username = config.get(CONF_USERNAME) username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD) password = config.get(CONF_PASSWORD)
try: try:
with async_timeout.timeout(TIMEOUT, loop=hass.loop): with async_timeout.timeout(TIMEOUT, loop=hass.loop):
response = yield from hass.websession.post( response = yield from hass.websession.post(
HOOK_ENDPOINT + 'user/login', '{}{}'.format(HOOK_ENDPOINT, 'user/login'),
data={ data={
'username': username, 'username': username,
'password': password}) 'password': password})
@ -57,7 +55,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
try: try:
with async_timeout.timeout(TIMEOUT, loop=hass.loop): with async_timeout.timeout(TIMEOUT, loop=hass.loop):
response = yield from hass.websession.get( response = yield from hass.websession.get(
HOOK_ENDPOINT + 'device', '{}{}'.format(HOOK_ENDPOINT, 'device'),
params={"token": data['data']['token']}) params={"token": data['data']['token']})
data = yield from response.json() data = yield from response.json()
except (asyncio.TimeoutError, except (asyncio.TimeoutError,
@ -79,7 +77,6 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
class HookSmartHome(SwitchDevice): class HookSmartHome(SwitchDevice):
"""Representation of a Hook device, allowing on and off commands.""" """Representation of a Hook device, allowing on and off commands."""
# pylint: disable=too-many-arguments
def __init__(self, hass, token, device_id, device_name): def __init__(self, hass, token, device_id, device_name):
"""Initialize the switch.""" """Initialize the switch."""
self._hass = hass self._hass = hass
@ -88,8 +85,7 @@ class HookSmartHome(SwitchDevice):
self._id = device_id self._id = device_id
self._name = device_name self._name = device_name
_LOGGER.debug( _LOGGER.debug(
"Creating Hook object: ID: " + self._id + "Creating Hook object: ID: %s Name: %s", self._id, self._name)
" Name: " + self._name)
@property @property
def name(self): def name(self):
@ -108,8 +104,7 @@ class HookSmartHome(SwitchDevice):
_LOGGER.debug("Sending: %s", url) _LOGGER.debug("Sending: %s", url)
with async_timeout.timeout(TIMEOUT, loop=self._hass.loop): with async_timeout.timeout(TIMEOUT, loop=self._hass.loop):
response = yield from self._hass.websession.get( response = yield from self._hass.websession.get(
url, url, params={"token": self._token})
params={"token": self._token})
data = yield from response.json() data = yield from response.json()
except (asyncio.TimeoutError, except (asyncio.TimeoutError,
aiohttp.errors.ClientError, aiohttp.errors.ClientError,
@ -123,15 +118,17 @@ class HookSmartHome(SwitchDevice):
def async_turn_on(self): def async_turn_on(self):
"""Turn the device on asynchronously.""" """Turn the device on asynchronously."""
_LOGGER.debug("Turning on: %s", self._name) _LOGGER.debug("Turning on: %s", self._name)
success = yield from self._send( url = '{}{}{}{}'.format(
HOOK_ENDPOINT + 'device/trigger/' + self._id + '/On') HOOK_ENDPOINT, 'device/trigger/', self._id, '/On')
success = yield from self._send(url)
self._state = success self._state = success
@asyncio.coroutine @asyncio.coroutine
def async_turn_off(self): def async_turn_off(self):
"""Turn the device off asynchronously.""" """Turn the device off asynchronously."""
_LOGGER.debug("Turning off: %s", self._name) _LOGGER.debug("Turning off: %s", self._name)
success = yield from self._send( url = '{}{}{}{}'.format(
HOOK_ENDPOINT + 'device/trigger/' + self._id + '/Off') HOOK_ENDPOINT, 'device/trigger/', self._id, '/Off')
success = yield from self._send(url)
# If it wasn't successful, keep state as true # If it wasn't successful, keep state as true
self._state = not success self._state = not success