mirror of
https://github.com/home-assistant/core.git
synced 2026-12-02 19:57:03 -04:00
Add action enums to minio (#190633)
This commit is contained in:
@@ -1,28 +1,48 @@
|
||||
"""Services for the Minio integration."""
|
||||
|
||||
from enum import StrEnum
|
||||
|
||||
import probatio
|
||||
|
||||
from homeassistant.core import HomeAssistant, ServiceCall
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
|
||||
from .const import ATTR_BUCKET, ATTR_KEY, DATA_MINIO, DOMAIN
|
||||
from .const import DATA_MINIO, DOMAIN
|
||||
|
||||
|
||||
class MinioService(StrEnum):
|
||||
"""Store keys for Minio services."""
|
||||
|
||||
GET = "get"
|
||||
PUT = "put"
|
||||
REMOVE = "remove"
|
||||
|
||||
|
||||
class MinioServiceArgument(StrEnum):
|
||||
"""Store keys for Minio service arguments."""
|
||||
|
||||
BUCKET = "bucket"
|
||||
KEY = "key"
|
||||
FILE_PATH = "file_path"
|
||||
|
||||
ATTR_FILE_PATH = "file_path"
|
||||
|
||||
BUCKET_KEY_SCHEMA = probatio.Schema(
|
||||
{probatio.Required(ATTR_BUCKET): cv.string, probatio.Required(ATTR_KEY): cv.string}
|
||||
{
|
||||
probatio.Required(MinioServiceArgument.BUCKET): cv.string,
|
||||
probatio.Required(MinioServiceArgument.KEY): cv.string,
|
||||
}
|
||||
)
|
||||
|
||||
BUCKET_KEY_FILE_SCHEMA = BUCKET_KEY_SCHEMA.extend(
|
||||
{probatio.Required(ATTR_FILE_PATH): cv.string}
|
||||
{probatio.Required(MinioServiceArgument.FILE_PATH): cv.string}
|
||||
)
|
||||
|
||||
|
||||
def _put_file(call: ServiceCall) -> None:
|
||||
"""Upload file service."""
|
||||
bucket = call.data[ATTR_BUCKET]
|
||||
key = call.data[ATTR_KEY]
|
||||
file_path = call.data[ATTR_FILE_PATH]
|
||||
bucket = call.data[MinioServiceArgument.BUCKET]
|
||||
key = call.data[MinioServiceArgument.KEY]
|
||||
file_path = call.data[MinioServiceArgument.FILE_PATH]
|
||||
|
||||
if not call.hass.config.is_allowed_path(file_path):
|
||||
raise ValueError(f"Invalid file_path {file_path}")
|
||||
@@ -32,9 +52,9 @@ def _put_file(call: ServiceCall) -> None:
|
||||
|
||||
def _get_file(call: ServiceCall) -> None:
|
||||
"""Download file service."""
|
||||
bucket = call.data[ATTR_BUCKET]
|
||||
key = call.data[ATTR_KEY]
|
||||
file_path = call.data[ATTR_FILE_PATH]
|
||||
bucket = call.data[MinioServiceArgument.BUCKET]
|
||||
key = call.data[MinioServiceArgument.KEY]
|
||||
file_path = call.data[MinioServiceArgument.FILE_PATH]
|
||||
|
||||
if not call.hass.config.is_allowed_path(file_path):
|
||||
raise ValueError(f"Invalid file_path {file_path}")
|
||||
@@ -44,14 +64,20 @@ def _get_file(call: ServiceCall) -> None:
|
||||
|
||||
def _remove_file(call: ServiceCall) -> None:
|
||||
"""Delete file service."""
|
||||
bucket = call.data[ATTR_BUCKET]
|
||||
key = call.data[ATTR_KEY]
|
||||
bucket = call.data[MinioServiceArgument.BUCKET]
|
||||
key = call.data[MinioServiceArgument.KEY]
|
||||
|
||||
call.hass.data[DATA_MINIO].remove_object(bucket, key)
|
||||
|
||||
|
||||
def setup_services(hass: HomeAssistant) -> None:
|
||||
"""Register the Minio services."""
|
||||
hass.services.register(DOMAIN, "put", _put_file, schema=BUCKET_KEY_FILE_SCHEMA)
|
||||
hass.services.register(DOMAIN, "get", _get_file, schema=BUCKET_KEY_FILE_SCHEMA)
|
||||
hass.services.register(DOMAIN, "remove", _remove_file, schema=BUCKET_KEY_SCHEMA)
|
||||
hass.services.register(
|
||||
DOMAIN, MinioService.PUT, _put_file, schema=BUCKET_KEY_FILE_SCHEMA
|
||||
)
|
||||
hass.services.register(
|
||||
DOMAIN, MinioService.GET, _get_file, schema=BUCKET_KEY_FILE_SCHEMA
|
||||
)
|
||||
hass.services.register(
|
||||
DOMAIN, MinioService.REMOVE, _remove_file, schema=BUCKET_KEY_SCHEMA
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user