|
4 | 4 | from typing import Optional |
5 | 5 |
|
6 | 6 |
|
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}" |
9 | 15 |
|
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. |
12 | 20 |
|
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 %}} |
18 | 38 | """ |
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. |
21 | 49 |
|
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: |
25 | 57 | 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 }}}}" |
35 | 65 | icon: "mdi:clock-outline" |
36 | 66 | 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