From 3195183854117e584dbffad86e441064528dc891 Mon Sep 17 00:00:00 2001 From: Claudiu Bucur Date: Wed, 18 Oct 2023 13:54:18 +0300 Subject: [PATCH] Make "buffered" optional Allow using query parameters via the "target" key Add some logging info --- custom_components/mysql_command/const.py | 2 ++ custom_components/mysql_command/notify.py | 25 +++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/custom_components/mysql_command/const.py b/custom_components/mysql_command/const.py index 466459b..bc3948c 100644 --- a/custom_components/mysql_command/const.py +++ b/custom_components/mysql_command/const.py @@ -1,3 +1,4 @@ +CONF_MYSQL_BUFFERED = "buffered" CONF_MYSQL_HOST = "host" CONF_MYSQL_PORT = "port" CONF_MYSQL_USERNAME = "username" @@ -6,5 +7,6 @@ CONF_MYSQL_TIMEOUT = "timeout" # Defaults +DEFAULT_MYSQL_BUFFERED = True DEFAULT_MYSQL_PORT = 3306 DEFAULT_MYSQL_TIMEOUT = 10 diff --git a/custom_components/mysql_command/notify.py b/custom_components/mysql_command/notify.py index d88f696..7722335 100644 --- a/custom_components/mysql_command/notify.py +++ b/custom_components/mysql_command/notify.py @@ -2,21 +2,23 @@ from __future__ import annotations from .const import ( + CONF_MYSQL_BUFFERED, CONF_MYSQL_HOST, CONF_MYSQL_PORT, CONF_MYSQL_USERNAME, CONF_MYSQL_PASSWORD, CONF_MYSQL_DB, CONF_MYSQL_TIMEOUT, + DEFAULT_MYSQL_BUFFERED, DEFAULT_MYSQL_PORT, DEFAULT_MYSQL_TIMEOUT, ) import voluptuous as vol +import logging from homeassistant.components.notify import ( - ATTR_TITLE, - ATTR_TITLE_DEFAULT, + ATTR_TARGET, PLATFORM_SCHEMA, BaseNotificationService, ) @@ -27,6 +29,8 @@ import mysql.connector +_LOGGER = logging.getLogger(__name__) + PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( { vol.Required(CONF_MYSQL_HOST): cv.string, @@ -35,6 +39,7 @@ vol.Required(CONF_MYSQL_PASSWORD): cv.string, vol.Required(CONF_MYSQL_DB): cv.string, vol.Optional(CONF_MYSQL_TIMEOUT, default=DEFAULT_MYSQL_TIMEOUT): vol.Coerce(int), + vol.Optional(CONF_MYSQL_BUFFERED, default=DEFAULT_MYSQL_BUFFERED): cv.boolean, } ) @@ -51,15 +56,16 @@ def get_service( password = config[CONF_MYSQL_PASSWORD] db = config[CONF_MYSQL_DB] timeout = config[CONF_MYSQL_TIMEOUT] + buffered = config[CONF_MYSQL_BUFFERED] - return MySQLCommandNotificationService(host, port, username, password, db, timeout) + return MySQLCommandNotificationService(host, port, username, password, db, timeout, buffered) class MySQLCommandNotificationService(BaseNotificationService): """Implement the notification service for the mysql_command service.""" - def __init__(self, host, port, username, password, db, timeout): + def __init__(self, host, port, username, password, db, timeout, buffered): """Initialize the service.""" self.host = host self.port = port @@ -67,6 +73,7 @@ def __init__(self, host, port, username, password, db, timeout): self.password = password self.db = db self.timeout = timeout + self.buffered = buffered def send_message(self, message="", **kwargs): @@ -79,9 +86,15 @@ def send_message(self, message="", **kwargs): db=self.db, connection_timeout=self.timeout, ) - cursor = cnx.cursor(buffered=True) # (Why buffered=True? I don't have a clue...) - cursor.execute(message) + cursor = cnx.cursor(buffered=self.buffered) + if ATTR_TARGET in kwargs: + _LOGGER.debug("Executing query: '%s' with target '%s'", message, tuple(kwargs[ATTR_TARGET])) + cursor.execute(message, tuple(kwargs[ATTR_TARGET])) + else: + _LOGGER.debug("Executing query: '%s'", message) + cursor.execute(message) cnx.commit() + _LOGGER.debug("%d row(s) affected", cursor.rowcount) cursor.close() cnx.close()