Fixes: flake8, missing import of exception and better handling of ble problems

This commit is contained in:
Sören Müller 2019-06-05 02:01:47 +02:00
parent 89b69cc6a1
commit bfa7e7d453

View file

@ -4,6 +4,7 @@ import logging
from homeassistant.components.light import ( from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_HS_COLOR, SUPPORT_BRIGHTNESS, ATTR_BRIGHTNESS, ATTR_HS_COLOR, SUPPORT_BRIGHTNESS,
SUPPORT_COLOR, Light) SUPPORT_COLOR, Light)
from homeassistant.exceptions import PlatformNotReady
import homeassistant.util.color as color_util import homeassistant.util.color as color_util
@ -20,7 +21,8 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
try: try:
for bulb in nearbyBulbs: for bulb in nearbyBulbs:
bulb.get_name() bulb.get_name()
except: bulb.get_brightness()
except Exception:
raise PlatformNotReady raise PlatformNotReady
add_devices(AveaLight(bulb) for bulb in nearbyBulbs) add_devices(AveaLight(bulb) for bulb in nearbyBulbs)
@ -34,7 +36,7 @@ class AveaLight(Light):
self._light = light self._light = light
self._name = light.name self._name = light.name
self._state = None self._state = None
self._brightness = None self._brightness = light.brightness
@property @property
def supported_features(self): def supported_features(self):
@ -74,7 +76,7 @@ class AveaLight(Light):
self._light.set_brightness(bright) self._light.set_brightness(bright)
if ATTR_HS_COLOR in kwargs: if ATTR_HS_COLOR in kwargs:
rgb = color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR]) rgb = color_util.color_hs_to_RGB(*kwargs[ATTR_HS_COLOR])
self._light.set_rgb(rgb[0],rgb[1],rgb[2]) self._light.set_rgb(rgb[0], rgb[1], rgb[2])
def turn_off(self, **kwargs): def turn_off(self, **kwargs):
"""Instruct the light to turn off.""" """Instruct the light to turn off."""
@ -86,9 +88,10 @@ class AveaLight(Light):
This is the only method that should fetch new data for Home Assistant. This is the only method that should fetch new data for Home Assistant.
""" """
brightness = self._light.get_brightness() brightness = self._light.get_brightness()
if brightness == 0: if brightness is not None:
self._state = False if brightness == 0:
else: self._state = False
self._state = True else:
bright_percent = round((brightness/4095)*100) self._state = True
self._brightness = round(255 * (bright_percent / 100)) bright_percent = round((brightness/4095)*100)
self._brightness = round(255 * (bright_percent / 100))