Initial commit of Simple Home Assistant Addon with web interface

This commit is contained in:
Manu
2025-06-01 12:04:22 +02:00
commit 3d987c3eb6
12 changed files with 660 additions and 0 deletions

49
web/index.html Normal file
View File

@ -0,0 +1,49 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Home Assistant Addon</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<header>
<h1>Simple Home Assistant Addon</h1>
<p>A simple interface for controlling your Home Assistant addon</p>
</header>
<main>
<div class="card">
<h2>Addon Status</h2>
<div class="status-indicator">
<span id="status-dot" class="status-dot"></span>
<span id="status-text">Checking...</span>
</div>
</div>
<div class="card">
<h2>Send Message</h2>
<div class="form-group">
<label for="message">Message:</label>
<input type="text" id="message" placeholder="Enter your message">
</div>
<button id="send-btn" class="btn">Send</button>
</div>
<div class="card">
<h2>Recent Activity</h2>
<div id="activity-log" class="activity-log">
<p>No recent activity</p>
</div>
</div>
</main>
<footer>
<p>&copy; 2025 Simple Home Assistant Addon</p>
</footer>
</div>
<script src="script.js"></script>
</body>
</html>

114
web/script.js Normal file
View File

@ -0,0 +1,114 @@
document.addEventListener('DOMContentLoaded', function() {
// Elements
const statusDot = document.getElementById('status-dot');
const statusText = document.getElementById('status-text');
const messageInput = document.getElementById('message');
const sendBtn = document.getElementById('send-btn');
const activityLog = document.getElementById('activity-log');
// API endpoints
const API_BASE = window.location.origin;
const STATUS_API = `${API_BASE}/api/status`;
const MESSAGE_API = `${API_BASE}/api/message`;
// Set initial status and check periodically
checkStatus();
setInterval(checkStatus, 30000); // Check every 30 seconds
// Event listeners
sendBtn.addEventListener('click', sendMessage);
messageInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
sendMessage();
}
});
// Check addon status
function checkStatus() {
logActivity('Checking addon status...');
fetch(STATUS_API)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
setStatus(data.status === 'online');
if (data.last_message) {
logActivity(`Last message: "${data.last_message}"`);
}
})
.catch(error => {
console.error('Error checking status:', error);
setStatus(false);
logActivity(`Error checking status: ${error.message}`);
});
}
// Set status indicator
function setStatus(isOnline) {
if (isOnline) {
statusDot.classList.add('online');
statusDot.classList.remove('offline');
statusText.textContent = 'Online';
} else {
statusDot.classList.add('offline');
statusDot.classList.remove('online');
statusText.textContent = 'Offline';
}
}
// Send message to Home Assistant
function sendMessage() {
const message = messageInput.value.trim();
if (!message) {
alert('Please enter a message');
return;
}
logActivity(`Sending message: "${message}"`);
fetch(MESSAGE_API, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message: message }),
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
if (data.success) {
logActivity(`Message sent successfully: "${message}"`);
messageInput.value = '';
} else {
logActivity(`Failed to send message: ${data.error || 'Unknown error'}`);
}
})
.catch(error => {
console.error('Error sending message:', error);
logActivity(`Error sending message: ${error.message}`);
});
}
// Log activity
function logActivity(text) {
const timestamp = new Date().toLocaleTimeString();
const logEntry = document.createElement('p');
logEntry.textContent = `[${timestamp}] ${text}`;
// Check if "No recent activity" message exists
if (activityLog.firstChild && activityLog.firstChild.textContent === 'No recent activity') {
activityLog.innerHTML = '';
}
activityLog.insertBefore(logEntry, activityLog.firstChild);
}
});

134
web/styles.css Normal file
View File

@ -0,0 +1,134 @@
:root {
--primary-color: #03a9f4;
--secondary-color: #2196f3;
--background-color: #f5f5f5;
--card-color: #ffffff;
--text-color: #333333;
--border-radius: 8px;
--box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: var(--background-color);
color: var(--text-color);
line-height: 1.6;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
header {
text-align: center;
margin-bottom: 30px;
}
header h1 {
color: var(--primary-color);
margin-bottom: 10px;
}
.card {
background-color: var(--card-color);
border-radius: var(--border-radius);
box-shadow: var(--box-shadow);
padding: 20px;
margin-bottom: 20px;
}
.card h2 {
color: var(--secondary-color);
margin-bottom: 15px;
font-size: 1.5rem;
}
.status-indicator {
display: flex;
align-items: center;
gap: 10px;
}
.status-dot {
display: inline-block;
width: 15px;
height: 15px;
border-radius: 50%;
background-color: #ccc;
}
.status-dot.online {
background-color: #4CAF50;
}
.status-dot.offline {
background-color: #F44336;
}
.form-group {
margin-bottom: 15px;
}
label {
display: block;
margin-bottom: 5px;
font-weight: 500;
}
input[type="text"] {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 16px;
}
.btn {
background-color: var(--primary-color);
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
transition: background-color 0.3s;
}
.btn:hover {
background-color: var(--secondary-color);
}
.activity-log {
max-height: 200px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
padding: 10px;
background-color: #f9f9f9;
}
.activity-log p {
margin-bottom: 5px;
padding-bottom: 5px;
border-bottom: 1px solid #eee;
}
.activity-log p:last-child {
margin-bottom: 0;
border-bottom: none;
}
footer {
text-align: center;
margin-top: 30px;
color: #777;
font-size: 14px;
}