From ed315f98537b228f17ba1c3f4bb0f3f7d879e563 Mon Sep 17 00:00:00 2001 From: Tomer Klein Date: Mon, 1 Apr 2024 16:52:25 +0300 Subject: [PATCH] First commit --- custom_components/greenapi/__init__.py | 1 + custom_components/greenapi/manifest.json | 12 +++++++ custom_components/greenapi/notify.py | 46 ++++++++++++++++++++++++ custom_components/greenapi/services.yaml | 7 ++++ 4 files changed, 66 insertions(+) create mode 100644 custom_components/greenapi/__init__.py create mode 100644 custom_components/greenapi/manifest.json create mode 100644 custom_components/greenapi/notify.py create mode 100644 custom_components/greenapi/services.yaml diff --git a/custom_components/greenapi/__init__.py b/custom_components/greenapi/__init__.py new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/custom_components/greenapi/__init__.py @@ -0,0 +1 @@ + diff --git a/custom_components/greenapi/manifest.json b/custom_components/greenapi/manifest.json new file mode 100644 index 0000000..ea2e67d --- /dev/null +++ b/custom_components/greenapi/manifest.json @@ -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" +} diff --git a/custom_components/greenapi/notify.py b/custom_components/greenapi/notify.py new file mode 100644 index 0000000..cb60b20 --- /dev/null +++ b/custom_components/greenapi/notify.py @@ -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)) \ No newline at end of file diff --git a/custom_components/greenapi/services.yaml b/custom_components/greenapi/services.yaml new file mode 100644 index 0000000..c767744 --- /dev/null +++ b/custom_components/greenapi/services.yaml @@ -0,0 +1,7 @@ +notify: + description: "Send a whatsapp notification using Green API" + fields: + message: + description: The message to send + example: "Hello World" + \ No newline at end of file