Wemo Dimmer Support ()

* Wemo Dimmer Support

Add support for the Wemo Dimmer Switch

* Add newline at end of file

Re: findings from hound

* Syntax for the hound

Sorry for the excess edits, new to python

* Change order of Models

Fixed order back to ABC order

* Changes as requested

I made the changes I was comfortable with at this point, the dimmer addition now very closely mirrors what is under the switch component, at least as far as the parts necessary for the dimmer.

Any changes past these with regards to the subscription registry / callback info is probably going to be over my head, but I will try to look deeper at them if required.

* Remove unnecessary lines

Removed self.schedule_update_ha_state() from turn off / turn on

* Remove update(self)

Removed update method

* Move subscription to async_added_to_hass

* Move subscription.
* Clean up.

* Wait until the job in the executor is done

* Run gen_requirements_all script

* Only update instance attributes via callback
pull/11680/head
angel12 2018-01-15 15:08:48 -07:00 committed by Fabian Affolter
parent fdcf332a8a
commit 799e1f0469
2 changed files with 95 additions and 5 deletions
homeassistant/components

View File

@ -4,6 +4,7 @@ Support for Belkin WeMo lights.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/light.wemo/
"""
import asyncio
import logging
from datetime import timedelta
@ -13,6 +14,7 @@ from homeassistant.components.light import (
Light, ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_RGB_COLOR, ATTR_TRANSITION,
ATTR_XY_COLOR, SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP, SUPPORT_RGB_COLOR,
SUPPORT_TRANSITION, SUPPORT_XY_COLOR)
from homeassistant.loader import get_component
DEPENDENCIES = ['wemo']
@ -26,7 +28,7 @@ SUPPORT_WEMO = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_RGB_COLOR |
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the WeMo bridges and register connected lights."""
"""Set up discovered WeMo switches."""
import pywemo.discovery as discovery
if discovery_info is not None:
@ -34,7 +36,9 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
mac = discovery_info['mac_address']
device = discovery.device_from_description(location, mac)
if device:
if device.model_name == 'Dimmer':
add_devices([WemoDimmer(device)])
else:
setup_bridge(device, add_devices)
@ -140,3 +144,88 @@ class WemoLight(Light):
def update(self):
"""Synchronize state with bridge."""
self.update_lights(no_throttle=True)
class WemoDimmer(Light):
"""Representation of a WeMo dimmer."""
def __init__(self, device):
"""Initialize the WeMo dimmer."""
self.wemo = device
self._brightness = None
self._state = None
@asyncio.coroutine
def async_added_to_hass(self):
"""Register update callback."""
wemo = get_component('wemo')
# The register method uses a threading condition, so call via executor.
# and yield from to wait until the task is done.
yield from self.hass.async_add_job(
wemo.SUBSCRIPTION_REGISTRY.register, self.wemo)
# The on method just appends to a defaultdict list.
wemo.SUBSCRIPTION_REGISTRY.on(self.wemo, None, self._update_callback)
def _update_callback(self, _device, _type, _params):
"""Update the state by the Wemo device."""
_LOGGER.debug("Subscription update for %s", _device)
updated = self.wemo.subscription_update(_type, _params)
self._update(force_update=(not updated))
self.schedule_update_ha_state()
@property
def unique_id(self):
"""Return the ID of this WeMo dimmer."""
return "{}.{}".format(self.__class__, self.wemo.serialnumber)
@property
def name(self):
"""Return the name of the dimmer if any."""
return self.wemo.name
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_BRIGHTNESS
@property
def should_poll(self):
"""No polling needed with subscriptions."""
return False
@property
def brightness(self):
"""Return the brightness of this light between 1 and 100."""
return self._brightness
@property
def is_on(self):
"""Return true if dimmer is on. Standby is on."""
return self._state
def _update(self, force_update=True):
"""Update the device state."""
try:
self._state = self.wemo.get_state(force_update)
wemobrightness = int(self.wemo.get_brightness(force_update))
self._brightness = int((wemobrightness * 255) / 100)
except AttributeError as err:
_LOGGER.warning("Could not update status for %s (%s)",
self.name, err)
def turn_on(self, **kwargs):
"""Turn the dimmer on."""
self.wemo.on()
# Wemo dimmer switches use a range of [0, 100] to control
# brightness. Level 255 might mean to set it to previous value
if ATTR_BRIGHTNESS in kwargs:
brightness = kwargs[ATTR_BRIGHTNESS]
brightness = int((brightness / 255) * 100)
else:
brightness = 255
self.wemo.set_brightness(brightness)
def turn_off(self, **kwargs):
"""Turn the dimmer off."""
self.wemo.off()

View File

@ -21,12 +21,13 @@ DOMAIN = 'wemo'
# Mapping from Wemo model_name to component.
WEMO_MODEL_DISPATCH = {
'Bridge': 'light',
'CoffeeMaker': 'switch',
'Dimmer': 'light',
'Insight': 'switch',
'LightSwitch': 'switch',
'Maker': 'switch',
'Sensor': 'binary_sensor',
'Socket': 'switch',
'LightSwitch': 'switch',
'CoffeeMaker': 'switch'
'Socket': 'switch'
}
SUBSCRIPTION_REGISTRY = None