Skip to content

Commit

Permalink
initial work
Browse files Browse the repository at this point in the history
  • Loading branch information
baldarn committed Jan 26, 2024
0 parents commit 5612411
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/hacs.yml
Original file line number Diff line number Diff line change
@@ -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"
15 changes: 15 additions & 0 deletions .github/workflows/hassfest.yml
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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: [email protected] # 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: [email protected] # WhatsApp chat id
```
1 change: 1 addition & 0 deletions custom_components/whatsapper/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

10 changes: 10 additions & 0 deletions custom_components/whatsapper/manifest.json
Original file line number Diff line number Diff line change
@@ -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"
}
57 changes: 57 additions & 0 deletions custom_components/whatsapper/notify.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions custom_components/whatsapper/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
notify:
description: "Send a notification using Whatsapper"
fields:
message:
description: The message to send
example: "Hello World"
5 changes: 5 additions & 0 deletions hacs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Whatsapper notification",
"render_readme": true,
"iot_class": "local_push"
}

0 comments on commit 5612411

Please sign in to comment.