diff --git a/.github/workflows/hacs.yml b/.github/workflows/hacs.yml new file mode 100644 index 0000000..9664c5f --- /dev/null +++ b/.github/workflows/hacs.yml @@ -0,0 +1,19 @@ +name: HACS Validation + +on: + workflow_dispatch: + push: + pull_request: + schedule: + - cron: "0 0 * * *" + + +jobs: + validate: + runs-on: "ubuntu-latest" + steps: + - uses: "actions/checkout@v2" + - name: HACS validation + uses: "hacs/action@main" + with: + category: "integration" diff --git a/.github/workflows/hassfest.yml b/.github/workflows/hassfest.yml new file mode 100644 index 0000000..8d766f6 --- /dev/null +++ b/.github/workflows/hassfest.yml @@ -0,0 +1,15 @@ +name: Validate with hassfest + +on: + workflow_dispatch: + push: + pull_request: + schedule: + - cron: "0 0 * * *" + +jobs: + validate: + runs-on: "ubuntu-latest" + steps: + - uses: "actions/checkout@v2" + - uses: home-assistant/actions/hassfest@master diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7d9d7f1 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 baldarn + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..2e3f292 --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +[![hacs_badge](https://img.shields.io/badge/HACS-Default-41BDF5.svg)](https://github.com/hacs/integration) + +# Whatsapper integration + +TODO descr + +## Getting started + +Install Whatsapper add on. + +You can find that in [alexbelgium](https://github.com/alexbelgium/hassio-addons) + +### Installation +Whatsapper is now part ot the Default HACS repositories list. +Just add it from the Integrations list. + +### Configuration +To use Whatsapper notifications edit the `configuration.yaml` file. + +```yaml +notify: + - platform: whatsapper + name: some name that you want + chat_id: 123123123@g.us # WhatsApp chat id (see below) +``` + +#### Get chat id + +To get all you chat ids, go on the web browser and call: + +```url +http://192.168.1.123:4000/chats +``` + +Search and find the chat where you want to send messages and get it's id + +### Whatsapper host_port configuration + +If your whatsapper instance is somewhere else, you can specify that: + +```yaml +notify: + - platform: whatsapper + name: some name that you want + host_port: 192.168.1.123:4000 # host:port of the whatsapper instance + chat_id: 123123123@g.us # WhatsApp chat id +``` diff --git a/custom_components/whatsapper/__init__.py b/custom_components/whatsapper/__init__.py new file mode 100644 index 0000000..8d1c8b6 --- /dev/null +++ b/custom_components/whatsapper/__init__.py @@ -0,0 +1 @@ + diff --git a/custom_components/whatsapper/manifest.json b/custom_components/whatsapper/manifest.json new file mode 100644 index 0000000..1e33ede --- /dev/null +++ b/custom_components/whatsapper/manifest.json @@ -0,0 +1,10 @@ +{ + "domain": "whatsapper", + "name": "Whatsapper integration", + "codeowners": ["baldarn"], + "documentation": "https://github.com/baldarn/whatsapper-ha-integration", + "iot_class": "local_push", + "issue_tracker": "https://github.com/baldarn/whatsapper-ha-integration/issues", + "requirements": [], + "version": "2024.1.26" +} diff --git a/custom_components/whatsapper/notify.py b/custom_components/whatsapper/notify.py new file mode 100644 index 0000000..b295c6d --- /dev/null +++ b/custom_components/whatsapper/notify.py @@ -0,0 +1,57 @@ +"""Whatsapper platform for notify component.""" +from __future__ import annotations + +import logging + +import voluptuous as vol + +import requests + +from homeassistant.components.notify import ( + PLATFORM_SCHEMA, + BaseNotificationService, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType + + +_LOGGER = logging.getLogger(__name__) + +HOST_PORT = "host_port" +CONF_CHAT_ID = "chat_id" + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({vol.Required(CONF_CHAT_ID): vol.Coerce(str)}) + +def get_service( + hass: HomeAssistant, + config: ConfigType, + discovery_info: DiscoveryInfoType | None = None, +) -> WhatsapperNotificationService: + """Get the Whatsapper notification service.""" + + chat_id = config.get(CONF_CHAT_ID) + host_port = config.get(HOST_PORT) + + if host_port is None: + host_port = "localhost:4000" + + return WhatsapperNotificationService(hass, chat_id, host_port) + +class WhatsapperNotificationService(BaseNotificationService): + + def __init__(self, hass, chat_id, host_port): + """Initialize the service.""" + self.chat_id = chat_id + self.host_port = host_port + self.hass = hass + + def send_message(self, message="", **kwargs): + """Send a message to the target.""" + try: + # Actually send the message + url = f'http://{self.host_port}/command' + body = {"command":"sendMessage", "params":[self.chat_id, message]} + resp = requests.post(url, json = body) + print(resp.text) + except Exception as e: + _LOGGER.error("Sending to %s failed: %s", self._chat_id, e) diff --git a/custom_components/whatsapper/services.yaml b/custom_components/whatsapper/services.yaml new file mode 100644 index 0000000..df18671 --- /dev/null +++ b/custom_components/whatsapper/services.yaml @@ -0,0 +1,6 @@ +notify: + description: "Send a notification using Whatsapper" + fields: + message: + description: The message to send + example: "Hello World" diff --git a/hacs.json b/hacs.json new file mode 100644 index 0000000..92ac703 --- /dev/null +++ b/hacs.json @@ -0,0 +1,5 @@ +{ + "name": "Whatsapper notification", + "render_readme": true, + "iot_class": "local_push" +}