From 31d150794dbcc44752afe78ec82c4f80c2e4a24a Mon Sep 17 00:00:00 2001
From: brefra <frank_van_breugel@hotmail.com>
Date: Fri, 13 Mar 2020 08:09:30 +0100
Subject: [PATCH] Add memo text service (#31222)

* Add set_memo_text service

* Apply template rendering for memo text

* Update constants to comply to naming conventions

* Local variable for module address and extended error description

* fixed typo
---
 homeassistant/components/velbus/__init__.py   | 30 +++++++++++++++++--
 homeassistant/components/velbus/const.py      |  4 +++
 homeassistant/components/velbus/services.yaml | 16 ++++++++++
 3 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/homeassistant/components/velbus/__init__.py b/homeassistant/components/velbus/__init__.py
index b4fe49a88e7..72ffda48b57 100644
--- a/homeassistant/components/velbus/__init__.py
+++ b/homeassistant/components/velbus/__init__.py
@@ -6,13 +6,13 @@ import velbus
 import voluptuous as vol
 
 from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
-from homeassistant.const import CONF_NAME, CONF_PORT
+from homeassistant.const import CONF_ADDRESS, CONF_NAME, CONF_PORT
 from homeassistant.exceptions import ConfigEntryNotReady
 import homeassistant.helpers.config_validation as cv
 from homeassistant.helpers.entity import Entity
 from homeassistant.helpers.typing import HomeAssistantType
 
-from .const import DOMAIN
+from .const import CONF_MEMO_TEXT, DOMAIN, SERVICE_SET_MEMO_TEXT
 
 _LOGGER = logging.getLogger(__name__)
 
@@ -80,6 +80,32 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
 
     hass.services.async_register(DOMAIN, "sync_clock", syn_clock, schema=vol.Schema({}))
 
+    def set_memo_text(service):
+        """Handle Memo Text service call."""
+        module_address = service.data[CONF_ADDRESS]
+        memo_text = service.data[CONF_MEMO_TEXT]
+        memo_text.hass = hass
+        try:
+            controller.get_module(module_address).set_memo_text(
+                memo_text.async_render()
+            )
+        except velbus.util.VelbusException as err:
+            _LOGGER.error("An error occurred while setting memo text: %s", err)
+
+    hass.services.async_register(
+        DOMAIN,
+        SERVICE_SET_MEMO_TEXT,
+        set_memo_text,
+        vol.Schema(
+            {
+                vol.Required(CONF_ADDRESS): vol.All(
+                    vol.Coerce(int), vol.Range(min=0, max=255)
+                ),
+                vol.Optional(CONF_MEMO_TEXT, default=""): cv.template,
+            }
+        ),
+    )
+
     return True
 
 
diff --git a/homeassistant/components/velbus/const.py b/homeassistant/components/velbus/const.py
index 0d3a66fa743..d3987295fce 100644
--- a/homeassistant/components/velbus/const.py
+++ b/homeassistant/components/velbus/const.py
@@ -1,3 +1,7 @@
 """Const for Velbus."""
 
 DOMAIN = "velbus"
+
+CONF_MEMO_TEXT = "memo_text"
+
+SERVICE_SET_MEMO_TEXT = "set_memo_text"
diff --git a/homeassistant/components/velbus/services.yaml b/homeassistant/components/velbus/services.yaml
index 273cc8b4caa..ea31b951a18 100644
--- a/homeassistant/components/velbus/services.yaml
+++ b/homeassistant/components/velbus/services.yaml
@@ -1,2 +1,18 @@
 sync_clock:
   description: Sync the velbus modules clock to the Home Assistant clock, this is the same as the 'sync clock' from VelbusLink
+
+set_memo_text:
+  description: >
+    Set the memo text to the display of modules like VMBGPO, VMBGPOD
+    Be sure the page(s) of the module is configured to display the memo text.
+  fields:
+    address:
+      description: >
+        The module address in decimal format.
+        The decimal addresses are displayed in front of the modules listed at the integration page.
+      example: '11'
+    memo_text:
+      description: >
+        The actual text to be displayed.
+        Text is limited to 64 characters.
+      example: 'Do not forget trash'
\ No newline at end of file