/
emby.yaml
111 lines (103 loc) · 3.91 KB
/
emby.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#
# Emby
#
# The purpose of this package is to allow a random TV show to start
# after an NFC tag is scanned.
#
# This package has largely been inspired by:
# - https://community.home-assistant.io/t/thought-id-share-my-nfc-tag-emby-media-player-setup/282127
# - How to get a dynamic input_select: https://community.home-assistant.io/t/fill-input-select/63865/43
# - https://community.home-assistant.io/t/how-to-process-large-json-over-255/67475
#
# - The Room detemines which Emby instance is enabled.
# - A list of shows to randomly tag
# - API access to Emby, needs to be added to secrets.
# - Check if the Roku is on, and Emby App is activated.
# - When an NFC Tag is scanned, select a random episode and play it.
#
# This is a hack, I wanted to use the base URL for the Rest commands, but
# secrets can't be used as variables. If this was a more important
# automation, I would conver this to a component.
input_text:
emby_base_url:
initial: !secret emby_base_url
media_player:
- platform: emby
host: !secret emby_host
api_key: !secret emby_key
sensor:
# Fetch all the episodes from the query, and then find a random episode.
- platform: rest
name: "Emby - Random Always Sunny Episode"
resource_template: "{{ states('input_text.emby_base_url') + '/emby/Shows/21775/Episodes' }}"
scan_interval: 4
value_template: "{{ value_json.Items[range(0,((value_json.TotalRecordCount | int(0)) -1 ))|random].Id}} "
headers:
X-Emby-Token: !secret emby_key
Content-Type: application/json
rest:
resource_template: "{{ states('input_text.emby_base_url') + '/emby/Sessions' }}"
scan_interval: 60
headers:
X-Emby-Token: !secret emby_key
Content-Type: application/json
sensor:
- name: Emby - Session Upstairs Living Room
value_template: >
{% set items = value_json | selectattr('DeviceName','eq', 'Roku Streaming Stick') | list | first %}
{{ items['Id'] }}
- name: Emby - Session Downstairs Living Room
value_template: >
{% set items = value_json | selectattr('DeviceName','eq', 'Downstairs Livingroom') | list | first %}
{{ items['Id'] }}
rest_command:
emby_start_show:
url: "{{ states('input_text.emby_base_url') }}/emby/Sessions/{{ states(session) }}/Playing?ItemIds={{ states(show) }}&PlayCommand=PlayNow"
method: POST
headers:
accept: "*/*"
X-Emby-Token: !secret emby_key
Content-Type: "application/json"
script:
emby_turn_on_livingroom:
sequence:
- type: turn_on
device_id: 0b3641ef9902529c6166b328fbdcfbe7
entity_id: media_player.roku_streaming_stick_2
domain: remote
mode: single
emby_open_livingroom:
sequence:
- service: media_player.select_source
data:
source: Emby
target:
entity_id: media_player.roku_streaming_stick_2
mode: single
automation:
- alias: Emby - NFC Tag Scanned
mode: single
trigger:
- platform: tag
tag_id: 6dca970e-7431-4082-be30-39768671197f
variables:
tags:
6dca970e-7431-4082-be30-39768671197f:
tv: Upstairs TV
turn_on_emby: script.emby_turn_on_livingroom
open_emby: script.emby_open_livingroom
media_player: media_player.roku_streaming_stick_2
action:
# Make sure Emby is open, and if it isn't, start it
- service_template: >
{% if (is_state( tags[trigger.event.data.tag_id].media_player , "off")) %}
{{ tags[trigger.event.data.tag_id].turn_on_emby }}
{% else %}
{{ tags[trigger.event.data.tag_id].open_emby }}
{% endif %}
- wait_template: "{{ is_state_attr(tags[trigger.event.data.tag_id].media_player, 'source', 'Emby') }}"
- delay: 5 #this is annoying, need to be more elegant here.
- service: rest_command.emby_start_show
data_template:
show: sensor.emby_random_always_sunny_episode
session: sensor.emby_session_upstairs_living_room