homeassistant-config/pyscript/modules/ir_helpers.py

132 lines
4.3 KiB
Python
Raw Permalink Normal View History

2021-10-05 13:22:27 +02:00
# ---------------------------------- Music ---------------------------------------------
URLS = {
'klassik_radio': "http://opml.radiotime.com/Tune.ashx?id=s25028&formats=aac,ogg,mp3",
'spotify_rain': "spotify:playlist:37i9dQZF1DXbcPC6Vvqudd",
'spotify_merkliste': "spotify:playlist:43wCckw3P4WKdy06Rd7eYO",
}
async def media_player_s(service_name, entity_id, **kwargs):
await hass.services.async_call('media_player', service_name, {'entity_id': entity_id, **kwargs})
async def media_player_f(service_name, entity_id, **kwargs):
async def f():
hass.services.async_call('media_player', service_name, {'entity_id': entity_id, **kwargs})
return f
async def default_music_settings(player, shuffle=False):
await media_player_s('shuffle_set', player, shuffle=shuffle)
await media_player_s('squeezebox_call_method', player, command="playlist repeat 0")
await media_player_s('squeezebox_call_method', player, command="playerpref transitionType 0")
async def repeat_one_crossfade(player):
await media_player_s('repeat_set', player, repeat='one')
await media_player_s('squeezebox_call_method', player, command='playerpref transitionType 1')
def playlist_f(name, player, shuffle=False):
async def f():
await default_music_settings(player, shuffle=shuffle)
await media_player_s('play_media', player, media_content_id=name, media_content_type='music')
return f
def radio_f(name, player):
async def f():
await media_player_s('play_media', player, media_content_id=name, media_content_type='channel')
return f
def scene_f(name):
async def f():
await hass.services.async_call('scene', 'turn_on', {'entity_id': 'scene.' + name})
return f
def cover_f(name, entity_id, **kwargs):
async def f():
await hass.services.async_call('cover', name, {'entity_id': entity_id, **kwargs})
return f
def cover_half_f(entity_id):
async def f():
await hass.services.async_call('cover_half', 'set_half', {'entity_id': entity_id})
return f
def light_f(name, entity_id, **kwargs):
async def f():
await hass.services.async_call('light', name, {'entity_id': entity_id, **kwargs})
return f
2024-05-03 20:50:49 +02:00
def multiple_f(*args):
async def f():
for func in args:
await func()
return f
2021-10-05 13:22:27 +02:00
def default_ir_music_control(player):
return {
'key_play': media_player_f("media_play_pause", player),
'key_pause': media_player_f("turn_off", player),
'key_forward': media_player_f('media_next_track', player),
'key_fastforward': media_player_f('media_next_track', player),
'key_previous': media_player_f('media_previous_track', player),
'key_rewind': media_player_f('media_previous_track', player),
'key_volumeup': media_player_f('volume_up', player),
'key_volumedown': media_player_f('volume_down', player),
'key_menu': media_player_f('shuffle_set', player, shuffle=True),
'key_exit': media_player_f('shuffle_set', player, shuffle=False),
}
# ---------------------------------- Covers --------------------------------------------
def default_ir_cover_control(device_group):
return {
'key_up': cover_f('open_cover', device_group),
'key_down': cover_f('close_cover', device_group),
'key_left': cover_half_f(device_group),
'key_right': cover_half_f(device_group),
}
# ---------------------------------- Lights ---------------------------------------------
def default_ir_light_control(device_group):
def dim_offset_f(offset):
async def f():
await hass.services.async_call('dimmer', 'dim', {'entity_id': device_group, 'offset': offset})
return f
return {
'key_channelup': dim_offset_f(+30),
'key_channeldown': dim_offset_f(-30),
'key_menu': dim_offset_f(130),
'key_stop': dim_offset_f(-130),
'key_goto': light_f('turn_off', device_group)
}
class CyclicCounter:
def __init__(self, func, values):
self.idx = 0
self.values = values
self.func = func
def next(self):
self.idx = (self.idx + 1) % len(self.values)
self.func(self.values[self.idx])
def prev(self):
self.idx = (self.idx - 1) % len(self.values)
self.func(self.values[self.idx])