Move WeMo discovery into a common component
This commit is contained in:
1 parent
4dad40fffb
commit
afe564fb3f
8 files changed
+147
-102
No files matched your search
@@ -11,7 +11,7 @@ import os
|
||||
import csv
|
||||
|
||||
from homeassistant.components import (
|
||||
group, discovery, wink, isy994, zwave, insteon_hub, mysensors)
|
||||
group, discovery, wemo, wink, isy994, zwave, insteon_hub, mysensors)
|
||||
from homeassistant.config import load_yaml_config_file
|
||||
from homeassistant.const import (
|
||||
STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE,
|
||||
@@ -59,6 +59,7 @@ LIGHT_PROFILES_FILE = "light_profiles.csv"
|
||||
|
||||
# Maps discovered services to their platforms
|
||||
DISCOVERY_PLATFORMS = {
|
||||
wemo.DISCOVER_LIGHTS: 'wemo',
|
||||
wink.DISCOVER_LIGHTS: 'wink',
|
||||
insteon_hub.DISCOVER_LIGHTS: 'insteon_hub',
|
||||
isy994.DISCOVER_LIGHTS: 'isy994',
|
||||
|
||||
@@ -13,7 +13,8 @@ import homeassistant.util as util
|
||||
from homeassistant.components.light import (
|
||||
Light, ATTR_BRIGHTNESS)
|
||||
|
||||
# REQUIREMENTS = ['pywemo==0.3.12']
|
||||
DEPENDENCIES = ['wemo']
|
||||
|
||||
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
||||
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(milliseconds=100)
|
||||
|
||||
@@ -21,8 +22,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
""" Find WeMo bridges and return connected lights. """
|
||||
import pywemo
|
||||
"""Probe WeMo bridges and register connected lights."""
|
||||
import pywemo.discovery as discovery
|
||||
|
||||
if discovery_info is not None:
|
||||
@@ -30,28 +30,17 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
||||
mac = discovery_info[3]
|
||||
device = discovery.device_from_description(location, mac)
|
||||
|
||||
if device and isinstance(device, pywemo.Bridge):
|
||||
setup_bridge(device, add_devices_callback)
|
||||
return
|
||||
|
||||
_LOGGER.info("Scanning for WeMo devices.")
|
||||
devices = pywemo.discover_devices()
|
||||
|
||||
# Filter out the bridges
|
||||
for device in devices:
|
||||
if isinstance(device, pywemo.Bridge):
|
||||
_LOGGER.info("Found bridge %s.", device)
|
||||
if device:
|
||||
setup_bridge(device, add_devices_callback)
|
||||
|
||||
|
||||
def setup_bridge(bridge, add_devices_callback):
|
||||
""" Setup a WeMo link. """
|
||||
|
||||
"""Setup a WeMo link."""
|
||||
lights = {}
|
||||
|
||||
@util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
|
||||
def update_lights():
|
||||
""" Updates the WeMo led objects with latest info from the bridge. """
|
||||
"""Updates the WeMo led objects with latest info from the bridge."""
|
||||
|
||||
bridge.bridge_get_lights()
|
||||
|
||||
@@ -59,8 +48,8 @@ def setup_bridge(bridge, add_devices_callback):
|
||||
|
||||
for light_id, info in bridge.Lights.items():
|
||||
if light_id not in lights:
|
||||
lights[light_id] = WemoLight(light_id, info,
|
||||
bridge, update_lights)
|
||||
lights[light_id] = WemoLight(bridge, light_id, info,
|
||||
update_lights)
|
||||
new_lights.append(lights[light_id])
|
||||
else:
|
||||
lights[light_id].info = info
|
||||
@@ -72,46 +61,46 @@ def setup_bridge(bridge, add_devices_callback):
|
||||
|
||||
|
||||
class WemoLight(Light):
|
||||
""" Represents a WeMo light """
|
||||
"""Represents a WeMo light"""
|
||||
|
||||
def __init__(self, light_id, info, bridge, update_lights):
|
||||
def __init__(self, bridge, light_id, info, update_lights):
|
||||
self.bridge = bridge
|
||||
self.light_id = light_id
|
||||
self.info = info
|
||||
self.bridge = bridge
|
||||
self.update_lights = update_lights
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
""" Returns the id of this light """
|
||||
"""Returns the id of this light"""
|
||||
deviceid = self.bridge.light_get_id(self.info)
|
||||
return "{}.{}".format(self.__class__, deviceid)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
""" Get the name of the light. """
|
||||
"""Get the name of the light."""
|
||||
return self.bridge.light_name(self.info)
|
||||
|
||||
@property
|
||||
def brightness(self):
|
||||
""" Brightness of this light between 0..255. """
|
||||
"""Brightness of this light between 0..255."""
|
||||
state = self.bridge.light_get_state(self.info)
|
||||
return int(state['dim'])
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
""" True if device is on. """
|
||||
"""True if device is on."""
|
||||
state = self.bridge.light_get_state(self.info)
|
||||
return int(state['state'])
|
||||
|
||||
def turn_on(self, **kwargs):
|
||||
""" Turn the light on. """
|
||||
"""Turn the light on."""
|
||||
dim = kwargs.get(ATTR_BRIGHTNESS, self.brightness)
|
||||
self.bridge.light_set_state(self.info, state=1, dim=dim)
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
""" Turn the light off. """
|
||||
"""Turn the light off."""
|
||||
self.bridge.light_set_state(self.info, state=0, dim=0)
|
||||
|
||||
def update(self):
|
||||
""" Synchronize state with bridge. """
|
||||
"""Synchronize state with bridge."""
|
||||
self.update_lights(no_throttle=True)
|
||||
Reference in new issue
Block a user