diff --git a/homeassistant/components/velbus/__init__.py b/homeassistant/components/velbus/__init__.py index 66d9fef71a3..585e093a42c 100644 --- a/homeassistant/components/velbus/__init__.py +++ b/homeassistant/components/velbus/__init__.py @@ -1,7 +1,9 @@ """Support for Velbus devices.""" from __future__ import annotations +from contextlib import suppress import logging +import os import shutil from velbusaio.controller import Velbus @@ -19,6 +21,7 @@ from .const import ( CONF_INTERFACE, CONF_MEMO_TEXT, DOMAIN, + SERVICE_CLEAR_CACHE, SERVICE_SCAN, SERVICE_SET_MEMO_TEXT, SERVICE_SYNC, @@ -132,6 +135,42 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ), ) + async def clear_cache(call: ServiceCall) -> None: + """Handle a clear cache service call.""" + # clear the cache + with suppress(FileNotFoundError): + if call.data[CONF_ADDRESS]: + await hass.async_add_executor_job( + os.unlink, + hass.config.path( + STORAGE_DIR, + f"velbuscache-{call.data[CONF_INTERFACE]}/{call.data[CONF_ADDRESS]}.p", + ), + ) + else: + await hass.async_add_executor_job( + shutil.rmtree, + hass.config.path( + STORAGE_DIR, f"velbuscache-{call.data[CONF_INTERFACE]}/" + ), + ) + # call a scan to repopulate + await scan(call) + + hass.services.async_register( + DOMAIN, + SERVICE_CLEAR_CACHE, + clear_cache, + vol.Schema( + { + vol.Required(CONF_INTERFACE): vol.All(cv.string, check_entry_id), + vol.Optional(CONF_ADDRESS): vol.All( + vol.Coerce(int), vol.Range(min=0, max=255) + ), + } + ), + ) + return True @@ -149,4 +188,5 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hass.services.async_remove(DOMAIN, SERVICE_SCAN) hass.services.async_remove(DOMAIN, SERVICE_SYNC) hass.services.async_remove(DOMAIN, SERVICE_SET_MEMO_TEXT) + hass.services.async_remove(DOMAIN, SERVICE_CLEAR_CACHE) return unload_ok diff --git a/homeassistant/components/velbus/const.py b/homeassistant/components/velbus/const.py index 7c41274f11c..a3949646598 100644 --- a/homeassistant/components/velbus/const.py +++ b/homeassistant/components/velbus/const.py @@ -16,6 +16,7 @@ CONF_MEMO_TEXT: Final = "memo_text" SERVICE_SCAN: Final = "scan" SERVICE_SYNC: Final = "sync_clock" SERVICE_SET_MEMO_TEXT: Final = "set_memo_text" +SERVICE_CLEAR_CACHE: Final = "clear_cache" PRESET_MODES: Final = { PRESET_ECO: "safe", diff --git a/homeassistant/components/velbus/services.yaml b/homeassistant/components/velbus/services.yaml index 6dbf5e8cb47..32cda00f708 100644 --- a/homeassistant/components/velbus/services.yaml +++ b/homeassistant/components/velbus/services.yaml @@ -24,6 +24,29 @@ scan: selector: text: +clear_cache: + name: Clear cache + description: Clears the velbuscache and then starts a new scan + fields: + interface: + name: Interface + description: The velbus interface to send the command to, this will be the same value as used during configuration + required: true + example: "192.168.1.5:27015" + default: "" + selector: + text: + address: + name: Address + description: > + The module address in decimal format, if this is provided we only clear this module, if nothing is provided we clear the whole cache directory (all modules) + The decimal addresses are displayed in front of the modules listed at the integration page. + required: false + selector: + number: + min: 1 + max: 254 + set_memo_text: name: Set memo text description: >