angel12

wemodimmer code

Oct 19th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.79 KB | None | 0 0
  1. """
  2. Support for Belkin WeMo lights.
  3.  
  4. For more details about this component, please refer to the documentation at
  5. https://home-assistant.io/components/light.wemo/
  6. """
  7. import logging
  8. from datetime import timedelta
  9.  
  10. import homeassistant.util as util
  11. import homeassistant.util.color as color_util
  12. from homeassistant.components.light import (
  13. Light, ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_RGB_COLOR, ATTR_TRANSITION,
  14. ATTR_XY_COLOR, SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP, SUPPORT_RGB_COLOR,
  15. SUPPORT_TRANSITION, SUPPORT_XY_COLOR)
  16.  
  17. DEPENDENCIES = ['wemo']
  18.  
  19. MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
  20. MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(milliseconds=100)
  21.  
  22. _LOGGER = logging.getLogger(__name__)
  23.  
  24. SUPPORT_WEMO = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_RGB_COLOR |
  25. SUPPORT_TRANSITION | SUPPORT_XY_COLOR)
  26.  
  27.  
  28. def setup_platform(hass, config, add_devices, add_devices_callback, discovery_info=None):
  29. """Set up the WeMo bridges and register connected lights."""
  30. import pywemo.discovery as discovery
  31.  
  32. if discovery_info is not None:
  33. location = discovery_info['ssdp_description']
  34. mac = discovery_info['mac_address']
  35. device = discovery.device_from_description(location, mac)
  36.  
  37. if device:
  38. setup_bridge(device, add_devices)
  39.  
  40. if device:
  41. add_devices_callback([WemoDimmer(device)])
  42.  
  43.  
  44. def setup_bridge(bridge, add_devices):
  45. """Set up a WeMo link."""
  46. lights = {}
  47.  
  48. @util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
  49. def update_lights():
  50. """Update the WeMo led objects with latest info from the bridge."""
  51. bridge.bridge_update()
  52.  
  53. new_lights = []
  54.  
  55. for light_id, device in bridge.Lights.items():
  56. if light_id not in lights:
  57. lights[light_id] = WemoLight(device, update_lights)
  58. new_lights.append(lights[light_id])
  59.  
  60. if new_lights:
  61. add_devices(new_lights)
  62.  
  63. update_lights()
  64.  
  65.  
  66. class WemoLight(Light):
  67. """Representation of a WeMo light."""
  68.  
  69. def __init__(self, device, update_lights):
  70. """Initialize the WeMo light."""
  71. self.light_id = device.name
  72. self.device = device
  73. self.update_lights = update_lights
  74.  
  75. @property
  76. def unique_id(self):
  77. """Return the ID of this light."""
  78. deviceid = self.device.uniqueID
  79. return '{}.{}'.format(self.__class__, deviceid)
  80.  
  81. @property
  82. def name(self):
  83. """Return the name of the light."""
  84. return self.device.name
  85.  
  86. @property
  87. def brightness(self):
  88. """Return the brightness of this light between 0..255."""
  89. return self.device.state.get('level', 255)
  90.  
  91. @property
  92. def xy_color(self):
  93. """Return the XY color values of this light."""
  94. return self.device.state.get('color_xy')
  95.  
  96. @property
  97. def color_temp(self):
  98. """Return the color temperature of this light in mireds."""
  99. return self.device.state.get('temperature_mireds')
  100.  
  101. @property
  102. def is_on(self):
  103. """Return true if device is on."""
  104. return self.device.state['onoff'] != 0
  105.  
  106. @property
  107. def supported_features(self):
  108. """Flag supported features."""
  109. return SUPPORT_WEMO
  110.  
  111. def turn_on(self, **kwargs):
  112. """Turn the light on."""
  113. transitiontime = int(kwargs.get(ATTR_TRANSITION, 0))
  114.  
  115. if ATTR_XY_COLOR in kwargs:
  116. xycolor = kwargs[ATTR_XY_COLOR]
  117. elif ATTR_RGB_COLOR in kwargs:
  118. xycolor = color_util.color_RGB_to_xy(
  119. *(int(val) for val in kwargs[ATTR_RGB_COLOR]))
  120. kwargs.setdefault(ATTR_BRIGHTNESS, xycolor[2])
  121. else:
  122. xycolor = None
  123.  
  124. if xycolor is not None:
  125. self.device.set_color(xycolor, transition=transitiontime)
  126.  
  127. if ATTR_COLOR_TEMP in kwargs:
  128. colortemp = kwargs[ATTR_COLOR_TEMP]
  129. self.device.set_temperature(mireds=colortemp,
  130. transition=transitiontime)
  131.  
  132. if ATTR_BRIGHTNESS in kwargs:
  133. brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness or 255)
  134. self.device.turn_on(level=brightness, transition=transitiontime)
  135. else:
  136. self.device.turn_on(transition=transitiontime)
  137.  
  138. def turn_off(self, **kwargs):
  139. """Turn the light off."""
  140. transitiontime = int(kwargs.get(ATTR_TRANSITION, 0))
  141. self.device.turn_off(transition=transitiontime)
  142.  
  143. def update(self):
  144. """Synchronize state with bridge."""
  145. self.update_lights(no_throttle=True)
  146.  
  147.  
  148. class WemoDimmer(light)
  149. ""Representation of a WeMo dimmer""
  150.  
  151. def __init__(self, device):
  152. """Initialize the WeMo dimmer."""
  153. self.wemo = device
  154. self._brightness = None
  155. self._state = None
  156. # look up model name once as it incurs network traffic
  157. self._model_name = self.wemo.model_name
  158.  
  159. wemo = get_component('wemo')
  160. wemo.SUBSCRIPTION_REGISTRY.register(self.wemo)
  161. wemo.SUBSCRIPTION_REGISTRY.on(self.wemo, None, self._update_callback)
  162.  
  163. def _update_callback(self, _device, _type, _params):
  164. """Update the state by the Wemo device."""
  165. _LOGGER.info("Subscription update for %s", _device)
  166. updated = self.wemo.subscription_update(_type, _params)
  167. self._update(force_update=(not updated))
  168.  
  169. if not hasattr(self, 'hass'):
  170. return
  171. self.schedule_update_ha_state()
  172.  
  173. @property
  174. def unique_id(self):
  175. """Return the ID of this WeMo dimmer."""
  176. return "{}.{}".format(self.__class__, self.wemo.serialnumber)
  177.  
  178. @property
  179. def name(self):
  180. """Return the name of the dimmer if any."""
  181. return self.wemo.name
  182.  
  183. @property
  184. def supported_features(self):
  185. """Flag supported features."""
  186. return (SUPPORT_BRIGHTNESS_PCT | SUPPORT_TRANSITION | SUPPORT_EFFECT)
  187.  
  188. @property
  189. def is_on(self):
  190. """Return true if dimmer is on. Standby is on."""
  191. return self._state
  192.  
  193. def turn_on(self, **kwargs):
  194. """Turn the dimmer on."""
  195. self._state = WEMO_ON
  196. transitiontime = int(kwargs.get(ATTR_TRANSITION, 0))
  197.  
  198. # Wemo dimmer switches use a range of [0, 99] to control
  199. # brightness. Level 255 might mean to set it to previous value
  200. if ATTR_BRIGHTNESS in kwargs:
  201. self._brightness = kwargs[ATTR_BRIGHTNESS]
  202. brightness = int((self._brightness / 255) * 99)
  203. else:
  204. brightness = 255
  205.  
  206. def turn_off(self, **kwargs):
  207. """Turn the dimmer off."""
  208. self._state = WEMO_OFF
  209. self.wemo.off()
  210. self.schedule_update_ha_state()
Advertisement
Add Comment
Please, Sign In to add comment