Spotify
Segue o tutorial para a configuração do Spotify no Home Assistant que permite controlar via HA o Spotify em diversos dispositivos , tambem é possivel configurar em contas gratuitas no entanto não existe a possibilidade de controlo ( mudar musica , selecionar fonte, play , stop… etc)
Pré requisitos
Configuração de packages
Conta Premium Spotify
Passos necessários para cirar a API do Spotify.
- Selecionar “Create Client ID”
- Colocar nome e descrição, preencher os restantes campos obrigatórios e submeter.
- Depois da aplicação estar criada é necessario copiar os seguintes campos Client ID e Client Secret ( no meu caso para o ficheiro secrets.yaml)
- Selecionar Edit Settings e adicionar o Redirect Uri
Sem SSL: http://<your_home_assistant_url_or_local_ip>:<port>/api/spotify
SSL: https://<your_home_assistant_url_or_local_ip>:<port>/api/spotify
-
Clicar em Save depois de adicionar os/o endereço URI, poderá ser em alguns casos necessário adicionar o base_url no componte HTTP existente no configuration.yaml.
http:
base_url: example.duckdns.org:8123
Colocar o ficheiro de configuração em packages
Spotify.yaml
media_player:
- platform: spotify
client_id: !secret spotify_client_id
client_secret: !secret spotify_client_secret
group:
home_spotify_player:
name: Spotify Player
entities:
- media_player.spotify
home_spotify_controls:
name: Spotify Controls
control: hidden
entities:
- input_select.spotify_playlist
- input_select.spotify_source
- script.spotify_music
- input_number.spotify_volume
- input_boolean.spotify_shuffle
homeassistant:
customize:
script.spotify_music:
friendly_name: 'Play'
icon: mdi:play
sensor:
- platform: template
sensors:
spotify_volume:
friendly_name: "Spotify Volume"
value_template: "{{ states.media_player.spotify.attributes.volume_level }}"
- platform: template
sensors:
spotify_shuffle:
friendly_name: "Spotify Shuffle"
value_template: "{{ states.media_player.spotify.attributes.shuffle }}"
- platform: template
sensors:
spotify_source:
friendly_name: "Spotify Source"
value_template: "{{ states.media_player.spotify.attributes.source }}"
input_number:
spotify_volume:
name: Volume
icon: mdi:volume-high
initial: 100
min: 0
max: 100
step: 1
input_select:
spotify_playlist:
name: 'Playlist:'
options:
- Lista1
- Lista2
- Lista3
- Lista4
icon: mdi:spotify
spotify_source:
name: 'Source:'
options:
- Web Player (Chrome)
- Mi Phone
- DESKTOP-922IFR3
initial: Web Player (Chrome)
icon: mdi:speaker-wireless
input_boolean:
spotify_shuffle:
name: Shuffle
icon: mdi:shuffle-variant
script:
spotify_music:
alias: "Play selected spotify playlist"
sequence:
- service: media_player.select_source
data_template:
entity_id: media_player.spotify
source: >
{% if is_state("input_select.spotify_source", "Web Player (Chrome)") %} Web Player (Chrome)
{% elif is_state("input_select.spotify_source", "Mi Phone") %} Mi Phone
{% elif is_state("input_select.spotify_source", "DESKTOP-922IFR3") %} DESKTOP-922IFR3
{% endif %}
- service: media_player.play_media
data_template:
entity_id: media_player.spotify
media_content_type: playlist
media_content_id: >
{% if is_state("input_select.spotify_playlist", "Lista1") %} spotify:user:spotify:playlist:37i9dQZF1DXakWdSZ8mUMO
{% elif is_state("input_select.spotify_playlist", "Lista2") %} spotify:user:spotify:playlist:5dfbLgeesT8gixq1GTWBoU
{% elif is_state("input_select.spotify_playlist", "Lista3") %} spotify:user:spotify:playlist:37i9dQZF1DX4WYpdgoIcn6
{% elif is_state("input_select.spotify_playlist", "Lista4") %} spotify:user:spotify:playlist:6IjDl5eRczFdgZkKYXhuHZ
{% endif %}
automation:
- alias: 'Spotify Volume (Set)'
trigger:
platform: state
entity_id: input_number.spotify_volume
action:
service: media_player.volume_set
data_template:
entity_id: media_player.spotify
volume_level: '{{ states.input_number.spotify_volume.state | int / 100 }}'
- alias: 'Spotify Volume (Sync)'
trigger:
- platform: homeassistant
event: start
- platform: state
entity_id: sensor.spotify_volume
action:
- delay:
seconds: 5
- service: input_number.set_value
data_template:
entity_id: input_number.spotify_volume
value: '{{ states.sensor.spotify_volume.state | float | round(2) * 100 }}'
- alias: 'Spotify Shuffle (Set)'
trigger:
platform: state
entity_id: input_boolean.spotify_shuffle
action:
service: media_player.shuffle_set
data_template:
entity_id: media_player.spotify
shuffle: >
{% if is_state('input_boolean.spotify_shuffle', 'off') %}
false
{% elif is_state('input_boolean.spotify_shuffle', 'on') %}
true
{% endif %}
- alias: 'Spotify Shuffle (Sync)'
trigger:
- platform: homeassistant
event: start
- platform: state
entity_id: sensor.spotify_shuffle
action:
- delay:
seconds: 5
- service_template: >
{% if states.sensor.spotify_shuffle.state == 'False' %}
input_boolean.turn_off
{% else %}
input_boolean.turn_on
{% endif %}
data:
entity_id: input_boolean.spotify_shuffle
- alias: 'Spotify Source (Sync)'
trigger:
- platform: homeassistant
event: start
- platform: state
entity_id: sensor.spotify_source
action:
service: input_select.select_option
entity_id: input_select.spotify_source
data_template:
option: '{{ states.sensor.spotify_source.state }}'
- Reiniciar o HA
- Após o Reboot irá surgir as seguinte opçao no HA que é necessário configurar/Autorizar, após esta ação estará pronto a ser utilizado o Spotify.
Os seguintes campos devem ser editados
name: 'Playlist:'
options:
- Lista1
spotify_source:
name: 'Source:'
options:
- Web Player (Chrome)
- Mi Phone
Para obter o nome das Sources
O mais simples será aceder ao Dev State , com todos os devices ligados ao Spotify vão aparcer neste campo.
Como obter o URI
É necessario instalar o Cliente do Spotify selecionar a Playlist conforme a imagem
O mesmo deverá ser substiuido no spotify.yaml
{% elif is_state("input_select.spotify_playlist", "Lista4") %} spotify:user:spotify:playlist:6IjDl5eRczFdgZkKYXhu
HZ