Skip to content

Commit

Permalink
Add service: Add entity to area (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Mar 10, 2023
1 parent 6e685f0 commit 31f6e05
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 23 additions & 2 deletions custom_components/spook/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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: >-
Expand Down
Original file line number Diff line number Diff line change
@@ -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"],
)

0 comments on commit 31f6e05

Please sign in to comment.