Digital-Alchemy-TS / synapse

Typescript utilities for creating and managing virtual entities within Home Assistant
https://docs.digital-alchemy.app
MIT License
0 stars 0 forks source link

Service registration #23

Open zoe-codez opened 1 month ago

zoe-codez commented 1 month ago

Context

It'd be awesome if synapse was able to expose service calls to home assistant, in addition to entities. This would enable more standard interactions via hass.call, instead of weird side workflows involving buttons

Some chatgpt proof of concept to visualize python side.

from homeassistant.helpers import config_validation as cv
import voluptuous as vol

async def handle_service_call(call):
    # Handle the service call here
    service_data = call.data
    # Example: service_data might include parameters passed to the service

async def async_setup(hass, config):
    service_schema = vol.Schema({
        vol.Required('field1'): cv.string,
        vol.Optional('field2', default='default_value'): cv.string,
        vol.Required('field3'): cv.positive_int,
    })

    service_description = {
        "description": "Description of what this service does",
        "fields": {
            "field1": {
                "description": "Description of field1",
                "example": "example_value"
            },
            "field2": {
                "description": "Description of field2",
                "example": "example_value"
            },
            "field3": {
                "description": "Description of field3",
                "example": 42
            }
        }
    }

    # Register the service
    hass.services.async_register(
        domain='your_domain',
        service='your_service',
        service_func=handle_service_call,
        schema=service_schema,
        description=service_description
    )

    return True

Blocked by https://github.com/Digital-Alchemy-TS/hass/issues/34

zoe-codez commented 1 month ago

Needs consideration for

zoe-codez commented 3 days ago

Schema

Seems like this might be best approached with something that do validation. class-validator, zod, and the like have the ability to check on the incoming values.

They can also translate to json schema, which can then be handled by vol in python


Not sure what the flavor of the month for validators is tho

zoe-codez commented 3 days ago

How to handle conflicts

Maybe an expansion issue here. Seems like v1 should allow some foot gun and allow conflicts

A follow up might have apps that are conflict-y emit some warnings