Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacrlevin committed Feb 29, 2024
2 parents 24890a5 + 264dc3b commit 18ab144
Show file tree
Hide file tree
Showing 13 changed files with 289 additions and 59 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
| LIFX |
| Yeelight |
| Philips Wiz |
| [WLED](https://kno.wled.ge/) (via serial or web API) |
| Any light which can be controlled via a GET or POST call to a web API |

## Docs
Expand Down
80 changes: 79 additions & 1 deletion docs/configure-custom-api.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Custom API

The Custom API page lets you use any generic service which has a web API which accepts GET or POST requests.

Expand All @@ -10,4 +11,81 @@ To connect PresenceLight to a custom API:
Configure the web service (e.g. created the applets in IFTTT)
Enter the corresponding API method and URI against each presence state.

![Configured](../static/customApi.png)
![Configured](../static/CustomAPI.png)

The Custom API REST API calls also support providing a json formatted body to the endpoints (Uri) of Custom API.

You can use the following variables in your JSON body:

- {{availability}}
- {{activity}}

If you use above variables in the JSON body they will be replaced with the availability and/or activity values of your Microsoft Teams status.

## Home Assistant integration

To use PresenceLight with Home Assistant you can use the Custom API functionality as follows:

In Home Assistant you can use [Webhooks triggers](https://www.home-assistant.io/docs/automation/trigger/#webhook-trigger) to trigger an Automation Action, like turning on a light bulb.

Example Automation for turning on a light bulb based on the Teams status send using the Custom API functionality of PresenceLight.

```yaml
alias: Teams presence - IKEA Light Bulb Living Room
description: >-
Show the Microsoft Teams status via a color of the Light Bulb in the Living
Room
trigger:
- platform: webhook
allowed_methods:
- POST
local_only: true
webhook_id: "<enter secret webhook id here>"
condition: []
action:
- choose:
- conditions:
- condition: template
value_template: "{{ trigger.json.presence_status == 'Busy' }}"
sequence:
- service: light.turn_on
metadata: {}
data:
color_name: red
target:
entity_id: light.ikea_bulb
- conditions:
- condition: template
value_template: "{{ trigger.json.presence_status == 'Available' }}"
sequence:
- service: light.turn_on
metadata: {}
data:
color_name: green
target:
entity_id: light.ikea_bulb
- conditions:
- condition: template
value_template: "{{ trigger.json.presence_status == 'Away' }}"
sequence:
- service: light.turn_on
metadata: {}
data:
color_name: yellow
target:
entity_id: light.ikea_bulb
- conditions: null
sequence:
- service: light.turn_off
metadata: {}
target:
entity_id: light.ikea_bulb
data: {}
mode: single
```
In PresenceLight Custom API setting you need to enter the following information:
| Method | Uri | Body |
|--------|----------------|------|
| POST | http://homeassistant.local:8123/api/webhook/webhook_id | { "presence_status":"Away" } |
63 changes: 42 additions & 21 deletions src/DesktopClient/PresenceLight/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -379,87 +379,108 @@
"UseActivityStatus": false,
"CustomApiAvailable": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiBusy": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiBeRightBack": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiAway": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiDoNotDisturb": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiOffline": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiOff": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiActivityAvailable": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiActivityInACall": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiActivityInAConferenceCall": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiActivityInAMeeting": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiActivityPresenting": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiActivityBusy": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiActivityAway": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiAvailableIdle": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiActivityBeRightBack": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiActivityDoNotDisturb": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiActivityIdle": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiActivityOffline": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiActivityOff": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiActivityOffWork": {
"Method": "",
"Uri": ""
"Uri": "",
"Body": ""
},
"CustomApiTimeout": 100,
"IgnoreCertificateErrors": false,
Expand Down
5 changes: 5 additions & 0 deletions src/PresenceLight.Core/Configuration/CustomApiSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,10 @@ public class CustomApiSetting
/// Gets or sets the URI of the API.
/// </summary>
public string? Uri { get; set; }

/// <summary>
/// Gets or sets the Body of the API.
/// </summary>
public string? Body { get; set; }
}
}
22 changes: 22 additions & 0 deletions src/PresenceLight.Core/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,27 @@ public static string HoursPassedStatusString(HoursPassedStatus status) =>
HoursPassedStatus.Off => "Off",
_ => throw new ArgumentException(message: "Invalid HoursPassedStatus Value", paramName: nameof(status)),
};

/// <summary>
/// Replaces the variables in the given body with the provided availability and activity.
/// </summary>
/// <param name="body">The body in which to replace the variables.</param>
/// <param name="availability">The availability to replace the {{availability}} variable.</param>
/// <param name="activity">The activity to replace the {{activity}} variable.</param>
/// <returns>The body with the variables replaced.</returns>
public static string ReplaceVariables(string body, string? availability, string? activity)
{
if (body.Contains("{{availability}}"))
{
body = body.Replace("{{availability}}", availability ?? string.Empty);
}

if (body.Contains("{{activity}}"))
{
body = body.Replace("{{activity}}", activity ?? string.Empty);
}
return body;
}

}
}
Loading

0 comments on commit 18ab144

Please sign in to comment.