-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Add RFID token management services to Peblar integration #169189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
syphernl
wants to merge
13
commits into
home-assistant:dev
Choose a base branch
from
syphernl:feature/peblar-rfid-services
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7ef8bd2
Add RFID token management services to Peblar integration
syphernl a0bdfd4
Address PR review: validate config entry state, add services.yaml, fi…
syphernl 702728b
refactor(peblar): register RFID services in async_setup
syphernl 51fbad7
Merge branch 'dev' into feature/peblar-rfid-services
syphernl d1a7d18
refactor(peblar): address frenck review on RFID services
syphernl 85ea7cc
wip
syphernl 3f92e24
wip
syphernl 6b65d53
Merge branch 'dev' into feature/peblar-rfid-services
syphernl dd234a1
Address PR review: admin service and error message fixes
syphernl cc0c142
Merge branch 'dev' into feature/peblar-rfid-services
syphernl af029a3
Address PR review: service deregistration and CONF_UID constant
syphernl 0b59039
fix(peblar): re-register services in async_setup_entry to survive reload
syphernl 31a73f2
Merge branch 'dev' into feature/peblar-rfid-services
syphernl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,130 @@ | ||
| """Services for the Peblar integration.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import cast | ||
|
|
||
| from peblar import Peblar | ||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.config_entries import ConfigEntryState | ||
| from homeassistant.const import ATTR_CONFIG_ENTRY_ID, CONF_DESCRIPTION | ||
| from homeassistant.core import ( | ||
| HomeAssistant, | ||
| ServiceCall, | ||
| ServiceResponse, | ||
| SupportsResponse, | ||
| callback, | ||
| ) | ||
| from homeassistant.exceptions import ServiceValidationError | ||
| from homeassistant.helpers.service import async_register_admin_service | ||
| from homeassistant.util.json import JsonValueType | ||
|
|
||
| from .const import CONF_UID, DOMAIN | ||
| from .coordinator import PeblarConfigEntry | ||
|
|
||
| LIST_RESPONSE_SCHEMA = vol.Schema( | ||
| { | ||
| "tokens": [ | ||
| vol.Schema( | ||
| { | ||
| "uid": str, | ||
| "description": str, | ||
| } | ||
| ) | ||
| ] | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def _get_peblar(hass: HomeAssistant, entry_id: str) -> Peblar: | ||
| entry = hass.config_entries.async_get_entry(entry_id) | ||
| if ( | ||
| entry is None | ||
| or entry.domain != DOMAIN | ||
| or entry.state is not ConfigEntryState.LOADED | ||
| ): | ||
| raise ServiceValidationError( | ||
| translation_domain=DOMAIN, | ||
| translation_key="invalid_config_entry", | ||
| translation_placeholders={ATTR_CONFIG_ENTRY_ID: entry_id}, | ||
| ) | ||
| return cast( | ||
| PeblarConfigEntry, entry | ||
| ).runtime_data.user_configuration_coordinator.peblar | ||
|
|
||
|
|
||
| @callback | ||
| def async_setup_services(hass: HomeAssistant) -> None: | ||
| """Register RFID management services.""" | ||
|
|
||
| async def _handle_list_rfid_tokens(call: ServiceCall) -> ServiceResponse: | ||
| peblar = _get_peblar(hass, call.data[ATTR_CONFIG_ENTRY_ID]) | ||
| tokens = await peblar.rfid_tokens() | ||
| return cast( | ||
| dict[str, JsonValueType], | ||
| LIST_RESPONSE_SCHEMA( | ||
| { | ||
| "tokens": [ | ||
| { | ||
| "uid": t.rfid_token_uid, | ||
| CONF_DESCRIPTION: t.rfid_token_description, | ||
| } | ||
| for t in tokens | ||
| ] | ||
| } | ||
| ), | ||
| ) | ||
|
|
||
| async def _handle_add_rfid_token(call: ServiceCall) -> None: | ||
| peblar = _get_peblar(hass, call.data[ATTR_CONFIG_ENTRY_ID]) | ||
| await peblar.add_rfid_token( | ||
| rfid_token_uid=call.data[CONF_UID], | ||
| rfid_token_description=call.data[CONF_DESCRIPTION], | ||
| ) | ||
|
|
||
| async def _handle_delete_rfid_token(call: ServiceCall) -> None: | ||
| peblar = _get_peblar(hass, call.data[ATTR_CONFIG_ENTRY_ID]) | ||
| await peblar.delete_rfid_token(uid=call.data[CONF_UID]) | ||
|
|
||
| async_register_admin_service( | ||
| hass, | ||
| DOMAIN, | ||
| "list_rfid_tokens", | ||
| _handle_list_rfid_tokens, | ||
| schema=vol.Schema({vol.Required(ATTR_CONFIG_ENTRY_ID): str}), | ||
| supports_response=SupportsResponse.ONLY, | ||
| ) | ||
| async_register_admin_service( | ||
| hass, | ||
| DOMAIN, | ||
| "add_rfid_token", | ||
| _handle_add_rfid_token, | ||
| schema=vol.Schema( | ||
| { | ||
| vol.Required(ATTR_CONFIG_ENTRY_ID): str, | ||
| vol.Required(CONF_UID): str, | ||
| vol.Required(CONF_DESCRIPTION): str, | ||
| } | ||
| ), | ||
| ) | ||
| async_register_admin_service( | ||
| hass, | ||
| DOMAIN, | ||
| "delete_rfid_token", | ||
| _handle_delete_rfid_token, | ||
| schema=vol.Schema( | ||
| { | ||
| vol.Required(ATTR_CONFIG_ENTRY_ID): str, | ||
| vol.Required(CONF_UID): str, | ||
| } | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| @callback | ||
| def async_unload_services(hass: HomeAssistant) -> None: | ||
| """Unregister RFID management services.""" | ||
| hass.services.async_remove(DOMAIN, "list_rfid_tokens") | ||
| hass.services.async_remove(DOMAIN, "add_rfid_token") | ||
| hass.services.async_remove(DOMAIN, "delete_rfid_token") | ||
This file contains hidden or 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,35 @@ | ||
| list_rfid_tokens: | ||
| fields: | ||
| config_entry_id: | ||
| required: true | ||
| selector: | ||
| config_entry: | ||
| integration: peblar | ||
|
|
||
| add_rfid_token: | ||
| fields: | ||
| config_entry_id: | ||
| required: true | ||
| selector: | ||
| config_entry: | ||
| integration: peblar | ||
| uid: | ||
| required: true | ||
| selector: | ||
| text: | ||
| description: | ||
| required: true | ||
| selector: | ||
| text: | ||
|
|
||
| delete_rfid_token: | ||
| fields: | ||
| config_entry_id: | ||
| required: true | ||
| selector: | ||
| config_entry: | ||
| integration: peblar | ||
| uid: | ||
| required: true | ||
| selector: | ||
| text: | ||
|
syphernl marked this conversation as resolved.
|
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.