I'm looking for some help with building a custom component for user access control in Home Assistant. Currently, the existing user login system in Home Assistant only allows users to be activated or deactivated via the UI with clicks. However, I need the ability to systematically activate/deactivate users via services that can be called in multiple ways, such as on a schedule or in a custom UI.
My python code in config/custom_component/user_access_control/init.py is
"""
Activate/Deactivate User Custom Component
"""
import logging
import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant.const import (
ATTR_ENTITY_ID,
SERVICE_TURN_ON,
SERVICE_TURN_OFF,
)
from homeassistant.core import callback
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.entity import Entity
DOMAIN = "user_access_control"
ENTITY_ID_FORMAT = DOMAIN + ".{}"
SERVICE_ACTIVATE_USER = "activate_user"
SERVICE_DEACTIVATE_USER = "deactivate_user"
ATTR_USERNAME = "username"
SERVICE_ACTIVATE_USER_SCHEMA = vol.Schema(
{
vol.Required(ATTR_USERNAME): cv.string,
}
)
SERVICE_DEACTIVATE_USER_SCHEMA = vol.Schema(
{
vol.Required(ATTR_USERNAME): cv.string,
}
)
_LOGGER = logging.getLogger(__name__)
async def async_setup(hass, config):
"""Set up the Activate/Deactivate User component."""
component = EntityComponent(_LOGGER, DOMAIN, hass)
await component.async_setup(config)
async def async_handle_activate_user_service(service_call):
"""Handle the activate user service."""
username = service_call.data.get(ATTR_USERNAME)
if username is not None:
user = hass.auth.async_get_user(username=username)
if user is not None:
await user.async_activate()
_LOGGER.info(f"{username} activated")
else:
_LOGGER.warning(f"No user found with username {username}")
else:
_LOGGER.warning(f"No username provided for {SERVICE_ACTIVATE_USER}")
async def async_handle_deactivate_user_service(service_call):
"""Handle the deactivate user service."""
username = service_call.data.get(ATTR_USERNAME)
if username is not None:
user = hass.auth.async_get_user(username=username)
if user is not None:
await user.async_deactivate()
_LOGGER.info(f"{username} deactivated")
else:
_LOGGER.warning(f"No user found with username {username}")
else:
_LOGGER.warning(f"No username provided for {SERVICE_DEACTIVATE_USER}")
hass.services.async_register(
DOMAIN,
SERVICE_ACTIVATE_USER,
async_handle_activate_user_service,
schema=SERVICE_ACTIVATE_USER_SCHEMA,
)
hass.services.async_register(
DOMAIN,
SERVICE_DEACTIVATE_USER,
async_handle_deactivate_user_service,
schema=SERVICE_DEACTIVATE_USER_SCHEMA,
)
return True
what should i write in configuration.yaml file so that two services
'user_access_control.activate_user' - takes a 'username' as an input and then activates that user.
'user_access_control.deactivate_user' - takes a 'username' as an input and then deactivates that user.
should show up in Developer Tools>Services UI
Help me out am new to Home Assistant
Am i doing correct or missing something?
I have writtren init.py but in configuration.yaml file when i write user user_access_control: It shows error in Developer Tools>Check and Restart>Check Configurations
I get Integration error: user_access_control - Integration 'user_access_control' not found.
What am i doing wrong