-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add service: Add entity to area (#54)
- Loading branch information
Showing
3 changed files
with
70 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
custom_components/spook/services/homeassistant_add_entity_to_area.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"], | ||
) |