Auto-register service via Supervisor API on startup (v2.0.5)
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user