Closed TroyFernandes closed 4 years ago
sorry for the late answer. I can implement these features but can't really test them because I don't have a device that supports them. If I find some time I will make some changes you could test
No worries. I tried my hand at making a custom component for this and here's what I came up with:
There's some room for improvement but as of right now it functions correctly. If you want to see how I implemented a few of the features by all means please do!
Hey I've created a new branch with a few new features. If you have time I would really love it if you could test them out: https://github.com/Sennevds/media_player.template/tree/new_features It has the following new features: added following new features:
Hey,
I tried out most of the features you put in. Here are the ones that were working correctly for me:
title, artist, album name, get volume level (current_volume_template).
And here are the ones I couldn't test/try out:
play media, set volume level
I also tried the media image, and it seems the implementations right; its just that homeassistant limits an entity state value to 255 characters. That means that if you provide a base64 image url it will exceed that limit. In your case where you're reading the album_art_template as a cv.template, you're limited to that hard limit.
Really nice job! I gotta take a look at how you did the value_templates for some of the entities. It's a better way to implement it than how I did with just reading the mqtt topics.
Also one last thing. I see you have the code for setting the on/off state, but in terms of when the media is actually playing you won't see the playing/paused state update. So with some media player cards where the play and pause button is combined into one, you'll only get the play button since it thinks the media player is always paused. Just something to consider
Heres how it looks btw (your component on the right): Imgur
Oh damn forgot the playing state my bad. I will try to implement that today. I will check for the album_art_template if I can try to workaround this problem. Is there a reason you couldn't test the play media and set volume? Here's an example of set volume action:
set_volume:
service: input_text.set_value
data:
entity_id: input_text.selected_volume
value: "{{volume}}"
the parameters for play media are:
I don't have a device that can use the play_media service and for the set_volume, my media player responds to a command like so:
{ "command": "volume_set", "args": { "volume": "0.88" } }
I was reading your code and if I'm assuming right, if I set this in my configuration.yaml:
set_volume:
service: mqtt.publish
data:
topic: mediaplayer/command
value: "{{volume}}"
your async_set_volume_level(self, volume)
function would call a mqtt.publish with the payload { "volume" : "SOME_VAL"}
If you want to make it so anyone can tie in their players' functionality, you should look into being able to grab whatever their payload string would be and sub-in the volume value.
In my code I just did a control statement to see if the user set a volume topic and if they did I route everything to the set_volume_level
function. Otherwise just use whatever service they setup for volume_up/volume_down
async def volume_up(self):
"""Volume up the media player."""
if(self._vol_up_action):
await self._vol_up_action.async_run(context=self._context)
else:
newvolume = min(self._volume + 5, 100)
self._volume = newvolume
#_LOGGER.debug("Volume_up: " + str(newvolume))
self.set_volume_level(newvolume)
Hm I think you misunderstand the code. The async_set_volume_level function executes the script you set in your configuration and sends an extra variable with it.
So you can create any script that you want and use the {{volume}}
variable (0.1 - 1 value) in your script.
So when you press in lovelace somewhere on the volume bar your script will be launched and {{volume}}
will have the value where you clicked so in your example it would be something like this:
set_volume:
service: mqtt.publish
data:
topic: mediaplayer/command
value: "{\"command\":\"volume_set\", \"args\":{\"volume\":\"{{volume}}\"}}"
edit: I can add the code for volume_up and volume_down that when the volume up and down command aren't defined but the volume_set is you still can use the buttons btw this is based on the template cover have a look at this: https://www.home-assistant.io/integrations/cover.template/#multiple-covers
Interesting, I actually never knew it was possible to do that in homeassistant. My bad
I tried what you put above and I get this error.
Failed to call service media_player/volume_set. extra keys not allowed @ data['value']
I even get that when I do value: "{{volume}}"
. I'm thinking I'm filling out this part of the configuration.yaml wrong?
If I look at the docs it looks like you need to use payload or payload_template: https://www.home-assistant.io/docs/mqtt/service/
Yup, you're right. It works with payload_template.
Now the only thing is that it's showing 7500% as the current volume level, and so the slider gets pinned all the way to the right, but it still updates the volume correctly.
I just did a quick & dirty change to your code locally:
@property
def volume_level(self):
"""Volume level of the media player (0..1)."""
locVol = int(self._volume) / 100.0
return locVol
and it works correctly. Of course though there's errors if you use the exact change I just did but you get the idea
Aah will check that part nice that it's working. The part off variable is also new for me but very interesting.
Trying the set_volume feature, I get this error:
Invalid config for [media_player.media_player_template]: [set_volume] is an invalid option for [media_player.media_player_template]. Check: media_player.media_player_template->media_players->bathroom->set_volume. (See ?, line ?).
Trying the set_volume feature, I get this error:
Invalid config for [media_player.media_player_template]: [set_volume] is an invalid option for [media_player.media_player_template]. Check: media_player.media_player_template->media_players->bathroom->set_volume. (See ?, line ?).
Did you clone the master repo or the new_features branch?
Hi there!
Could "play_pause" be included as well?
I just wanna say first, great job on this component! I'm not sure if you realized but you're close to finishing this request
https://community.home-assistant.io/t/media-player-as-mqtt/204396
I installed your component and set it up with my MQTT enabled media player and the basic functionality works great! I can play/pause, skip, go back etc.
You should consider adding the other remaining services that Media Player provides. Mainly these are the major ones for a media player:
Anyways, this is just a suggestion I had for this otherwise great component! Hope to see you publish it one day 😄