# @name SpotifyNowPlaying # @desc Zeigt Interpret und Titel aus Home Assistant, wenn Spotify spielt # @author r2k # @version 1.1 # @config ha_url text "Home Assistant URL" default="http://homeassistant.local:8123" maxlen=128 # @config ha_token text "Home Assistant Token" default="PASTE_LONG_LIVED_TOKEN_HERE" maxlen=256 # @config entity text "Spotify Entity" default="media_player.spotify" maxlen=64 # @config refresh_seconds number "Refresh" default=15 min=5 max=120 unit=s # @config scroll_speed slider "Scrolltempo" default=70 min=30 max=150 unit=% # @config spotify_icon_id text "Spotify Icon ID" default="18207" maxlen=16 # @config airplay_icon_id text "AirPlay Icon ID" default="" maxlen=16 # @config radio_icon_id text "Radio Icon ID" default="" maxlen=16 import HAflow class SpotifyNowPlaying var ticks, in_flight var label, text_color, icon_id var entity, refresh_seconds, spotify_icon_id, airplay_icon_id, radio_icon_id, scroll_opts def init() HAflow.init(store.get("ha_url"), store.get("ha_token")) self.entity = store.get("entity") self.refresh_seconds = store.get("refresh_seconds") self.spotify_icon_id = store.get("spotify_icon_id") self.airplay_icon_id = store.get("airplay_icon_id") self.radio_icon_id = store.get("radio_icon_id") self.ticks = 0 self.in_flight = false self.label = nil self.icon_id = self.spotify_icon_id self.text_color = settings.get("textColor") if self.text_color == nil self.text_color = 0xFFFFFF end self.scroll_opts = { "mode": "loop", "whenFits": "static", "speed": clamp(store.get("scroll_speed"), 30, 150), "gap": 8, "holdMs": 400, "repeat": 2 } end def set_inactive() self.label = nil end def on_entity(data, error) self.in_flight = false if error != nil || data == nil || !isinstance(data, map) self.set_inactive() return end var state = data.find("state") if state != "playing" self.set_inactive() return end var attrs = data.find("attributes") if !isinstance(attrs, map) self.set_inactive() return end var artist = attrs.find("media_artist") var title = attrs.find("media_title") var source = attrs.find("source") var media_content_id = attrs.find("media_content_id") self.icon_id = self.radio_icon_id if type(source) == "string" && source == "AirPlay" self.icon_id = self.airplay_icon_id elif type(source) == "string" && re.search("Spotify", source) != nil self.icon_id = self.spotify_icon_id elif type(media_content_id) == "string" && re.search("spotify:", media_content_id) != nil self.icon_id = self.spotify_icon_id end if type(artist) == "string" && artist != "" && type(title) == "string" && title != "" self.label = artist + " - " + title return end if (artist == nil || artist == "") && type(title) == "string" && title != "" var m = re.match("^(.+) ˗ (.+)$", title) if m == nil m = re.match("^(.+) - (.+)$", title) end if m != nil && m[1] != nil && m[1] != "" && m[2] != nil && m[2] != "" self.label = m[1] + " - " + m[2] return end end self.set_inactive() end def loop() if self.ticks <= 0 self.ticks = self.refresh_seconds if !self.in_flight self.in_flight = true HAflow.entity(self.entity, / data, err -> self.on_entity(data, err)) end end self.ticks -= 1 end def should_show() return self.label != nil && self.label != "" end def draw() clear() var text_x = 0 if self.icon_id != nil && self.icon_id != "" if !icon(self.icon_id, 0, 0) rect_fill(0, 0, 8, 8, 0x222222) end text_x = 9 end scroll_text(text_x, 6, width() - text_x, self.label, self.text_color, self.scroll_opts) end def on_button(btn) if btn == "select" self.ticks = 0 end end end return SpotifyNowPlaying()