From ae84e1d6e920def67c5097a88d92519ecfa435e1 Mon Sep 17 00:00:00 2001 From: OpenClaw Bot Date: Sun, 8 Feb 2026 14:45:30 +0100 Subject: [PATCH] Auto-register service via Supervisor API on startup (v2.0.5) --- sip-notifier/config.yaml | 4 +++- sip-notifier/run.sh | 35 +++++++++++++++++++---------- sip-notifier/sip_service.py | 44 +++++++++++++++++++++++++++++++++++-- 3 files changed, 69 insertions(+), 14 deletions(-) diff --git a/sip-notifier/config.yaml b/sip-notifier/config.yaml index 4a4b876..23a0a0b 100644 --- a/sip-notifier/config.yaml +++ b/sip-notifier/config.yaml @@ -1,5 +1,5 @@ name: "SIP Voice Notifier" -version: "2.0.4" +version: "2.0.5" slug: "sip-notifier" description: "Send voice notifications via SIP phone calls (includes integration)" arch: @@ -14,6 +14,8 @@ boot: auto homeassistant_api: true hassio_api: true hassio_role: default +services: + - sip_notifier.send_notification:sip_notifier ports: 8099/tcp: null options: diff --git a/sip-notifier/run.sh b/sip-notifier/run.sh index e5f0c14..9561efc 100644 --- a/sip-notifier/run.sh +++ b/sip-notifier/run.sh @@ -12,16 +12,29 @@ bashio::log.info "SIP Server: ${SIP_SERVER}" bashio::log.info "SIP User: ${SIP_USER}" bashio::log.info "Default Duration: ${DEFAULT_DURATION}s" -bashio::log.info "Add-on ready - listening on port 8099" -bashio::log.info "To use from Home Assistant, add this to your configuration.yaml:" -bashio::log.info "" -bashio::log.info "rest_command:" -bashio::log.info " sip_notification:" -bashio::log.info " url: http://088d3b92-sip-notifier:8099/send_notification" -bashio::log.info " method: POST" -bashio::log.info " content_type: application/json" -bashio::log.info " payload: '{\"destination\": \"{{ destination }}\", \"message\": \"{{ message }}\", \"duration\": {{ duration | default(30) }}}'" -bashio::log.info "" +# Wait for supervisor to be ready +sleep 3 -# Start the service +# Register service with Home Assistant via Supervisor API +bashio::log.info "Registering sip_notifier.send_notification service with Home Assistant..." + +# Get add-on slug +ADDON_SLUG="088d3b92_sip-notifier" + +# Register the service +curl -sSL -X POST \ + -H "Authorization: Bearer ${SUPERVISOR_TOKEN}" \ + -H "Content-Type: application/json" \ + -d "{ + \"addon\": \"${ADDON_SLUG}\", + \"service\": \"send_notification\" + }" \ + "http://supervisor/services/sip_notifier/send_notification" \ + && bashio::log.info "Service registered successfully!" \ + || bashio::log.warning "Service registration failed (may already exist)" + +bashio::log.info "Add-on ready - listening on port 8099" +bashio::log.info "Service available as: sip_notifier.send_notification" + +# Start the Flask service exec python3 /app/sip_service.py diff --git a/sip-notifier/sip_service.py b/sip-notifier/sip_service.py index 4a56e15..e28419e 100644 --- a/sip-notifier/sip_service.py +++ b/sip-notifier/sip_service.py @@ -24,6 +24,40 @@ app = Flask(__name__) # Global config CONFIG = {} DEFAULT_SAMPLE_RATE = 8000 +SUPERVISOR_TOKEN = os.environ.get('SUPERVISOR_TOKEN') + + +def register_service(): + """Register service with Home Assistant via Supervisor.""" + if not SUPERVISOR_TOKEN: + _LOGGER.warning("No SUPERVISOR_TOKEN, skipping service registration") + return + + try: + headers = { + 'Authorization': f'Bearer {SUPERVISOR_TOKEN}', + 'Content-Type': 'application/json', + } + + # Call Home Assistant service registration endpoint + response = requests.post( + 'http://supervisor/services', + headers=headers, + json={ + 'domain': 'sip_notifier', + 'service': 'send_notification', + 'addon': '088d3b92_sip-notifier' + }, + timeout=10 + ) + + if response.status_code in [200, 201]: + _LOGGER.info("✅ Service registered: sip_notifier.send_notification") + else: + _LOGGER.warning(f"Service registration returned: {response.status_code}") + + except Exception as e: + _LOGGER.error(f"Failed to register service: {e}") def download_and_convert_audio(url: str) -> str: @@ -227,6 +261,12 @@ if __name__ == '__main__': 'default_duration': int(os.getenv('DEFAULT_DURATION', 30)) } - _LOGGER.info("SIP Voice Notifier ready - service will be auto-registered by Supervisor") - _LOGGER.info("Starting service on port 8099") + # Register service with Home Assistant + _LOGGER.info("Registering service with Home Assistant...") + time.sleep(2) # Wait for supervisor to be ready + register_service() + + _LOGGER.info("SIP Voice Notifier ready") + _LOGGER.info("Service: sip_notifier.send_notification") + _LOGGER.info("Starting Flask service on port 8099") app.run(host='0.0.0.0', port=8099, debug=False)