Built-in component cleanup

This commit is contained in:
Paulus Schoutsen 2015-08-03 17:42:28 +02:00
parent 382c1de981
commit 4096a67251
3 changed files with 85 additions and 96 deletions

View File

@ -166,26 +166,25 @@ def setup(hass, config):
profiles = {} profiles = {}
for profile_path in profile_paths: for profile_path in profile_paths:
if not os.path.isfile(profile_path):
continue
with open(profile_path) as inp:
reader = csv.reader(inp)
if os.path.isfile(profile_path): # Skip the header
with open(profile_path) as inp: next(reader, None)
reader = csv.reader(inp)
# Skip the header try:
next(reader, None) for profile_id, color_x, color_y, brightness in reader:
profiles[profile_id] = (float(color_x), float(color_y),
int(brightness))
except ValueError:
# ValueError if not 4 values per row
# ValueError if convert to float/int failed
_LOGGER.error(
"Error parsing light profiles from %s", profile_path)
try: return False
for profile_id, color_x, color_y, brightness in reader:
profiles[profile_id] = (float(color_x), float(color_y),
int(brightness))
except ValueError:
# ValueError if not 4 values per row
# ValueError if convert to float/int failed
_LOGGER.error(
"Error parsing light profiles from %s", profile_path)
return False
def handle_light_service(service): def handle_light_service(service):
""" Hande a turn light on or off service call. """ """ Hande a turn light on or off service call. """
@ -206,66 +205,70 @@ def setup(hass, config):
for light in target_lights: for light in target_lights:
light.turn_off(**params) light.turn_off(**params)
else: if light.should_poll:
# Processing extra data for turn light on request for light in target_lights:
light.update_ha_state(True)
return
# We process the profile first so that we get the desired # Processing extra data for turn light on request
# behavior that extra service data attributes overwrite
# profile values
profile = profiles.get(dat.get(ATTR_PROFILE))
if profile: # We process the profile first so that we get the desired
*params[ATTR_XY_COLOR], params[ATTR_BRIGHTNESS] = profile # behavior that extra service data attributes overwrite
# profile values
profile = profiles.get(dat.get(ATTR_PROFILE))
if ATTR_BRIGHTNESS in dat: if profile:
# We pass in the old value as the default parameter if parsing *params[ATTR_XY_COLOR], params[ATTR_BRIGHTNESS] = profile
# of the new one goes wrong.
params[ATTR_BRIGHTNESS] = util.convert(
dat.get(ATTR_BRIGHTNESS), int, params.get(ATTR_BRIGHTNESS))
if ATTR_XY_COLOR in dat: if ATTR_BRIGHTNESS in dat:
try: # We pass in the old value as the default parameter if parsing
# xy_color should be a list containing 2 floats # of the new one goes wrong.
xycolor = dat.get(ATTR_XY_COLOR) params[ATTR_BRIGHTNESS] = util.convert(
dat.get(ATTR_BRIGHTNESS), int, params.get(ATTR_BRIGHTNESS))
# Without this check, a xycolor with value '99' would work if ATTR_XY_COLOR in dat:
if not isinstance(xycolor, str): try:
params[ATTR_XY_COLOR] = [float(val) for val in xycolor] # xy_color should be a list containing 2 floats
xycolor = dat.get(ATTR_XY_COLOR)
except (TypeError, ValueError): # Without this check, a xycolor with value '99' would work
# TypeError if xy_color is not iterable if not isinstance(xycolor, str):
# ValueError if value could not be converted to float params[ATTR_XY_COLOR] = [float(val) for val in xycolor]
pass
if ATTR_RGB_COLOR in dat: except (TypeError, ValueError):
try: # TypeError if xy_color is not iterable
# rgb_color should be a list containing 3 ints # ValueError if value could not be converted to float
rgb_color = dat.get(ATTR_RGB_COLOR) pass
if len(rgb_color) == 3: if ATTR_RGB_COLOR in dat:
params[ATTR_XY_COLOR] = \ try:
color_util.color_RGB_to_xy(int(rgb_color[0]), # rgb_color should be a list containing 3 ints
int(rgb_color[1]), rgb_color = dat.get(ATTR_RGB_COLOR)
int(rgb_color[2]))
except (TypeError, ValueError): if len(rgb_color) == 3:
# TypeError if rgb_color is not iterable params[ATTR_XY_COLOR] = \
# ValueError if not all values can be converted to int color_util.color_RGB_to_xy(int(rgb_color[0]),
pass int(rgb_color[1]),
int(rgb_color[2]))
if ATTR_FLASH in dat: except (TypeError, ValueError):
if dat[ATTR_FLASH] == FLASH_SHORT: # TypeError if rgb_color is not iterable
params[ATTR_FLASH] = FLASH_SHORT # ValueError if not all values can be converted to int
pass
elif dat[ATTR_FLASH] == FLASH_LONG: if ATTR_FLASH in dat:
params[ATTR_FLASH] = FLASH_LONG if dat[ATTR_FLASH] == FLASH_SHORT:
params[ATTR_FLASH] = FLASH_SHORT
if ATTR_EFFECT in dat: elif dat[ATTR_FLASH] == FLASH_LONG:
if dat[ATTR_EFFECT] == EFFECT_COLORLOOP: params[ATTR_FLASH] = FLASH_LONG
params[ATTR_EFFECT] = EFFECT_COLORLOOP
for light in target_lights: if ATTR_EFFECT in dat:
light.turn_on(**params) if dat[ATTR_EFFECT] == EFFECT_COLORLOOP:
params[ATTR_EFFECT] = EFFECT_COLORLOOP
for light in target_lights:
light.turn_on(**params)
for light in target_lights: for light in target_lights:
if light.should_poll: if light.should_poll:

View File

@ -98,9 +98,7 @@ ATTR_TO_PROPERTY = [
def is_on(hass, entity_id=None): def is_on(hass, entity_id=None):
""" Returns true if specified media player entity_id is on. """ Returns true if specified media player entity_id is on.
Will check all media player if no entity_id specified. """ Will check all media player if no entity_id specified. """
entity_ids = [entity_id] if entity_id else hass.states.entity_ids(DOMAIN) entity_ids = [entity_id] if entity_id else hass.states.entity_ids(DOMAIN)
return any(not hass.states.is_state(entity_id, STATE_OFF) return any(not hass.states.is_state(entity_id, STATE_OFF)
for entity_id in entity_ids) for entity_id in entity_ids)
@ -108,28 +106,24 @@ def is_on(hass, entity_id=None):
def turn_on(hass, entity_id=None): def turn_on(hass, entity_id=None):
""" Will turn on specified media player or all. """ """ Will turn on specified media player or all. """
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.services.call(DOMAIN, SERVICE_TURN_ON, data) hass.services.call(DOMAIN, SERVICE_TURN_ON, data)
def turn_off(hass, entity_id=None): def turn_off(hass, entity_id=None):
""" Will turn off specified media player or all. """ """ Will turn off specified media player or all. """
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.services.call(DOMAIN, SERVICE_TURN_OFF, data) hass.services.call(DOMAIN, SERVICE_TURN_OFF, data)
def volume_up(hass, entity_id=None): def volume_up(hass, entity_id=None):
""" Send the media player the command for volume up. """ """ Send the media player the command for volume up. """
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.services.call(DOMAIN, SERVICE_VOLUME_UP, data) hass.services.call(DOMAIN, SERVICE_VOLUME_UP, data)
def volume_down(hass, entity_id=None): def volume_down(hass, entity_id=None):
""" Send the media player the command for volume down. """ """ Send the media player the command for volume down. """
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.services.call(DOMAIN, SERVICE_VOLUME_DOWN, data) hass.services.call(DOMAIN, SERVICE_VOLUME_DOWN, data)
@ -156,35 +150,30 @@ def set_volume_level(hass, volume, entity_id=None):
def media_play_pause(hass, entity_id=None): def media_play_pause(hass, entity_id=None):
""" Send the media player the command for play/pause. """ """ Send the media player the command for play/pause. """
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.services.call(DOMAIN, SERVICE_MEDIA_PLAY_PAUSE, data) hass.services.call(DOMAIN, SERVICE_MEDIA_PLAY_PAUSE, data)
def media_play(hass, entity_id=None): def media_play(hass, entity_id=None):
""" Send the media player the command for play/pause. """ """ Send the media player the command for play/pause. """
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.services.call(DOMAIN, SERVICE_MEDIA_PLAY, data) hass.services.call(DOMAIN, SERVICE_MEDIA_PLAY, data)
def media_pause(hass, entity_id=None): def media_pause(hass, entity_id=None):
""" Send the media player the command for play/pause. """ """ Send the media player the command for play/pause. """
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.services.call(DOMAIN, SERVICE_MEDIA_PAUSE, data) hass.services.call(DOMAIN, SERVICE_MEDIA_PAUSE, data)
def media_next_track(hass, entity_id=None): def media_next_track(hass, entity_id=None):
""" Send the media player the command for next track. """ """ Send the media player the command for next track. """
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.services.call(DOMAIN, SERVICE_MEDIA_NEXT_TRACK, data) hass.services.call(DOMAIN, SERVICE_MEDIA_NEXT_TRACK, data)
def media_previous_track(hass, entity_id=None): def media_previous_track(hass, entity_id=None):
""" Send the media player the command for prev track. """ """ Send the media player the command for prev track. """
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.services.call(DOMAIN, SERVICE_MEDIA_PREVIOUS_TRACK, data) hass.services.call(DOMAIN, SERVICE_MEDIA_PREVIOUS_TRACK, data)
@ -262,29 +251,30 @@ def setup(hass, config):
hass.services.register(DOMAIN, SERVICE_MEDIA_SEEK, media_seek_service) hass.services.register(DOMAIN, SERVICE_MEDIA_SEEK, media_seek_service)
def play_youtube_video_service(service, media_id): def play_youtube_video_service(service, media_id=None):
""" Plays specified media_id on the media player. """ """ Plays specified media_id on the media player. """
target_players = component.extract_from_service(service) if media_id is None:
service.data.get('video')
if media_id: if media_id is None:
for player in target_players: return
player.play_youtube(media_id)
if player.should_poll: for player in component.extract_from_service(service):
player.update_ha_state(True) player.play_youtube(media_id)
hass.services.register(DOMAIN, "start_fireplace", if player.should_poll:
lambda service: player.update_ha_state(True)
play_youtube_video_service(service, "eyU3bRy2x44"))
hass.services.register(DOMAIN, "start_epic_sax", hass.services.register(
lambda service: DOMAIN, "start_fireplace",
play_youtube_video_service(service, "kxopViU98Xo")) lambda service: play_youtube_video_service(service, "eyU3bRy2x44"))
hass.services.register(DOMAIN, SERVICE_YOUTUBE_VIDEO, hass.services.register(
lambda service: DOMAIN, "start_epic_sax",
play_youtube_video_service( lambda service: play_youtube_video_service(service, "kxopViU98Xo"))
service, service.data.get('video')))
hass.services.register(
DOMAIN, SERVICE_YOUTUBE_VIDEO, play_youtube_video_service)
return True return True

View File

@ -45,21 +45,18 @@ _LOGGER = logging.getLogger(__name__)
def is_on(hass, entity_id=None): def is_on(hass, entity_id=None):
""" Returns if the switch is on based on the statemachine. """ """ Returns if the switch is on based on the statemachine. """
entity_id = entity_id or ENTITY_ID_ALL_SWITCHES entity_id = entity_id or ENTITY_ID_ALL_SWITCHES
return hass.states.is_state(entity_id, STATE_ON) return hass.states.is_state(entity_id, STATE_ON)
def turn_on(hass, entity_id=None): def turn_on(hass, entity_id=None):
""" Turns all or specified switch on. """ """ Turns all or specified switch on. """
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.services.call(DOMAIN, SERVICE_TURN_ON, data) hass.services.call(DOMAIN, SERVICE_TURN_ON, data)
def turn_off(hass, entity_id=None): def turn_off(hass, entity_id=None):
""" Turns all or specified switch off. """ """ Turns all or specified switch off. """
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.services.call(DOMAIN, SERVICE_TURN_OFF, data) hass.services.call(DOMAIN, SERVICE_TURN_OFF, data)
@ -84,7 +81,6 @@ def setup(hass, config):
switch.update_ha_state(True) switch.update_ha_state(True)
hass.services.register(DOMAIN, SERVICE_TURN_OFF, handle_switch_service) hass.services.register(DOMAIN, SERVICE_TURN_OFF, handle_switch_service)
hass.services.register(DOMAIN, SERVICE_TURN_ON, handle_switch_service) hass.services.register(DOMAIN, SERVICE_TURN_ON, handle_switch_service)
return True return True