Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
t0mer committed Apr 1, 2024
1 parent db36d92 commit ed315f9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions custom_components/greenapi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

12 changes: 12 additions & 0 deletions custom_components/greenapi/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"domain": "greenapi",
"name": "Whatsapp notifier basde on Green API",
"codeowners": [
"@t0mer"
],
"documentation": "https://github.com/t0mer/green-api-custom-notifier",
"iot_class": "local_polling",
"issue_tracker": "https://github.com/t0mer/green-api-custom-notifier",
"requirements": ["whatsapp-api-client-python"],
"version": "0.1.0"
}
46 changes: 46 additions & 0 deletions custom_components/greenapi/notify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import logging
import voluptuous as vol
from whatsapp_api_client_python import API
import homeassistant.helpers.config_validation as cv
from homeassistant.components.notify import (
ATTR_TARGET, ATTR_TITLE, PLATFORM_SCHEMA, BaseNotificationService)

ATTR_INSTANCE = "instance_id"
ATTR_TOKEN = "token"


_LOGGER = logging.getLogger(__name__)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(ATTR_TARGET): cv.string,
vol.Required(ATTR_INSTANCE): cv.string,
vol.Required(ATTR_TOKEN): cv.string,
vol.Optional(ATTR_TITLE): cv.string,
})

def get_service(hass, config, discovery_info=None):
"""Get the custom notifier service."""
target = config.get(ATTR_TARGET)
title = config.get(ATTR_TITLE)
token = config.get(ATTR_TOKEN)
instance_id = config.get(ATTR_INSTANCE)
return GreenAPINotificationService(target, title, token, instance_id)

class GreenAPINotificationService(BaseNotificationService):

def __init__(self, target, title, token,instance_id):
"""Initialize the service."""
self._targets = target.split(',')
self._title = title
self._token = token
self._instance_id = instance_id
self._greenAPI = API.GreenAPI(self._instance_id, self._token)

def send_message(self, message="", **kwargs):
"""Send a message to the target."""
try:
for target in self._targets:
_LOGGER.info("Sending message to %s: %s", target, message)
self._greenAPI.sending.sendMessage(target, message)
except Exception as e:
_LOGGER.error("Sending message to %s: %s has failed with the following error %s", self._target, message, str(e))
7 changes: 7 additions & 0 deletions custom_components/greenapi/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
notify:
description: "Send a whatsapp notification using Green API"
fields:
message:
description: The message to send
example: "Hello World"

0 comments on commit ed315f9

Please sign in to comment.