I’m trying to create a custom component on my HA docker
this is the dir hierarchy:
custom_components/
└── person_creator/
├── init.py
├── manifest.json
└── services.yaml
init.py:
from homeassistant.core import HomeAssistant, ServiceCall
from homeassistant.helpers.service import async_register_admin_service
from homeassistant.components.http import HomeAssistantView
import homeassistant.helpers.config_validation as cv
import voluptuous as vol
import logging, uuid, hashlib
DOMAIN = 'person_creator'
_LOGGER = logging.getLogger(__name__)
SERVICE_CREATE_PERSON = 'create_person'
ATTR_NAME = 'name'
PERSON_SCHEMA = vol.Schema({
vol.Required(ATTR_NAME): cv.string,
})
class CreatePersonView(HomeAssistantView):
"""Handle HTTP requests to create a new person."""
url = "/api/person_creator/create"
name = "api:person_creator:create"
requires_auth = True
async def post(self, request):
"""Handle the POST request for creating a person."""
_LOGGER.info("Received HTTP POST request to create a person.")
hass = request.app["hass"]
data = await request.json()
name = data.get("name")
if not name:
_LOGGER.error("No name provided in the request.")
return self.json_message("Name is required", status_code=400)
_LOGGER.info(f"Attempting to create person with name: {name}")
await hass.services.async_call(DOMAIN, SERVICE_CREATE_PERSON, {ATTR_NAME: name})
_LOGGER.info(f"Person '{name}' created successfully via HTTP request.")
return self.json({"status": "success", "message": f"Person '{name}' created"})
async def async_setup(hass: HomeAssistant, config: dict):
"""Set up the person creator component."""
_LOGGER.info("Starting setup of person_creator component.")
async def handle_create_person(service: ServiceCall):
"""Handle the service call to create a new person."""
name = service.data[ATTR_NAME]
_LOGGER.info(f"Service called to create a new person with name: {name}")
# Create a unique ID by hashing the name and a UUID
unique_id = hashlib.md5(f"{name}_{uuid.uuid4().hex}".encode()).hexdigest()
_LOGGER.info(f"Generated unique ID for the person: {unique_id}")
try:
# Create a new person entity with a unique ID
await hass.services.async_call(
'person', 'create', {
'name': name,
'id': unique_id
}
)
_LOGGER.info(f"Person '{name}' created successfully with ID '{unique_id}'")
except Exception as e:
_LOGGER.error(f"Failed to create person '{name}': {e}")
try:
# Register the service
_LOGGER.info("Registering service create_person.")
async_register_admin_service(
hass, DOMAIN, SERVICE_CREATE_PERSON, handle_create_person, schema=PERSON_SCHEMA
)
# Register the HTTP view
_LOGGER.info("Registering HTTP view for person_creator.")
hass.http.register_view(CreatePersonView())
_LOGGER.info("Finished setting up person_creator component.")
except Exception as e:
_LOGGER.error(f"Failed during setup of person_creator: {e}")
return True
manifest.json:
{
"domain": "person_creator",
"name": "Person Creator",
"version": "1.0.0",
"documentation": "",
"dependencies": [],
"requirements": []
}
services.yaml:
create_person:
name: Create Person
description: Dynamically create a new person entity.
fields:
name:
description: The name of the person.
example: John Doe
I can’t find any mention of my new component in my logs except of a warning from loader:
(SyncWorker_0) [homeassistant.loader] We found a custom integration person_creator which has not been tested by Home Assistant. This component might cause stability problems, be sure to disable it if you experience issues with Home Assistant
when I tires:
curl -X POST -H “Authorization: Bearer $HA_TOKEN”
-H “Content-Type: application/json”
-d ‘{“name”: “John Doe”}’
http://homeassistant.local:8123/api/person_creator/create
I get 404…
what am I missing?