Add a new clear cache service to the velbus integration (#79995)

This commit is contained in:
Maikel Punie 2022-10-26 16:44:24 +02:00 committed by GitHub
parent e18adb379d
commit 33ef2abf9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 0 deletions

View File

@ -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

View File

@ -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",

View File

@ -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: >