Author Topic: Plugin: Emby Interface  (Read 343 times)

Offline rigpapa

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 902
  • Karma: +143/-1
Plugin: Emby Interface
« on: January 03, 2019, 05:29:50 pm »
Emby Interface is a control and response interface for the Emby media server. It allows you to control Emby sessions (clients playing media), and respond to basic session events.

This plugin was developed at the request of PrincessCleavage and cw-kid, so if you're an Emby user, be sure to thank them for stepping up to get it launched! I'd also like to thank Luke, the lead Emby developer/architect, for his support.

This project lives in a Github repository, here: https://github.com/toggledbits/Emby

Documentation is in the README file in the above Github repository. As is typical for my work, the "master" branch represents the latest full release; the "stable" branch contains updates from development that have had minimal testing and are deemed "stable"; and the "develop" branch is the latest code with work in progress checkpoints (the bleeding edge). The CHANGELOG file in each branch will tell you what has evolved.

Please submit questions or bug reports here in this thread. Bug reports can instead be submitted to the issues section of the Github repo if you like that approach.


VERSION HISTORY
2019-01-06: Version 1.0. General public release.
« Last Edit: January 07, 2019, 07:57:18 am by rigpapa »
Author of Reactor, DelayLight, SiteSensor, Rachio, Deus Ex Machina II, Intesis WMP Gateway, Auto Virtual Thermostat and VirtualSensor plugins. Vera Plus w/100+ Z-wave devices. Vera3 sandbox.

Offline rigpapa

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 902
  • Karma: +143/-1
Re: Plugin: Emby Interface
« Reply #1 on: January 06, 2019, 12:50:06 pm »
Version 1.0 has been submitted to the Vera Plugin Marketplace for approval (expected publicly available tomorrow morning). It is available immediately in the AltAppStore.

You can also download and install the release package manually: https://github.com/toggledbits/Emby/releases/tag/v1.0
Author of Reactor, DelayLight, SiteSensor, Rachio, Deus Ex Machina II, Intesis WMP Gateway, Auto Virtual Thermostat and VirtualSensor plugins. Vera Plus w/100+ Z-wave devices. Vera3 sandbox.

Offline Jamr

  • Hero Member
  • *****
  • Posts: 837
  • Karma: +29/-8
Re: Plugin: Emby Interface
« Reply #2 on: January 07, 2019, 12:03:34 pm »
Looking forward to this.
As I understand, there is no easy way to integrate Plex with Vera but Emby does this pretty easy?
And with your add on, you can do more or is it easier?

Offline rigpapa

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 902
  • Karma: +143/-1
Re: Plugin: Emby Interface
« Reply #3 on: January 07, 2019, 12:25:42 pm »
It's available now. I would not say it was easy, but that's mostly because the documentation is out of date and has lots of errors--code was a no-brainer, once you figured out how something worked, but figuring it out when the docs say one thing and the API wants another had some frustrating moments. I got further using swagger to pound the API than I did when I was relying on the docs. The product itself is quite robust, though, and the chief architect/developer is responsive to questions and suggestions. Clients are a bit quirky, because many are developed by third parties and so bring their own ideas (and bugs) into the mix, but that's not different from Plex.

I have to say, I really like it. I've been using it every day for about a month, and it has really grown on me. I've been a Plex user for years, but recently the Plex UI changed dramatically on Roku and frankly, I hate it, and if that's the direction others will be going, I will jump ship. I prefer the previous UI, and Emby's UI is similar with its own spin, and I find it very agreeable. So Emby is getting a lot of use in my house quite suddenly.
Author of Reactor, DelayLight, SiteSensor, Rachio, Deus Ex Machina II, Intesis WMP Gateway, Auto Virtual Thermostat and VirtualSensor plugins. Vera Plus w/100+ Z-wave devices. Vera3 sandbox.

Offline Don Phillips

  • Hero Member
  • *****
  • Posts: 1421
  • Karma: +38/-35
Re: Plugin: Emby Interface
« Reply #4 on: January 07, 2019, 09:12:31 pm »
It's available now. I would not say it was easy, but that's mostly because the documentation is out of date and has lots of errors--code was a no-brainer, once you figured out how something worked, but figuring it out when the docs say one thing and the API wants another had some frustrating moments. I got further using swagger to pound the API than I did when I was relying on the docs. The product itself is quite robust, though, and the chief architect/developer is responsive to questions and suggestions. Clients are a bit quirky, because many are developed by third parties and so bring their own ideas (and bugs) into the mix, but that's not different from Plex.

I have to say, I really like it. I've been using it every day for about a month, and it has really grown on me. I've been a Plex user for years, but recently the Plex UI changed dramatically on Roku and frankly, I hate it, and if that's the direction others will be going, I will jump ship. I prefer the previous UI, and Emby's UI is similar with its own spin, and I find it very agreeable. So Emby is getting a lot of use in my house quite suddenly.


I agree the Roku UI was shocking when I first saw it. After a while you figure out how to edit the icons at the top and it is easier to jump around than it was before.
Vera 3, 1.7.1030, CT101, Everspring motion sensor, GE/Jasco switch, Leviton outlet, AeonLabs sensor, NuTone garage door, Blue Iris, Sricam SP011, iPhone locator, APCUPSD, VeraMate, VeraAlerts, PLEG, House Modes, Countdown Timer, DVR, Virtual/Multi Switch, Weatherunderground, LB60Z-1 bulb, Hue, Alexa

Offline chef

  • Sr. Newbie
  • *
  • Posts: 24
  • Karma: +0/-0
Re: Plugin: Emby Interface
« Reply #5 on: January 13, 2019, 10:12:19 pm »
I really like how Vera will send you messages when playback initiates on a device.
It helps me monitor my kids!!

Could another item like the "urn:schemas-toggledbits-com:device:EmbyUsers:1" device type be made for users?

Notifications to my phone and Android app are cool when someone logs into the emby server.

Especially if it was repeated login attempts.

The lua is polling server for information, the xml for serverInfo and sessionInfos. There is no persistent WebSocket connection, I don't think?

serverRequest to "/users"  will list all the current logged in userNames.


The perfect scene:
If Julia's Geofence is "HOME", and Julia is signed into Emby Device, started playback and time is past 10:00pm, make sure the Door is locked, and alarms are armed."



"Sessions/{Id}/Message/" is a good favorite. it can send messages to the devices the plugin has found earlier.
Might be cool for doorbell events.

If Julia's started playback of mediaItem, and past 10:00pm, and door is unlocked then send message to emby device "Door is unlocked".

That would be Smart Living!


« Last Edit: January 13, 2019, 11:07:39 pm by chef »

Offline chef

  • Sr. Newbie
  • *
  • Posts: 24
  • Karma: +0/-0
Re: Plugin: Emby Interface
« Reply #6 on: January 13, 2019, 11:15:43 pm »
Mobile has no playback interface.

I think each emby device needs to be marked as category 3 and sub_catagory 0

Offline rigpapa

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 902
  • Karma: +143/-1
Re: Plugin: Emby Interface
« Reply #7 on: January 14, 2019, 08:00:39 am »
Mobile has no playback interface.

I think each emby device needs to be marked as category 3 and sub_catagory 0

You can try setting them and see what happens. The mobile apps have a long history of no UI for plugins.
Author of Reactor, DelayLight, SiteSensor, Rachio, Deus Ex Machina II, Intesis WMP Gateway, Auto Virtual Thermostat and VirtualSensor plugins. Vera Plus w/100+ Z-wave devices. Vera3 sandbox.

Offline rigpapa

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 902
  • Karma: +143/-1
Re: Plugin: Emby Interface
« Reply #8 on: January 14, 2019, 10:19:30 am »
Mobile has no playback interface.

I think each emby device needs to be marked as category 3 and sub_catagory 0

...adding to my previous reply... category 3 is a switch, which I don't think would be the best choice. Category 15 is A/V, and that is configured in the device file. http://wiki.micasaverde.com/index.php/Luup_Device_Categories
Author of Reactor, DelayLight, SiteSensor, Rachio, Deus Ex Machina II, Intesis WMP Gateway, Auto Virtual Thermostat and VirtualSensor plugins. Vera Plus w/100+ Z-wave devices. Vera3 sandbox.

Offline chef

  • Sr. Newbie
  • *
  • Posts: 24
  • Karma: +0/-0
Re: Plugin: Emby Interface
« Reply #9 on: January 14, 2019, 02:25:57 pm »
Mobile has no playback interface.

I think each emby device needs to be marked as category 3 and sub_catagory 0

...adding to my previous reply... category 3 is a switch, which I don't think would be the best choice. Category 15 is A/V, and that is configured in the device file. http://wiki.micasaverde.com/index.php/Luup_Device_Categories

Yes category 15 of coarse... Wonderful.

This plugin is put together very well. It is a great read. Thank you. I'll definitely put some money in the coffers for this :)


I notice that you used require"socket" for http requests, but wasn't sure if the lua require"websocket" would allow for server events to pass through to vera?

emby does have a url which for websockets specifically: ws://{host}?api_key={authenticationtoken}&deviceId={deviceId}

I'm really new to lua? {cringe} ... it's kinda like node... a little bit I guess ....


--  -------------------------------------------------------------------------
local websocket = require'websocket'
local client = websocket.client.sync({timeout=60})

-- connect to the server:
local fullurl = luup.variable_get( SERVERSID, "LocalAddress", dev ) or "http://localhost:8096"
local key = luup.variable_get( SERVERSID, "APIKey", dev )

local ok,err = client:connect("ws://" .. fullurl .. "?api_key=" .. urlencode(key) .. "&deviceId=" .. {STILL NEED DEVICE ID}",'echo')
if not ok then
   print('could not connect',err)
end

--  ----------------------------

I wondering what kind of magical things vera could do when parsing event info from emby.
I keep thinking of interesting utilization.  With in reason, we don't want lights to change state each time there is an authentication error, but I'm just loving the playback notifications from vera, they are extremely useful.


I notice that Alexa is unable to register emby devices through vera. I don't think that matters.

I personally use OWIN Self hosted server to create endpoints that Alexa can hit, for most directives my custom Emby plugins or Vera endpoints can't handle.
These OWIN instances keep code in the realm of C#, which means coding is lighting fast for me ;)




Offline chef

  • Sr. Newbie
  • *
  • Posts: 24
  • Karma: +0/-0
Re: Plugin: Emby Interface
« Reply #10 on: January 14, 2019, 02:39:00 pm »
Cool you did add message capabilities:

function actionSessionMessage( );

Fantastic! Will we be able to use it in vera scenes?

Perhaps if the Server was available as a device in scene creation, with the ability to use the message action, by selecting an emby device to message.

Then vera could check conditions of zwave products and message appropriately.


Sorry for so many posts, the lua is a great read sir.
The MVP is luke. Swagger didn't work for a long time when they added authentication. But, yeah...

Also, if you wanted specific endpoints in the emby server for something, I can whip them up for you.

That is, if there is something you'd want the emby server to handle and return to vera.
   
« Last Edit: January 14, 2019, 03:04:44 pm by chef »

Offline rigpapa

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 902
  • Karma: +143/-1
Re: Plugin: Emby Interface
« Reply #11 on: January 14, 2019, 04:34:20 pm »
Cool you did add message capabilities:

function actionSessionMessage( );

Fantastic! Will we be able to use it in vera scenes?

Perhaps if the Server was available as a device in scene creation, with the ability to use the message action, by selecting an emby device to message.
Alas, if it is to be done in the scene editor, it has to be done using the limited UI capabilities of Vera's "static JSON" page/action/control description. Anything dynamic, like presenting a list of Emby servers, won't be happening, at least not in UI7. We can hope for better days ahead, perhaps. If I did it in JavaScript, you'd have it on the control panel, but it wouldn't show up in the scene editor.

For now, the way to send a message in a scene is to use scene Lua (or you can use the action-running abilities of a Reactor activity, or Rules Engine, PLEG, etc.).
Code: [Select]
-- Send a message to an Emby session (devnum = its Vera device number)
luup.call_action( "urn:toggledbits-com:serviceId:EmbySession1", "Message", { Text="hello world!", Header="Greeting" }, devnum )

Quote
Sorry for so many posts, the lua is a great read sir.
The MVP is luke. Swagger didn't work for a long time when they added authentication. But, yeah...

Ah hahah! Indeed, those docs were killing me. The most trivial tasks turned into hours-long fussing about, until I found swagger. But I get it. The Remote Control API is probably not the most used, so between not getting a lot of in-team attention, and probably very few people looking at it outside, it's bound to slip off. Swagger is the white knight.

Quote
Also, if you wanted specific endpoints in the emby server for something, I can whip them up for you.

That is, if there is something you'd want the emby server to handle and return to vera.

You've probably seen the discussion with Luke regarding a real "resume"--start replaying the whole queue from where it stopped. I faked it trivially, as you've now seen, by tracking the current media item playing, but since I also can't access the session's queue, I don't know what comes after the current playing media item. The simplest thing for me would be a resume, that simply restarts the last queue on the session (which would imply that the session's queue has to have some persistence, of course, which currently does not appear to be the case. Second best would be a way to fetch the session's queue--a simple list of all the media items that are to be played (and perhaps even those played, and some kind of pointer to the current position on the list). That's a user request, by the way. I'm not sure how useful it is, myself. I'm not sure I'd use it. If I want my queue to stop so I can pick it up later, I'd pause and unpause. If I want media queued up so I can start it playing, I'd create a playlist and use PlayMedia to launch it on demand. Relying on the client's or server's memory of what to play doesn't seem like the right approach. But, it was requested, so I passed it along, and did what I could for now.

The other thing I'd like to get done that I think would really cool, expanding on the "Message" action, would be text-to-speech. I could pull that off if I could get a session to play media by URI (even if its just a file:///tmp/foo.mp3 to play a file that's local to the server, but ideally, any common protocol/location). Basically, play media that isn't in the library. I've mentioned that to Luke as well. Not a priority for anyone (self included), but could be useful.

Websocket... I have a whole implementation for that. I did it all, made it work. But the real benefit--update immediately when a message arrives--can't be realized. If I block on the socket, Vera eventually deadlocks, trips the watchdog and reboots. They're not hooked in deep enough to yield on I/O waits. They want the plugin routines to execute in five seconds or less, or they start logging warning messages. Even if I block for just five seconds, then, I have to give control back to them, I have to return; and then the only way to get control back is with a timer action (one second resolution)--basically polling again. Benefit gone. So I backed it out and shelved it. There is another method, an old, crusty method that's actually modeled for collecting data from serial devices, that I can try, but I haven't gone there yet; it would require restructuring all the I/O into that model. I look at this plugin more as a command and control thing, not as a replacement UI for the client UIs. Even if I overcame the socket blocking, Vera only updates its UI about once every two seconds at best, so it will still look jittery and, well, crappy. I can't even do a free-running clock in JavaScript and try to sync it with the session play progress--UI7 isn't that flexible on the dashboard cards. If Vera used ALTUI's model, it would be easy. Hopefully they go that direction. But anyway, for now, it's command and control, with just enough UI that you can generally see what's going on.
Author of Reactor, DelayLight, SiteSensor, Rachio, Deus Ex Machina II, Intesis WMP Gateway, Auto Virtual Thermostat and VirtualSensor plugins. Vera Plus w/100+ Z-wave devices. Vera3 sandbox.

Offline chef

  • Sr. Newbie
  • *
  • Posts: 24
  • Karma: +0/-0
Re: Plugin: Emby Interface
« Reply #12 on: January 14, 2019, 09:18:56 pm »
A request to "NextUp" would give the info for the next item in a queue, but I'm not sure if emby creates a new queue of items each time a session is started.

Specifically when dealing with TV shows emby can be set to play the next item (episode) automatically, and music can be played by album.

A persistence plugin could be made with an accessable endpoint.

If it is about audio, persisting a playlist of favorites, starting where it left off, then I can see some usefulness in having it.   


I use text to speech with a home brew Alexa skill and emby plugin. When I ask for a media item, Alexa will read the overview.

I made a video once about it...
https://youtu.be/bnay9NBgOSQ

Getting emby to play a background audio track for TTS  is a very interesting concept.
Wait! There is Theme Song plugin for emby which does exactly that, plays audio behind the interface.
Leave that with me, and I'll talk to the plugin Dev about how he initalized a background playback instance  without hijacking the player.


Thank you for the Lua Message service example as well, it is invaluable!  It is pure magic sir!

Benny
« Last Edit: January 14, 2019, 09:21:00 pm by chef »

Offline baxy_AU

  • Sr. Member
  • ****
  • Posts: 268
  • Karma: +5/-0
Re: Plugin: Emby Interface
« Reply #13 on: January 15, 2019, 04:50:56 pm »
The theme song and video plugins purely just download the media to the correct location and name for emby server to pick up. It is up to the cilent devices to implement the display/playback of them