From a3183cab4ef9249a73b51a1867df0a9728a8e03f Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 30 Mar 2024 21:17:13 +0100 Subject: [PATCH] Add homeassistant.add_area_to_floor service --- .../services/add_area_to_floor.py | 45 +++++++++++++++++++ custom_components/spook/services.yaml | 20 +++++++++ 2 files changed, 65 insertions(+) create mode 100644 custom_components/spook/ectoplasms/homeassistant/services/add_area_to_floor.py diff --git a/custom_components/spook/ectoplasms/homeassistant/services/add_area_to_floor.py b/custom_components/spook/ectoplasms/homeassistant/services/add_area_to_floor.py new file mode 100644 index 00000000..fc586c07 --- /dev/null +++ b/custom_components/spook/ectoplasms/homeassistant/services/add_area_to_floor.py @@ -0,0 +1,45 @@ +"""Spook - Your homie.""" + +from __future__ import annotations + +from typing import TYPE_CHECKING + +import voluptuous as vol + +from homeassistant.components.homeassistant import DOMAIN +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers import ( + area_registry as ar, + config_validation as cv, + floor_registry as fr, +) + +from ....services import AbstractSpookAdminService + +if TYPE_CHECKING: + from homeassistant.core import ServiceCall + + +class SpookService(AbstractSpookAdminService): + """Home Assistant service to add an area to a floor.""" + + domain = DOMAIN + service = "add_area_to_floor" + schema = { + vol.Required("floor_id"): cv.string, + vol.Required("entity_id"): vol.All(cv.ensure_list, [cv.string]), + } + + async def async_handle_service(self, call: ServiceCall) -> None: + """Handle the service call.""" + floor_registry = fr.async_get(self.hass) + if not floor_registry.async_get_floor(call.data["floor_id"]): + msg = f"Floor {call.data['floor_id']} not found" + raise HomeAssistantError(msg) + + area_registry = ar.async_get(self.hass) + for area_id in call.data["area_id"]: + area_registry.async_update( + area_id=area_id, + floor_id=call.data["floor_id"], + ) diff --git a/custom_components/spook/services.yaml b/custom_components/spook/services.yaml index 1ca4778a..8c8901af 100644 --- a/custom_components/spook/services.yaml +++ b/custom_components/spook/services.yaml @@ -390,6 +390,26 @@ homeassistant_set_floor_aliases: selector: object: +homeassistant_add_area_to_floor: + name: Add an area to a floor 👻 + description: >- + Adds an area to a floor. Please note, if the area is already on a floor, + it will be removed from the previous floor. + fields: + floor_id: + name: Floor ID + description: The ID of the floor to add the area on. + required: true + selector: + text: + area_id: + name: Area ID + description: The ID of the area(s) to add to the floor. + required: true + selector: + area: + multiple: true + homeassistant_ignore_all_discovered: name: Ignore all currently discovered devices 👻 description: >-