diff --git a/homeassistant/auth/__init__.py b/homeassistant/auth/__init__.py index 23fdb775a8a..12511a7f4a5 100644 --- a/homeassistant/auth/__init__.py +++ b/homeassistant/auth/__init__.py @@ -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) diff --git a/tests/auth/test_init.py b/tests/auth/test_init.py index 53c2a4261ae..22d720da587 100644 --- a/tests/auth/test_init.py +++ b/tests/auth/test_init.py @@ -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