Microsoft graph api

Hello,

Is it possible to use the json of https://graph.microsoft.com/beta/me/presence in home assistant?
I want to try to use part of this information to use it in an automation.

1 Like

Hi @Thuurke

You certainly can!

You can use rest sensor to get the relevant state.

I have just finished integrating this, so can give you an idea what this involves.

You will have to create a new app in your AAD tenant (you can follow this).

The necessary permissions are described here (you probably want to grant just what’s needed) - for the API endpoint you mention, Presence.Read should be enough.

I was using Postman to fetch my Auth Bearer token - MS again have a tutorial here - I followed “Set up on-behalf-of API calls”.

You can then use Postman (or any other API client) to get your bearer token to use in HA.

My rest sensor config is as follows:

 - platform: rest
   name: Teams Presence
   resource: https://graph.microsoft.com/beta/me/presence
   json_attributes:
     - availability
     - activity
   value_template: '{{ value_json.activity }}'
   headers:
     Content-Type: application/json
     Authorization: Bearer MySecretTokenFromPostman

That should give you 2 attributes to play with, activity & availability (more on presence here).

I am planning to use activity (and especially the InACall state) to automate my hue light to keep kids away from my office during calls! :slight_smile:

Give me a shout if you need any more help.

Thanks for the writeup! However when I follow it, the auth token it gives you is only valid for an hour. Is there any way to extend this?

Sorry, same issue on my end. The writeup was done just after I’ve managed to get it working, so didn’t get a chance to test token longevity.

The docs (here and here) suggest that the token can be refreshed, but this is not something that rest sensor, at least as implemented here, would be able to do.

In theory, rest sensor can do post requests to an endpoint (including json payload), so it might be just about possible to implement somehow, but this won’t be the most elegant solution. You probably would want a component doing that for you.

If I figure out how to make that work, will update this post.

Ok just wanted to make sure I wasn’t missing something. I’ll respond too if I figure something out.

Very nice! I’am trying but I have trouble to get a part of the postman tutorial.

Set up application API calls

In the MicrosoftGraph | Application collection on left side, choose Get App-only Access Token .
I don’t see this part in postman. Can you point me in the right direction?


If you don’t see this, make sure you imported the postman collections. Look for “Consume the collection” in the link you posted.
Unfortunately the token it will give you only lasts for an hour. I am working on making either a rest sensor or component that will refresh the bearer token.

Okay,
The Postman sharing link will launch Postman. must also be imported :slight_smile:
Must I follow “Set up application API calls” or is " Set up on-behalf-of API calls" enough?
I have followed “Set up application API calls” but step 5 and 6 give errors. So I skipped them and went to " Set up on-behalf-of API calls". But I don’t get values in rest sensor. I’am missing something but I don’t know what :frowning:

Hey guys - I’m also hoping to do the same thing, with my goal being to use a PyPortal to display an image relative to my presence state… I’m just about to have a play using the manual token creation in Postman.

Anyone worked out a way to refresh the token from HA directly, yet?

And I fell at the first hurdle! :smiley: Looks like I need to talk my CEO into creating a new app registration in our tenant first!

@Dakiel Did you make any progress on this?

If so, is it online somewhere so i can help?

Interested in doing something like that - anyone found a solution for the refresh token?

Yeah, I’m very interested in this too. Shout if anyone makes any progress, I’ll have a look into it myself.

I suspect I’ll end up writing an app in between given that I’m brand new to HA and don’t even have it installed anywhere yet :smiley:

You guys seen Presence Light on the Microsoft App store? I’m now using that, and linking to my emulated Philips Hue bridge in HA, working like a dream.

Well almost a dream… I’m adding some features to Presence Light as it’s open source and I want some extra functionality :slight_smile:

6 Likes

This works quite nice. Maybe would be handy that with some buttons presses the state could be changed. But maybe that’s is the otherway around.

I considered adding that myself but I just change my status in teams to force a change through. Unless I’m misunderstanding?

A colleague of mine did a similar thing, although HA wasn’t involved. Maybe this writeup would be useful @am3rig0?