Merge branch 'component-wink' into dev
* component-wink: Wink component bug fixes Bugfixes to make Wink component work Added wink component to auto-load other needed components Initial version wink component by @loghound changed bearer_token to access_token in conf file Wink Hub integration of it's switches & lights Conflicts: homeassistant/components/switch/__init__.py homeassistant/const.py
This commit is contained in:
commit
86e4fb9655
11 files changed
+520
-52
No files matched your search
@@ -12,7 +12,7 @@ from homeassistant.const import (
|
||||
STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, ATTR_ENTITY_ID)
|
||||
from homeassistant.helpers import (
|
||||
extract_entity_ids, platform_devices_from_config)
|
||||
from homeassistant.components import group, discovery
|
||||
from homeassistant.components import group, discovery, wink
|
||||
|
||||
DOMAIN = 'switch'
|
||||
DEPENDENCIES = []
|
||||
@@ -29,8 +29,9 @@ ATTR_CURRENT_POWER_MWH = "current_power_mwh"
|
||||
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
||||
|
||||
# Maps discovered services to their platforms
|
||||
DISCOVERY = {
|
||||
discovery.services.BELKIN_WEMO: 'wemo'
|
||||
DISCOVERY_PLATFORMS = {
|
||||
discovery.services.BELKIN_WEMO: 'wemo',
|
||||
wink.DISCOVER_SWITCHES: 'wink',
|
||||
}
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -82,22 +83,24 @@ def setup(hass, config):
|
||||
|
||||
def switch_discovered(service, info):
|
||||
""" Called when a switch is discovered. """
|
||||
platform = get_component("{}.{}".format(DOMAIN, DISCOVERY[service]))
|
||||
platform = get_component("{}.{}".format(
|
||||
DOMAIN, DISCOVERY_PLATFORMS[service]))
|
||||
|
||||
switch = platform.device_discovered(hass, config, info)
|
||||
discovered = platform.devices_discovered(hass, config, info)
|
||||
|
||||
if switch is not None and switch not in switches.values():
|
||||
switch.entity_id = util.ensure_unique_string(
|
||||
ENTITY_ID_FORMAT.format(util.slugify(switch.name)),
|
||||
switches.keys())
|
||||
for switch in discovered:
|
||||
if switch is not None and switch not in switches.values():
|
||||
switch.entity_id = util.ensure_unique_string(
|
||||
ENTITY_ID_FORMAT.format(util.slugify(switch.name)),
|
||||
switches.keys())
|
||||
|
||||
switches[switch.entity_id] = switch
|
||||
switches[switch.entity_id] = switch
|
||||
|
||||
switch.update_ha_state(hass)
|
||||
switch.update_ha_state(hass)
|
||||
|
||||
switch_group.update_tracked_entity_ids(switches.keys())
|
||||
switch_group.update_tracked_entity_ids(switches.keys())
|
||||
|
||||
discovery.listen(hass, discovery.services.BELKIN_WEMO, switch_discovered)
|
||||
discovery.listen(hass, DISCOVERY_PLATFORMS.keys(), switch_discovered)
|
||||
|
||||
def handle_switch_service(service):
|
||||
""" Handles calls to the switch services. """
|
||||
|
||||
@@ -24,16 +24,16 @@ def get_devices(hass, config):
|
||||
if isinstance(switch, pywemo.Switch)]
|
||||
|
||||
|
||||
def device_discovered(hass, config, info):
|
||||
def devices_discovered(hass, config, info):
|
||||
""" Called when a device is discovered. """
|
||||
_, discovery = get_pywemo()
|
||||
|
||||
if discovery is None:
|
||||
return
|
||||
return []
|
||||
|
||||
device = discovery.device_from_description(info)
|
||||
|
||||
return None if device is None else WemoSwitch(device)
|
||||
return [] if device is None else [WemoSwitch(device)]
|
||||
|
||||
|
||||
def get_pywemo():
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
""" Support for WeMo switchces. """
|
||||
import logging
|
||||
|
||||
# pylint: disable=no-name-in-module, import-error
|
||||
import homeassistant.external.wink.pywink as pywink
|
||||
|
||||
from homeassistant.helpers import ToggleDevice
|
||||
from homeassistant.const import ATTR_FRIENDLY_NAME, CONF_ACCESS_TOKEN
|
||||
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def get_devices(hass, config):
|
||||
""" Find and return Wink switches. """
|
||||
token = config.get(CONF_ACCESS_TOKEN)
|
||||
|
||||
if token is None:
|
||||
logging.getLogger(__name__).error(
|
||||
"Missing wink access_token - "
|
||||
"get one at https://winkbearertoken.appspot.com/")
|
||||
return []
|
||||
|
||||
pywink.set_bearer_token(token)
|
||||
|
||||
return get_switches()
|
||||
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def devices_discovered(hass, config, info):
|
||||
""" Called when a device is discovered. """
|
||||
return get_switches()
|
||||
|
||||
|
||||
def get_switches():
|
||||
""" Returns the Wink switches. """
|
||||
return [WinkSwitch(switch) for switch in pywink.get_switches()]
|
||||
|
||||
|
||||
class WinkSwitch(ToggleDevice):
|
||||
""" represents a WeMo switch within home assistant. """
|
||||
|
||||
def __init__(self, wink):
|
||||
self.wink = wink
|
||||
self.state_attr = {ATTR_FRIENDLY_NAME: wink.name()}
|
||||
|
||||
def get_name(self):
|
||||
""" Returns the name of the switch if any. """
|
||||
return self.wink.name()
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
""" Turns the switch on. """
|
||||
self.wink.setState(True)
|
||||
|
||||
def turn_off(self):
|
||||
""" Turns the switch off. """
|
||||
self.wink.setState(False)
|
||||
|
||||
def is_on(self):
|
||||
""" True if switch is on. """
|
||||
return self.wink.state()
|
||||
|
||||
def get_state_attributes(self):
|
||||
""" Returns optional state attributes. """
|
||||
return self.state_attr
|
||||
Reference in new issue
Block a user