├── hacs.json
├── custom_components
└── mysql_command
│ ├── __init__.py
│ ├── const.py
│ ├── manifest.json
│ └── notify.py
└── README.md
/hacs.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "MySQL Command",
3 | "render_readme": true
4 | }
5 |
--------------------------------------------------------------------------------
/custom_components/mysql_command/__init__.py:
--------------------------------------------------------------------------------
1 | """The mysql_command notify component."""
2 |
--------------------------------------------------------------------------------
/custom_components/mysql_command/const.py:
--------------------------------------------------------------------------------
1 | CONF_MYSQL_HOST = "host"
2 | CONF_MYSQL_PORT = "port"
3 | CONF_MYSQL_USERNAME = "username"
4 | CONF_MYSQL_PASSWORD = "password"
5 | CONF_MYSQL_DB = "db"
6 | CONF_MYSQL_TIMEOUT = "timeout"
7 |
8 | # Defaults
9 | DEFAULT_MYSQL_PORT = 3306
10 | DEFAULT_MYSQL_TIMEOUT = 10
11 |
--------------------------------------------------------------------------------
/custom_components/mysql_command/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "domain": "mysql_command",
3 | "name": "MySQL Command",
4 | "codeowners": ["@qrioniclabs"],
5 | "documentation": "https://github.com/qrioniclabs/homeassistant-mysql_command",
6 | "issue_tracker": "https://github.com/qrioniclabs/homeassistant-mysql_command/issues",
7 | "requirements": ["mysql-connector-python"],
8 | "iot_class": "local_push",
9 | "version": "1.2.0"
10 | }
11 |
--------------------------------------------------------------------------------
/custom_components/mysql_command/notify.py:
--------------------------------------------------------------------------------
1 | """Support for mysql_command notification."""
2 | from __future__ import annotations
3 |
4 | from .const import (
5 | CONF_MYSQL_HOST,
6 | CONF_MYSQL_PORT,
7 | CONF_MYSQL_USERNAME,
8 | CONF_MYSQL_PASSWORD,
9 | CONF_MYSQL_DB,
10 | CONF_MYSQL_TIMEOUT,
11 | DEFAULT_MYSQL_PORT,
12 | DEFAULT_MYSQL_TIMEOUT,
13 | )
14 |
15 | import voluptuous as vol
16 |
17 | from homeassistant.components.notify import (
18 | ATTR_TITLE,
19 | ATTR_TITLE_DEFAULT,
20 | PLATFORM_SCHEMA,
21 | BaseNotificationService,
22 | )
23 |
24 | from homeassistant.core import HomeAssistant
25 | import homeassistant.helpers.config_validation as cv
26 | from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
27 |
28 | import mysql.connector
29 |
30 | PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
31 | {
32 | vol.Required(CONF_MYSQL_HOST): cv.string,
33 | vol.Optional(CONF_MYSQL_PORT, default=DEFAULT_MYSQL_PORT): vol.Coerce(int),
34 | vol.Required(CONF_MYSQL_USERNAME): cv.string,
35 | vol.Required(CONF_MYSQL_PASSWORD): cv.string,
36 | vol.Required(CONF_MYSQL_DB): cv.string,
37 | vol.Optional(CONF_MYSQL_TIMEOUT, default=DEFAULT_MYSQL_TIMEOUT): vol.Coerce(int),
38 | }
39 | )
40 |
41 |
42 | def get_service(
43 | hass: HomeAssistant,
44 | config: ConfigType,
45 | discovery_info: DiscoveryInfoType | None = None,
46 | ) -> MySQLCommandNotificationService:
47 | """Get the mysql_command service."""
48 | host = config[CONF_MYSQL_HOST]
49 | port = config[CONF_MYSQL_PORT]
50 | username = config[CONF_MYSQL_USERNAME]
51 | password = config[CONF_MYSQL_PASSWORD]
52 | db = config[CONF_MYSQL_DB]
53 | timeout = config[CONF_MYSQL_TIMEOUT]
54 |
55 | return MySQLCommandNotificationService(host, port, username, password, db, timeout)
56 |
57 |
58 | class MySQLCommandNotificationService(BaseNotificationService):
59 | """Implement the notification service for the mysql_command service."""
60 |
61 |
62 | def __init__(self, host, port, username, password, db, timeout):
63 | """Initialize the service."""
64 | self.host = host
65 | self.port = port
66 | self.username = username
67 | self.password = password
68 | self.db = db
69 | self.timeout = timeout
70 |
71 |
72 | def send_message(self, message="", **kwargs):
73 | """Send a message as command to a MySQL server."""
74 | cnx = mysql.connector.connect(
75 | host=self.host,
76 | port=self.port,
77 | username=self.username,
78 | password=self.password,
79 | db=self.db,
80 | connection_timeout=self.timeout,
81 | )
82 | cursor = cnx.cursor(buffered=True) # (Why buffered=True? I don't have a clue...)
83 | cursor.execute(message)
84 |
85 | cnx.commit()
86 | cursor.close()
87 | cnx.close()
88 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [![HACS Custom][hacs_shield]][hacs]
2 | [![GitHub Latest Release][releases_shield]][latest_release]
3 | [![GitHub All Releases][downloads_total_shield]][releases]
4 | [![Community Forum][community_forum_shield]][community_forum]
5 |
6 | [hacs_shield]: https://img.shields.io/badge/HACS-Custom-41BDF5.svg?style=for-the-badge
7 | [hacs]: https://github.com/hacs/integration
8 |
9 | [latest_release]: https://github.com/qrioniclabs/homeassistant-mysql_command/releases/latest
10 | [releases_shield]: https://img.shields.io/github/release/qrioniclabs/homeassistant-mysql_command.svg?style=for-the-badge
11 |
12 | [releases]: https://github.com/qrioniclabs/home-assistant-mysql-command/releases/
13 | [downloads_total_shield]: https://img.shields.io/github/downloads/qrioniclabs/homeassistant-mysql_command/total?style=for-the-badge
14 |
15 | [community_forum_shield]: https://img.shields.io/static/v1.svg?label=%20&message=Forum&style=for-the-badge&color=41bdf5&logo=HomeAssistant&logoColor=white
16 | [community_forum]: https://community.home-assistant.io/t/mysql-command/539839
17 |
18 | # MySQL Command
19 | A Home Assistant custom component that creates a `notify` service to send a command to a MySQL server.
20 |
21 | Forum Topic: https://community.home-assistant.io/t/mysql-command/539839
22 |
23 | ## Features
24 | - Send commands to a MySQL server using the Notify platform / service
25 |
26 | ## Installation
27 |
28 | ### Using [HACS](https://hacs.xyz/)
29 | This component can be installed using HACS. Please follow directions [here](https://hacs.xyz/docs/faq/custom_repositories/) and use [https://github.com/qrioniclabs/homeassistant-mysql_command](https://github.com/qrioniclabs/homeassistant-mysql_command) as the repository URL.
30 |
31 | ### Manual
32 | - Copy directory `custom_components/mysql_command` to your `/custom_components` directory
33 | - Configure with config below
34 | - Restart Home Assistant
35 |
36 | ## Configuration
37 | ### YAML
38 | In configuration.yaml:
39 | ```yaml
40 | notify:
41 | - name: mysql_command_example_db
42 | platform: mysql_command
43 | host: YourHostnameOrIP
44 | username: YourUser
45 | password: YourPassword
46 | db: YourDB
47 | ```
48 |
49 | Then, use the service like so:
50 | ```yaml
51 | - service: notify.mysql_command_example_db
52 | data_template:
53 | message: >
54 | INSERT INTO `table` (column1, column2, column3) VALUES ('value1', 'value2', 'value3');
55 | ```
56 |
57 | Here is an example with a template timestamp:
58 | ```yaml
59 | - service: notify.mysql_command_example_db
60 | data_template:
61 | message: >
62 | INSERT INTO `table` (datetime, column1, column2) VALUES ('{{ now().timestamp() | timestamp_custom('%Y-%m-%d %H:%M:%S') }}', 'value1', 'value2');
63 | ```
64 |
65 | ### Available configuration parameters
66 | | Key | Type | Required | Value | Description |
67 | |---|---|---|---|---|
68 | | `platform` | string | true | `mysql_command` | Name of a platform |
69 | | `host` | string | true | `192.168.1.123` | Hostname or IP address of MySQL server |
70 | | `username` | string | true | `example_user` | MySQL user with access to the database |
71 | | `password` | string | true | `aVerySecretPassword` | Password for the MySQL user |
72 | | `db` | string | true | `example_db` | The database that the command is sent to |
73 | | `port` | int | false | `3307` | The TCP/IP port of the MySQL server. Default: 3306 |
74 | | `timeout` | int | false | `30` | Timeout (in seconds) for the database connection. Default: 10 |
75 |
76 | ## Special thanks
77 | - Inspired by https://community.home-assistant.io/t/how-do-i-call-an-insert-sql-command-to-mariadb-addon/
78 | - First steps taken with the help of [@mikey0000](https://github.com/mikey0000) via HA Discord
79 |
--------------------------------------------------------------------------------