angel12

Untitled

Nov 6th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.05 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.switch import SwitchDevice
  13. from homeassistant.util import convert
  14. from homeassistant.components.light import (
  15. Light, ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_RGB_COLOR, ATTR_TRANSITION,
  16. ATTR_XY_COLOR, SUPPORT_BRIGHTNESS, SUPPORT_COLOR_TEMP, SUPPORT_RGB_COLOR,
  17. SUPPORT_TRANSITION, SUPPORT_XY_COLOR)
  18. from homeassistant.const import (
  19. STATE_OFF, STATE_ON, STATE_STANDBY, STATE_UNKNOWN)
  20. from homeassistant.loader import get_component
  21.  
  22. DEPENDENCIES = ['wemo']
  23.  
  24. MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
  25. MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(milliseconds=100)
  26.  
  27. _LOGGER = logging.getLogger(__name__)
  28.  
  29. SUPPORT_WEMO = (SUPPORT_BRIGHTNESS | SUPPORT_COLOR_TEMP | SUPPORT_RGB_COLOR |
  30. SUPPORT_TRANSITION | SUPPORT_XY_COLOR)
  31.  
  32. WEMO_ON = 1
  33. WEMO_OFF = 0
  34. WEMO_STANDBY = 8
  35.  
  36. def to_wemo_level(level):
  37. """Convert the given HASS light level (0-255) to wemo (0.0-100.0)."""
  38. return float((level * 100) / 255)
  39.  
  40.  
  41. def to_hass_level(level):
  42. """Convert the given wemo(0.0-100.0) light level to HASS (0-255)."""
  43. return int((level * 255) / 100)
  44.  
  45. # pylint: disable=unused-argument, too-many-function-args
  46. def setup_platform(hass, config, add_devices_callback, discovery_info=None):
  47. """Set up discovered WeMo switches."""
  48. import pywemo.discovery as discovery
  49.  
  50. if discovery_info is not None:
  51. location = discovery_info['ssdp_description']
  52. mac = discovery_info['mac_address']
  53. device = discovery.device_from_description(location, mac)
  54.  
  55. if device.model_name == 'Dimmer':
  56. add_devices_callback([WemoDimmer(device)])
  57. else:
  58. setup_bridge(device, add_devices)
  59.  
  60. def setup_bridge(bridge, add_devices):
  61. """Set up a WeMo link."""
  62. lights = {}
  63.  
  64. @util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
  65. def update_lights():
  66. """Update the WeMo led objects with latest info from the bridge."""
  67. bridge.bridge_update()
  68.  
  69. new_lights = []
  70.  
  71. for light_id, device in bridge.Lights.items():
  72. if light_id not in lights:
  73. lights[light_id] = WemoLight(device, update_lights)
  74. new_lights.append(lights[light_id])
  75.  
  76. if new_lights:
  77. add_devices(new_lights)
  78.  
  79. update_lights()
  80.  
  81.  
  82. class WemoLight(Light):
  83. """Representation of a WeMo light."""
  84.  
  85. def __init__(self, device, update_lights):
  86. """Initialize the WeMo light."""
  87. self.light_id = device.name
  88. self.device = device
  89. self.update_lights = update_lights
  90.  
  91. @property
  92. def unique_id(self):
  93. """Return the ID of this light."""
  94. deviceid = self.device.uniqueID
  95. return '{}.{}'.format(self.__class__, deviceid)
  96.  
  97. @property
  98. def name(self):
  99. """Return the name of the light."""
  100. return self.device.name
  101.  
  102. @property
  103. def brightness(self):
  104. """Return the brightness of this light between 0..255."""
  105. return self.device.state.get('level', 255)
  106.  
  107. @property
  108. def xy_color(self):
  109. """Return the XY color values of this light."""
  110. return self.device.state.get('color_xy')
  111.  
  112. @property
  113. def color_temp(self):
  114. """Return the color temperature of this light in mireds."""
  115. return self.device.state.get('temperature_mireds')
  116.  
  117. @property
  118. def is_on(self):
  119. """Return true if device is on."""
  120. return self.device.state['onoff'] != 0
  121.  
  122. @property
  123. def supported_features(self):
  124. """Flag supported features."""
  125. return SUPPORT_WEMO
  126.  
  127. def turn_on(self, **kwargs):
  128. """Turn the light on."""
  129. transitiontime = int(kwargs.get(ATTR_TRANSITION, 0))
  130.  
  131. if ATTR_XY_COLOR in kwargs:
  132. xycolor = kwargs[ATTR_XY_COLOR]
  133. elif ATTR_RGB_COLOR in kwargs:
  134. xycolor = color_util.color_RGB_to_xy(
  135. *(int(val) for val in kwargs[ATTR_RGB_COLOR]))
  136. kwargs.setdefault(ATTR_BRIGHTNESS, xycolor[2])
  137. else:
  138. xycolor = None
  139.  
  140. if xycolor is not None:
  141. self.device.set_color(xycolor, transition=transitiontime)
  142.  
  143. if ATTR_COLOR_TEMP in kwargs:
  144. colortemp = kwargs[ATTR_COLOR_TEMP]
  145. self.device.set_temperature(mireds=colortemp,
  146. transition=transitiontime)
  147.  
  148. if ATTR_BRIGHTNESS in kwargs:
  149. brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness or 255)
  150. self.device.turn_on(level=brightness, transition=transitiontime)
  151. else:
  152. self.device.turn_on(transition=transitiontime)
  153.  
  154. def turn_off(self, **kwargs):
  155. """Turn the light off."""
  156. transitiontime = int(kwargs.get(ATTR_TRANSITION, 0))
  157. self.device.turn_off(transition=transitiontime)
  158.  
  159. def update(self):
  160. """Synchronize state with bridge."""
  161. self.update_lights(no_throttle=True)
  162.  
  163. class WemoDimmer(Light):
  164. """Representation of a WeMo dimmer"""
  165.  
  166. def __init__(self, device):
  167. """Initialize the WeMo dimmer."""
  168. self.wemo = device
  169. self._brightness = None
  170. self._state = None
  171. # look up model name once as it incurs network traffic
  172. self._model_name = self.wemo.model_name
  173.  
  174. wemo = get_component('wemo')
  175. wemo.SUBSCRIPTION_REGISTRY.register(self.wemo)
  176. wemo.SUBSCRIPTION_REGISTRY.on(self.wemo, None, self._update_callback)
  177.  
  178. def _update_callback(self, _device, _type, _params):
  179. """Update the state by the Wemo device."""
  180. _LOGGER.info("Subscription update for %s", _device)
  181. updated = self.wemo.subscription_update(_type, _params)
  182. self._update(force_update=(not updated))
  183.  
  184. if not hasattr(self, 'hass'):
  185. return
  186. self.schedule_update_ha_state()
  187.  
  188. @property
  189. def unique_id(self):
  190. """Return the ID of this WeMo dimmer."""
  191. return "{}.{}".format(self.__class__, self.wemo.serialnumber)
  192.  
  193. @property
  194. def name(self):
  195. """Return the name of the dimmer if any."""
  196. return self.wemo.name
  197.  
  198. @property
  199. def supported_features(self):
  200. """Flag supported features."""
  201. return SUPPORT_BRIGHTNESS
  202.  
  203. @property
  204. def get_brightness(self):
  205. """Return the brightness of this light between 1 and 100"""
  206. brightness = self.wemo.get_brightness()
  207. return brightness
  208.  
  209.  
  210. @property
  211. def is_on(self):
  212. """Return true if dimmer is on. Standby is on."""
  213. return self._state
  214.  
  215. def update(self):
  216. """Update WeMo state."""
  217. self._update(force_update=True)
  218.  
  219. def _update(self, force_update=True):
  220. """Update the device state."""
  221. try:
  222. self._state = self.wemo.get_state(force_update)
  223. self._brightness = self.wemo.get_brightness(force_update)
  224. except AttributeError as err:
  225. _LOGGER.warning("Could not update status for %s (%s)",
  226. self.name, err)
  227.  
  228. def turn_on(self, **kwargs):
  229. """Turn the dimmer on."""
  230. self._state = WEMO_ON
  231. self.wemo.on()
  232. transitiontime = int(kwargs.get(ATTR_TRANSITION, 0))
  233.  
  234. # Wemo dimmer switches use a range of [0, 99] to control
  235. # brightness. Level 255 might mean to set it to previous value
  236. if ATTR_BRIGHTNESS in kwargs:
  237. self._brightness = kwargs[ATTR_BRIGHTNESS]
  238. brightness = int((self._brightness / 255) * 99)
  239. else:
  240. brightness = 255
  241. self.wemo.set_brightness(brightness)
  242.  
  243. def turn_off(self, **kwargs):
  244. """Turn the dimmer off."""
  245. self._state = WEMO_OFF
  246. self.wemo.off()
  247. self.schedule_update_ha_state()
Advertisement
Add Comment
Please, Sign In to add comment