Add EVENT_USER_UPDATED (#71965)

This commit is contained in:
J. Nick Koston 2022-06-09 17:49:02 -10:00 committed by GitHub
parent 2f106112df
commit 211e5432ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -20,6 +20,7 @@ from .mfa_modules import MultiFactorAuthModule, auth_mfa_module_from_config
from .providers import AuthProvider, LoginFlow, auth_provider_from_config
EVENT_USER_ADDED = "user_added"
EVENT_USER_UPDATED = "user_updated"
EVENT_USER_REMOVED = "user_removed"
_MfaModuleDict = dict[str, MultiFactorAuthModule]
@ -338,6 +339,8 @@ class AuthManager:
else:
await self.async_deactivate_user(user)
self.hass.bus.async_fire(EVENT_USER_UPDATED, {"user_id": user.id})
async def async_activate_user(self, user: models.User) -> None:
"""Activate a user."""
await self._store.async_activate_user(user)

View File

@ -8,6 +8,7 @@ import voluptuous as vol
from homeassistant import auth, data_entry_flow
from homeassistant.auth import (
EVENT_USER_UPDATED,
InvalidAuthError,
auth_store,
const as auth_const,
@ -1097,3 +1098,20 @@ async def test_rename_does_not_change_refresh_token(mock_hass):
token_after = list(user.refresh_tokens.values())[0]
assert token_before == token_after
async def test_event_user_updated_fires(hass):
"""Test the user updated event fires."""
manager = await auth.auth_manager_from_config(hass, [], [])
user = MockUser().add_to_auth_manager(manager)
await manager.async_create_refresh_token(user, CLIENT_ID)
assert len(list(user.refresh_tokens.values())) == 1
events = async_capture_events(hass, EVENT_USER_UPDATED)
await manager.async_update_user(user, name="new name")
assert user.name == "new name"
await hass.async_block_till_done()
assert len(events) == 1