Skip to content

Commit 064dcd5

Browse files
Web tidy
1 parent 3d64849 commit 064dcd5

2 files changed

Lines changed: 79 additions & 62 deletions

File tree

‎apps/predbat/web.py‎

Lines changed: 7 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import shutil
2020
import html as html_module
2121
import time
22-
from web_helper import get_header_html, get_plan_css, get_editor_js, get_editor_css, get_log_css, get_charts_css, get_apps_css, get_html_config_css, get_apps_js, get_components_css, get_logfile_js
22+
from web_helper import get_header_html, get_plan_css, get_editor_js, get_editor_css, get_log_css, get_charts_css, get_apps_css, get_html_config_css, get_apps_js, get_components_css, get_logfile_js, get_entity_toggle_js, get_entity_control_css
2323

2424
from utils import calc_percent_limit, str2time, dp0, dp2
2525
from config import TIME_FORMAT, TIME_FORMAT_DAILY
@@ -815,37 +815,8 @@ def get_entity_control_html(self, entity_id, days):
815815
html = '<div class="entity-edit-container" style="margin-top: 15px; padding: 15px; border: 1px solid var(--border-color, #ddd); border-radius: 5px; background-color: var(--background-secondary, #f9f9f9);">'
816816

817817
# Add CSS for dark mode support
818-
html += """
819-
<style>
820-
.entity-edit-container {
821-
--border-color: #ddd;
822-
--background-secondary: #f9f9f9;
823-
--text-color: #333;
824-
--text-secondary: #666;
825-
--input-background: #fff;
826-
}
818+
html += get_entity_control_css()
827819

828-
body.dark-mode .entity-edit-container {
829-
--border-color: #555;
830-
--background-secondary: #2d2d2d;
831-
--text-color: #e0e0e0;
832-
--text-secondary: #bbb;
833-
--input-background: #3d3d3d;
834-
color: var(--text-color);
835-
}
836-
837-
body.dark-mode .entity-edit-container input[type="number"],
838-
body.dark-mode .entity-edit-container select {
839-
background-color: var(--input-background) !important;
840-
border-color: var(--border-color) !important;
841-
color: var(--text-color) !important;
842-
}
843-
844-
body.dark-mode .entity-edit-container span {
845-
color: var(--text-color) !important;
846-
}
847-
</style>
848-
"""
849820
html += f'<form method="post" action="./entity" style="display: flex; align-items: center; gap: 10px;">'
850821
html += f'<input type="hidden" name="entity_id" value="{entity_id}">'
851822
html += f'<input type="hidden" name="days" value="{days}">'
@@ -860,37 +831,7 @@ def get_entity_control_html(self, entity_id, days):
860831
html += f"</div>"
861832

862833
# Add JavaScript for toggle functionality
863-
html += f"""
864-
<script>
865-
function toggleEntitySwitch(button, entityId, days) {{
866-
// Toggle the visual state
867-
button.classList.toggle('active');
868-
869-
// Determine the new value
870-
const newValue = button.classList.contains('active') ? 'on' : 'off';
871-
872-
// Create form data
873-
const formData = new FormData();
874-
formData.append('entity_id', entityId);
875-
formData.append('days', days);
876-
formData.append('value', newValue);
877-
878-
// Submit the form
879-
fetch('./entity', {{
880-
method: 'POST',
881-
body: formData
882-
}}).then(response => {{
883-
if (response.redirected) {{
884-
window.location.href = response.url;
885-
}}
886-
}}).catch(error => {{
887-
console.error('Error:', error);
888-
// Revert the toggle on error
889-
button.classList.toggle('active');
890-
}});
891-
}}
892-
</script>
893-
"""
834+
html += get_entity_toggle_js()
894835

895836
elif control_type == "number":
896837
min_val = self.base.get_state_wrapper(entity_id=entity_id, attribute="min", default=None)
@@ -1118,11 +1059,15 @@ async def html_api_get_log(self, request):
11181059
"""
11191060
try:
11201061
logfile = "predbat.log"
1062+
logfile_1 = "predbat.1.log"
11211063
logdata = ""
11221064

11231065
if os.path.exists(logfile):
11241066
with open(logfile, "r") as f:
11251067
logdata = f.read()
1068+
if os.path.exists(logfile_1):
1069+
with open(logfile_1, "r") as f:
1070+
logdata = f.read() + "\n" + logdata
11261071

11271072
# Get query parameters
11281073
args = request.query

‎apps/predbat/web_helper.py‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,78 @@
1111
# Helper functions for web pages
1212

1313

14+
def get_entity_control_css():
15+
# Add CSS for dark mode support
16+
html = """
17+
<style>
18+
.entity-edit-container {
19+
--border-color: #ddd;
20+
--background-secondary: #f9f9f9;
21+
--text-color: #333;
22+
--text-secondary: #666;
23+
--input-background: #fff;
24+
}
25+
26+
body.dark-mode .entity-edit-container {
27+
--border-color: #555;
28+
--background-secondary: #2d2d2d;
29+
--text-color: #e0e0e0;
30+
--text-secondary: #bbb;
31+
--input-background: #3d3d3d;
32+
color: var(--text-color);
33+
}
34+
35+
body.dark-mode .entity-edit-container input[type="number"],
36+
body.dark-mode .entity-edit-container select {
37+
background-color: var(--input-background) !important;
38+
border-color: var(--border-color) !important;
39+
color: var(--text-color) !important;
40+
}
41+
42+
body.dark-mode .entity-edit-container span {
43+
color: var(--text-color) !important;
44+
}
45+
</style>
46+
"""
47+
return html
48+
49+
def get_entity_toggle_js():
50+
"""
51+
JavaScript for entity toggle buttons
52+
"""
53+
html = f"""
54+
<script>
55+
function toggleEntitySwitch(button, entityId, days) {{
56+
// Toggle the visual state
57+
button.classList.toggle('active');
58+
59+
// Determine the new value
60+
const newValue = button.classList.contains('active') ? 'on' : 'off';
61+
62+
// Create form data
63+
const formData = new FormData();
64+
formData.append('entity_id', entityId);
65+
formData.append('days', days);
66+
formData.append('value', newValue);
67+
68+
// Submit the form
69+
fetch('./entity', {{
70+
method: 'POST',
71+
body: formData
72+
}}).then(response => {{
73+
if (response.redirected) {{
74+
window.location.href = response.url;
75+
}}
76+
}}).catch(error => {{
77+
console.error('Error:', error);
78+
// Revert the toggle on error
79+
button.classList.toggle('active');
80+
}});
81+
}}
82+
</script>
83+
"""
84+
return html
85+
1486
def get_apps_js(all_states_json):
1587
text = (
1688
f"""

0 commit comments

Comments
 (0)