-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from HCookie/develop
Update master
- Loading branch information
Showing
11 changed files
with
251 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,28 @@ | ||
import logging | ||
import requests | ||
import json | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
|
||
DOMAIN = "webhook_service" | ||
_LOGGER = logging.getLogger(__name__) | ||
from .const import DOMAIN, WEBHOOKS_DATAS | ||
|
||
import logging | ||
_LOGGER = logging.getLogger(__name__) | ||
|
||
SERVICE_SEND = "basic_webhook" | ||
async def async_setup(hass: HomeAssistant, config: dict): | ||
"""Set up the integration.""" | ||
if DOMAIN not in hass.data: | ||
hass.data[DOMAIN] = {} | ||
await _setup_webhooks(hass, config) | ||
return True | ||
|
||
def setup(hass, config): | ||
def send_basic_webhook(call): | ||
data = call.data.copy() | ||
if "json" in data: | ||
jsondata = json.loads(data["json"]) | ||
else: | ||
jsondata = {} | ||
result = requests.post(data["webhook"], json = jsondata) | ||
_LOGGER.warn('Received data', data["webhook"]) | ||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): | ||
"""Set up the platform.""" | ||
if DOMAIN not in hass.data: | ||
hass.data[DOMAIN] = {} | ||
await _setup_webhooks(hass, entry) | ||
return True | ||
|
||
hass.services.register(DOMAIN, SERVICE_SEND, send_basic_webhook) | ||
|
||
return True | ||
async def _setup_webhooks(hass: HomeAssistant, data: dict | ConfigEntry): | ||
for webhook_data in WEBHOOKS_DATAS: | ||
if "service" in webhook_data and "function" in webhook_data: | ||
hass.services.async_register(DOMAIN, webhook_data["service"], webhook_data["function"]) | ||
_LOGGER.info(f'{webhook_data["service"]} set up') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from typing import Any | ||
|
||
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult | ||
|
||
from .const import DOMAIN, DEFAULT_NAME | ||
|
||
class WebhookServiceConfigFlow(ConfigFlow, domain=DOMAIN): | ||
"""Handle a config flow.""" | ||
|
||
async def async_step_user( | ||
self, user_input: dict[str, Any] | None = None | ||
) -> ConfigFlowResult: | ||
"""Handle the initial step.""" | ||
if self._async_current_entries(): | ||
return self.async_abort(reason="single_instance_allowed") | ||
|
||
if user_input is None: | ||
return self.async_show_form(step_id="user") | ||
|
||
return self.async_create_entry(title=DEFAULT_NAME, data={}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from typing import Final | ||
|
||
DOMAIN = "webhook_service" | ||
DEFAULT_NAME = "Webhook Service Integration" | ||
|
||
from .webhook_functions import functions | ||
WEBHOOKS_DATAS: Final = [ | ||
{ | ||
"service": "basic_webhook", | ||
"function": functions.basic_webhook | ||
}, | ||
{ | ||
"service": "discord_webhook", | ||
"function": functions.discord_webhook | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
{ | ||
"domain": "webhook_service", | ||
"name": "Webhook Service Integration", | ||
"codeowners": [], | ||
"config_flow": true, | ||
"dependencies": [], | ||
"documentation": "https://github.com/HCookie/Webhook-Service-home-assistant", | ||
"iot_class": "local_polling", | ||
"issue_tracker": "https://github.com/HCookie/Webhook-Service-home-assistant/issues", | ||
"dependencies": [], | ||
"codeowners": [], | ||
"requirements": [], | ||
"iot_class": "local_polling", | ||
"version": "0.1.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"config": { | ||
"step": { | ||
"user": { | ||
"description": "Do you want to add Webhook Service?" | ||
} | ||
}, | ||
"abort": { | ||
"single_instance_allowed": "Only single instance is allowed." | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"config": { | ||
"step": { | ||
"user": { | ||
"description": "Do you want to add Webhook Service?" | ||
} | ||
}, | ||
"abort": { | ||
"single_instance_allowed": "Only single instance is allowed." | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
custom_components/webhook_service/webhook_functions/basic.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import logging | ||
import requests | ||
import json | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
def basic_webhook(call): | ||
data = call.data.copy() | ||
if "json" in data: | ||
jsondata = json.loads(data["json"]) | ||
elif "jsonObj" in data: | ||
jsondata = data["jsonObj"] | ||
else: | ||
jsondata = {} | ||
result = requests.post(data["webhook"], json = jsondata) | ||
_LOGGER.warn('Received data', data["webhook"]) |
60 changes: 60 additions & 0 deletions
60
custom_components/webhook_service/webhook_functions/discord.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import logging | ||
import requests | ||
import json | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
def discord_webhook(call): | ||
data = call.data.copy() | ||
title = data.get("title", "Embed Title") | ||
title_url = data.get("title_url") | ||
description = data.get("description", "Embed Description") | ||
thumbnail = data.get("thumbnail") | ||
author = data.get("author") | ||
fields = data.get("fields") | ||
image = data.get("image") | ||
color = data.get("color") | ||
timestamp = data.get("timestamp") | ||
footer = data.get("footer") | ||
webhook_url = data.get("webhook") | ||
if not webhook_url: | ||
_LOGGER.error("No webhook URL provided.") | ||
return | ||
|
||
output_data = { | ||
"title": title, | ||
"description": description | ||
} | ||
|
||
if title_url: | ||
output_data["url"] = title_url | ||
if thumbnail: | ||
output_data["thumbnail"] = thumbnail | ||
if author: | ||
try: | ||
output_data["author"] = json.loads(str(author)) | ||
except Exception as e: | ||
_LOGGER.error(e) | ||
if fields: | ||
try: | ||
output_data["fields"] = json.loads(str(fields))[:25] | ||
except Exception as e: | ||
_LOGGER.error(e) | ||
if image: | ||
output_data["image"] = image | ||
if color: | ||
output_data["color"] = color | ||
if timestamp: | ||
output_data["timestamp"] = timestamp | ||
if footer: | ||
output_data["footer"] = footer | ||
|
||
embed_data = { | ||
"embeds": [output_data] | ||
} | ||
|
||
result = requests.post(webhook_url, json=embed_data) | ||
if result.status_code == 200: | ||
_LOGGER.info("Embed sent successfully.") | ||
else: | ||
_LOGGER.error("Failed to send embed: %s", result.text) |
2 changes: 2 additions & 0 deletions
2
custom_components/webhook_service/webhook_functions/functions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .basic import basic_webhook | ||
from .discord import discord_webhook |