112 lines
3.3 KiB
Python
112 lines
3.3 KiB
Python
import logging
|
|
import asyncio
|
|
import os
|
|
import json
|
|
from aiohttp import web
|
|
import aiohttp_cors
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_START
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers import discovery
|
|
from homeassistant.components.http import HomeAssistantView
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
# Define the web server port
|
|
WEB_PORT = 8099
|
|
|
|
async def async_setup(hass: HomeAssistant, config: dict):
|
|
"""Set up the simple addon component."""
|
|
# Log that the addon is starting
|
|
_LOGGER.info("Simple Addon is starting")
|
|
|
|
# Get the directory of this file
|
|
current_dir = os.path.dirname(os.path.realpath(__file__))
|
|
web_dir = os.path.join(current_dir, 'web')
|
|
|
|
# Create a simple service
|
|
async def handle_hello(call):
|
|
"""Handle the service call."""
|
|
message = call.data.get('message', 'No message provided')
|
|
_LOGGER.info(f"Hello service called with message: {message}")
|
|
|
|
# Store the message in the state
|
|
hass.states.async_set('simple_addon.last_message', message)
|
|
|
|
return {"success": True, "message": message}
|
|
|
|
# Register our service
|
|
hass.services.async_register(
|
|
'simple_addon',
|
|
'hello',
|
|
handle_hello
|
|
)
|
|
|
|
# Set up the web server
|
|
app = web.Application()
|
|
|
|
# Configure CORS
|
|
cors = aiohttp_cors.setup(app, defaults={
|
|
"*": aiohttp_cors.ResourceOptions(
|
|
allow_credentials=True,
|
|
expose_headers="*",
|
|
allow_headers="*",
|
|
)
|
|
})
|
|
|
|
# API endpoint for sending messages
|
|
class MessageView(HomeAssistantView):
|
|
url = "/api/message"
|
|
name = "api:message"
|
|
|
|
async def post(self, request):
|
|
data = await request.json()
|
|
message = data.get('message', '')
|
|
|
|
# Call the hello service
|
|
await hass.services.async_call(
|
|
'simple_addon', 'hello',
|
|
{"message": message}
|
|
)
|
|
|
|
return web.json_response({"success": True})
|
|
|
|
# API endpoint for getting status
|
|
class StatusView(HomeAssistantView):
|
|
url = "/api/status"
|
|
name = "api:status"
|
|
|
|
async def get(self, request):
|
|
return web.json_response({
|
|
"status": "online",
|
|
"last_message": hass.states.get('simple_addon.last_message').state
|
|
if hass.states.get('simple_addon.last_message') else None
|
|
})
|
|
|
|
# Register the API endpoints
|
|
hass.http.register_view(MessageView)
|
|
hass.http.register_view(StatusView)
|
|
|
|
# Serve static files
|
|
app.router.add_static('/addon/', web_dir)
|
|
|
|
# Start the web server
|
|
runner = web.AppRunner(app)
|
|
await runner.setup()
|
|
site = web.TCPSite(runner, '0.0.0.0', WEB_PORT)
|
|
await site.start()
|
|
|
|
_LOGGER.info(f"Web interface started on port {WEB_PORT}")
|
|
|
|
# Set the webui URL in the addon configuration
|
|
hass.states.async_set('simple_addon.webui', f"http://localhost:{WEB_PORT}/addon/")
|
|
|
|
# Return boolean to indicate that initialization was successfully
|
|
return True
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry):
|
|
"""Set up the addon from a config entry."""
|
|
hass.async_create_task(
|
|
hass.config_entries.async_forward_entry_setup(entry, "sensor")
|
|
)
|
|
return True
|