Skip to content

Commit db2578f

Browse files
committed
feature: add formated time card
1 parent cd02139 commit db2578f

2 files changed

Lines changed: 76 additions & 36 deletions

File tree

workpulse/cli.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pathlib import Path
99

1010
from .database import Database
11-
from .homeassistant import generate_yaml_config
11+
from .homeassistant import YAMLGenerator
1212
from .mqtt_client import MQTTClient
1313
from .mqtt_config import create_default_config, load_config
1414
from .service import ServiceManager
@@ -519,18 +519,28 @@ def mqtt_yaml(self) -> int:
519519
Returns:
520520
Exit code (0 for success, non-zero for failure)
521521
"""
522-
yaml_config = generate_yaml_config()
522+
yaml_config = YAMLGenerator().generate_mqtt_yaml()
523+
template_config = YAMLGenerator().generate_template_yaml()
523524

524525
print("Home Assistant YAML Configuration:")
525526
print("=" * 60)
527+
print("Template Sensor Configuration:")
528+
print(" - Provides a formatted time sensor to use on dashboard\n")
529+
print(template_config)
530+
print("=" * 60)
531+
print("MQTT Sensor Configuration:")
532+
print(
533+
" - Connects to WorkPulse MQTT publisher to get working time (required)\n"
534+
)
526535
print(yaml_config)
527536
print("=" * 60)
528537
print("\nInstructions:")
529-
print("1. Copy the YAML configuration above")
538+
print("1. Copy both YAML configurations above")
530539
print("2. Paste it into your Home Assistant configuration.yaml file")
531540
print("3. Restart Home Assistant (or reload MQTT integration)")
532-
print("4. Ensure WorkPulse MQTT publisher is running: workpulse mqtt start")
533-
541+
print(
542+
"4. Ensure WorkPulse MQTT publisher is running: workpulse mqtt start service"
543+
)
534544
return 0
535545

536546

workpulse/homeassistant.py

Lines changed: 61 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,71 @@
44
from typing import Optional
55

66

7-
def generate_yaml_config(hostname: Optional[str] = None) -> str:
8-
"""Generate Home Assistant YAML configuration for WorkPulse.
7+
class YAMLGenerator:
8+
def __init__(self, hostname: Optional[str] = None):
9+
if not hostname:
10+
self.hostname = socket.gethostname()
11+
else:
12+
self.hostname = hostname
13+
self.name = "WorkPulse Daily Time"
14+
self.identifier = f"workpulse_{self.hostname}"
915

10-
Generates complete YAML configuration with hostname automatically filled in.
11-
The output can be copied and pasted directly into Home Assistant's configuration.yaml.
16+
def generate_template_yaml(self) -> str:
17+
"""Generates YAML configuration for a template sensor that formats the daily time
18+
reported by the MQTT sensor into a human-readable format (hours and minutes).
19+
The output can be copied and pasted directly into Home Assistant's configuration.yaml.
1220
13-
Args:
14-
hostname: Hostname to use in the configuration. If None, automatically detects.
15-
16-
Returns:
17-
Complete YAML configuration string ready for Home Assistant
21+
Returns:
22+
YAML configuration string for the template sensor
23+
"""
24+
sensor = f"sensor.{self.identifier}_{self.name.lower().replace(' ', '_')}"
25+
yaml_template = f"""template:
26+
- sensor:
27+
- name: "WorkPulse Daily Time Formatted ({self.hostname})"
28+
icon: mdi:clock-outline
29+
state: >
30+
{{% set total_seconds = states('{sensor}') | int %}}
31+
{{% set hours = (total_seconds // 3600) %}}
32+
{{% set minutes = ((total_seconds % 3600) // 60) %}}
33+
{{% if hours > 0 %}}
34+
{{{{ hours }}}}h {{% if minutes > 0 %}}{{{{ minutes }}}}m{{% endif %}}
35+
{{% else %}}
36+
{{{{ minutes }}}}m
37+
{{% endif %}}
1838
"""
19-
if hostname is None:
20-
hostname = socket.gethostname()
39+
return yaml_template
40+
41+
def generate_mqtt_yaml(self) -> str:
42+
"""Generate Home Assistant YAML configuration for WorkPulse.
43+
44+
Generates complete YAML configuration with hostname automatically filled in.
45+
The output can be copied and pasted directly into Home Assistant's configuration.yaml.
46+
47+
Args:
48+
hostname: Hostname to use in the configuration. If None, automatically detects.
2149
22-
# Escape braces for Home Assistant template syntax
23-
# In f-strings: {{ becomes {, so {{{{ becomes {{
24-
yaml_config = f"""mqtt:
50+
Returns:
51+
Complete YAML configuration string ready for Home Assistant
52+
"""
53+
54+
# Escape braces for Home Assistant template syntax
55+
# In f-strings: {{ becomes {, so {{{{ becomes {{
56+
yaml_config = f"""mqtt:
2557
sensor:
26-
# Daily Total Active Time (formatted as hours:minutes)
27-
- name: "WorkPulse Daily Time"
28-
unique_id: "workpulse_{hostname}_daily_time"
29-
state_topic: "workpulse/{hostname}/status"
30-
value_template: >
31-
{{% set total_seconds = value_json.total_time | int %}}
32-
{{% set hours = (total_seconds / 3600) | int %}}
33-
{{% set minutes = ((total_seconds % 3600) / 60) | int %}}
34-
{{% if hours > 0 %}}{{{{ hours }}}}h {{% if minutes > 0 %}}{{{{ minutes }}}}m{{% endif %}}{{% else %}}{{{{ minutes }}}}m{{% endif %}}
58+
- name: "{self.name}"
59+
unit_of_measurement: "s"
60+
device_class: duration
61+
state_class: total_increasing
62+
unique_id: "workpulse_{self.hostname}_daily_time"
63+
state_topic: "workpulse/{self.hostname}/status"
64+
value_template: "{{{{ value_json.total_time | int }}}}"
3565
icon: "mdi:clock-outline"
3666
device:
37-
identifiers:
38-
- "workpulse_{hostname}"
39-
name: "WorkPulse - {hostname}"
40-
manufacturer: "WorkPulse"
41-
model: "WorkPulse"
42-
"""
43-
44-
return yaml_config
67+
identifiers:
68+
- "{self.identifier}"
69+
name: "WorkPulse - {self.hostname}"
70+
manufacturer: "WorkPulse"
71+
model: "WorkPulse"
72+
"""
73+
74+
return yaml_config

0 commit comments

Comments
 (0)