2015-01-11 07:47:23 +00:00
|
|
|
"""
|
2015-05-13 17:18:30 +00:00
|
|
|
homeassistant.components.wink
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2015-01-11 07:47:23 +00:00
|
|
|
Connects to a Wink hub and loads relevant components to control its devices.
|
2015-09-09 07:37:45 +00:00
|
|
|
|
2015-10-21 08:44:29 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/wink.html
|
2015-01-11 07:47:23 +00:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant import bootstrap
|
|
|
|
from homeassistant.loader import get_component
|
2015-03-22 02:16:13 +00:00
|
|
|
from homeassistant.helpers import validate_config
|
|
|
|
from homeassistant.helpers.entity import ToggleEntity
|
2015-01-11 07:47:23 +00:00
|
|
|
from homeassistant.const import (
|
2015-01-16 05:25:24 +00:00
|
|
|
EVENT_PLATFORM_DISCOVERED, CONF_ACCESS_TOKEN,
|
|
|
|
ATTR_SERVICE, ATTR_DISCOVERED, ATTR_FRIENDLY_NAME)
|
2015-01-11 07:47:23 +00:00
|
|
|
|
|
|
|
DOMAIN = "wink"
|
|
|
|
DEPENDENCIES = []
|
2015-09-09 02:49:27 +00:00
|
|
|
REQUIREMENTS = ['https://github.com/balloob/python-wink/archive/'
|
|
|
|
'c2b700e8ca866159566ecf5e644d9c297f69f257.zip'
|
|
|
|
'#python-wink==0.1']
|
2015-01-11 07:47:23 +00:00
|
|
|
|
|
|
|
DISCOVER_LIGHTS = "wink.lights"
|
|
|
|
DISCOVER_SWITCHES = "wink.switches"
|
2015-01-20 01:16:04 +00:00
|
|
|
DISCOVER_SENSORS = "wink.sensors"
|
2015-01-11 07:47:23 +00:00
|
|
|
|
2015-01-20 04:23:31 +00:00
|
|
|
|
2015-01-11 07:47:23 +00:00
|
|
|
def setup(hass, config):
|
|
|
|
""" Sets up the Wink component. """
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
if not validate_config(config, {DOMAIN: [CONF_ACCESS_TOKEN]}, logger):
|
|
|
|
return False
|
|
|
|
|
2015-07-20 07:41:57 +00:00
|
|
|
import pywink
|
2015-01-11 07:47:23 +00:00
|
|
|
pywink.set_bearer_token(config[DOMAIN][CONF_ACCESS_TOKEN])
|
|
|
|
|
|
|
|
# Load components for the devices in the Wink that we support
|
|
|
|
for component_name, func_exists, discovery_type in (
|
|
|
|
('light', pywink.get_bulbs, DISCOVER_LIGHTS),
|
2015-01-20 01:16:04 +00:00
|
|
|
('switch', pywink.get_switches, DISCOVER_SWITCHES),
|
|
|
|
('sensor', pywink.get_sensors, DISCOVER_SENSORS)):
|
2015-01-11 07:47:23 +00:00
|
|
|
|
|
|
|
if func_exists():
|
2015-01-11 22:21:44 +00:00
|
|
|
component = get_component(component_name)
|
|
|
|
|
2015-01-11 07:47:23 +00:00
|
|
|
# Ensure component is loaded
|
2015-03-22 05:02:47 +00:00
|
|
|
bootstrap.setup_component(hass, component.DOMAIN, config)
|
2015-01-11 07:47:23 +00:00
|
|
|
|
|
|
|
# Fire discovery event
|
2015-01-12 16:21:50 +00:00
|
|
|
hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {
|
2015-01-11 07:47:23 +00:00
|
|
|
ATTR_SERVICE: discovery_type,
|
|
|
|
ATTR_DISCOVERED: {}
|
|
|
|
})
|
2015-01-11 22:21:44 +00:00
|
|
|
|
|
|
|
return True
|
2015-01-16 05:25:24 +00:00
|
|
|
|
2015-01-20 04:23:31 +00:00
|
|
|
|
2015-03-22 02:16:13 +00:00
|
|
|
class WinkToggleDevice(ToggleEntity):
|
2015-05-13 17:18:30 +00:00
|
|
|
""" Represents a Wink switch within Home Assistant. """
|
2015-01-16 05:25:24 +00:00
|
|
|
|
|
|
|
def __init__(self, wink):
|
|
|
|
self.wink = wink
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
2015-05-13 17:18:30 +00:00
|
|
|
""" Returns the id of this Wink switch. """
|
2015-01-16 05:25:24 +00:00
|
|
|
return "{}.{}".format(self.__class__, self.wink.deviceId())
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
""" Returns the name of the light if any. """
|
|
|
|
return self.wink.name()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
""" True if light is on. """
|
|
|
|
return self.wink.state()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
|
|
|
""" Returns optional state attributes. """
|
|
|
|
return {
|
|
|
|
ATTR_FRIENDLY_NAME: 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 update(self):
|
|
|
|
""" Update state of the light. """
|
2015-06-16 07:21:30 +00:00
|
|
|
self.wink.updateState()
|