From 31f6e054f49938f0b529ef5ab12ce52537e094cf Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 10 Mar 2023 22:22:03 +0100 Subject: [PATCH] Add service: Add entity to area (#54) --- README.md | 6 +++ custom_components/spook/services.yaml | 25 ++++++++++- .../homeassistant_add_entity_to_area.py | 41 +++++++++++++++++++ 3 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 custom_components/spook/services/homeassistant_add_entity_to_area.py diff --git a/README.md b/README.md index 6ce716f4..6ad84818 100644 --- a/README.md +++ b/README.md @@ -297,6 +297,12 @@ Call it using: [`homeassistant.remove_device_from_area`](https://my.home-assista > Dynamicaly remove a device from an area. _#poef_ +## Service: Add entity to area + +Call it using: [`homeassistant.add_entity_to_area`](https://my.home-assistant.io/redirect/developer_call_service/?service=homeassistant.add_entity_to_area) + +> Dynamicaly add/move an entity to an area. _#bam_ + ## Service: Delete area Call it using: [`homeassistant.delete_area`](https://my.home-assistant.io/redirect/developer_call_service/?service=homeassistant.delete_area) diff --git a/custom_components/spook/services.yaml b/custom_components/spook/services.yaml index 49aa555c..3c11e544 100644 --- a/custom_components/spook/services.yaml +++ b/custom_components/spook/services.yaml @@ -53,7 +53,7 @@ homeassistant_add_device_to_area: area: device_id: name: Device ID - description: The ID of the device(s) to add the device to. + description: The ID of the device(s) to add to the area. required: true selector: device: @@ -62,7 +62,7 @@ homeassistant_add_device_to_area: homeassistant_remove_device_from_area: name: Remove a device from an area 👻 description: >- - Removes an device from an area. As a device can only be in one area, this + Removes a device from an area. As a device can only be in one area, this call doesn't need to specify the area. fields: device_id: @@ -73,6 +73,27 @@ homeassistant_remove_device_from_area: device: multiple: true +homeassistant_add_entity_to_area: + name: Add an entity to an area 👻 + description: >- + Adds an entity to an area. Please note, if the enity is already in an area, + it will be removed from the previous area. This will override the area + the device, that provides this entity, is in. + fields: + area_id: + name: Area ID + description: The ID of the area to add the entity to. + required: true + selector: + area: + entity_id: + name: Entity ID + description: The ID of the entity (or entities) to add to the area. + required: true + selector: + entity: + multiple: true + homeassistant_delete_area: name: Delete an area 👻 description: >- diff --git a/custom_components/spook/services/homeassistant_add_entity_to_area.py b/custom_components/spook/services/homeassistant_add_entity_to_area.py new file mode 100644 index 00000000..dbec1ba7 --- /dev/null +++ b/custom_components/spook/services/homeassistant_add_entity_to_area.py @@ -0,0 +1,41 @@ +"""Spook - Not 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 +from homeassistant.helpers import config_validation as cv +from homeassistant.helpers import entity_registry as er + +from . import AbstractSpookAdminService + +if TYPE_CHECKING: + from homeassistant.core import ServiceCall + + +class SpookService(AbstractSpookAdminService): + """Home Assistant service to add a entity to an area.""" + + domain = DOMAIN + service = "add_entity_to_area" + schema = { + vol.Required("area_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.""" + area_registry = ar.async_get(self.hass) + if not area_registry.async_get(call.data["area_id"]): + msg = f"Area {call.data['area_id']} not found" + raise HomeAssistantError(msg) + + entity_registry = er.async_get(self.hass) + for entity_id in call.data["entity_id"]: + entity_registry.async_update_entity( + entity_id, + area_id=call.data["area_id"], + )