Skip to content

Commit

Permalink
Add service: Remove device from area (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Mar 10, 2023
1 parent 93e8d3e commit 6e685f0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ Call it using: [`homeassistant.add_device_to_area`](https://my.home-assistant.io

> Dynamicaly add/move a device to an new area. _#moveit_
## Service: Remove device from area

Call it using: [`homeassistant.remove_device_from_area`](https://my.home-assistant.io/redirect/developer_call_service/?service=homeassistant.remove_device_from_area)

> Dynamicaly remove a device from an area. _#poef_
## Service: Delete area

Call it using: [`homeassistant.delete_area`](https://my.home-assistant.io/redirect/developer_call_service/?service=homeassistant.delete_area)
Expand Down
16 changes: 15 additions & 1 deletion custom_components/spook/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,21 @@ homeassistant_add_device_to_area:
area:
device_id:
name: Device ID
description: The ID of the area to add the device to.
description: The ID of the device(s) to add the device to.
required: true
selector:
device:
multiple: true

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
call doesn't need to specify the area.
fields:
device_id:
name: Device ID
description: The ID of the device to remove the area from.
required: true
selector:
device:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""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.helpers import config_validation as cv
from homeassistant.helpers import device_registry as dr

from . import AbstractSpookAdminService

if TYPE_CHECKING:
from homeassistant.core import ServiceCall


class SpookService(AbstractSpookAdminService):
"""Home Assistant service to remove a device from an area."""

domain = DOMAIN
service = "remove_device_from_area"
schema = {
vol.Required("device_id"): vol.All(cv.ensure_list, [cv.string]),
}

async def async_handle_service(self, call: ServiceCall) -> None:
"""Handle the service call."""
device_registry = dr.async_get(self.hass)
for device_id in call.data["device_id"]:
device_registry.async_update_device(
device_id,
area_id=None,
)

0 comments on commit 6e685f0

Please sign in to comment.