This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
utils.tsx
142 lines (120 loc) · 3.79 KB
/
utils.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import React from 'react';
import {HomeAssistant, ThreedyCondition, ThreedyConfig} from "../../types";
import TemperatureStat from "./TemperatureStat";
import { getEntity } from '../../Utils/HomeAssistant';
import Stat from "./Stat";
import TimeStat from "./TimeStat";
/*
const entityEnding = (() => {
switch (condition) {
case 'Status':
return config.use_mqtt ? '_print_status' : '_current_state'
case 'ETA':
return config.use_mqtt ? '_print_time_left' : '_time_remaining'
case 'Elapsed':
return config.use_mqtt ? '_print_time' : '_time_elapsed'
case 'Hotend':
return config.use_mqtt ? '_tool_0_temperature' : '_actual_tool0_temp'
case 'Bed':
return config.use_mqtt ? '_bed_temperature' : '_actual_bed_temp'
default:
return undefined
}
})();
*/
/**
* Function to dynamically render a stat by determining what type of stat it is
* @param hass
* @param config
* @param condition
*/
const renderCondition = (
hass: HomeAssistant,
config: ThreedyConfig,
condition: ThreedyCondition | string
) => {
const entity = (suffix: string) => getEntity(hass, `${config.base_entity}${suffix}`);
const mqtt = config.use_mqtt;
switch (condition) {
case ThreedyCondition.Status:
return (
<Stat
name={"Status"}
value={ entity( mqtt ? '_print_status' : '_current_state').state }
/>
)
case ThreedyCondition.ETA:
return (
<TimeStat
timeEntity={ entity( mqtt ? '_print_time_left' : '_time_remaining' ) }
condition={condition}
config={config}
direction={0}
/>
)
case ThreedyCondition.Elapsed:
return (
<TimeStat
timeEntity={ entity( mqtt ? '_print_time' : '_time_elapsed' ) }
condition={condition}
config={config}
direction={1}
/>
)
case ThreedyCondition.Remaining:
return (
<TimeStat
timeEntity={ entity( mqtt ? '_print_time_left' : '_time_remaining' ) }
condition={condition}
config={config}
direction={-1}
/>
)
case ThreedyCondition.Bed:
return (
<TemperatureStat
name={"Bed"}
temperatureEntity={ entity( mqtt ? '_bed_temperature' : '_actual_bed_temp' ) }
config={config}
/>
)
case ThreedyCondition.Hotend:
return (
<TemperatureStat
name={"Hotend"}
temperatureEntity={ entity( mqtt ? '_tool_0_temperature' : '_actual_tool0_temp' ) }
config={config}
/>
)
default:
return (
<Stat
name={"Unknown"}
value={"<unknown>"}
/>
)
}
}
/**
* Function to render all stats
* @param hass
* @param config
*/
const renderStats = (
hass: HomeAssistant,
config: ThreedyConfig
) => {
return config.monitored.map(
condition => renderCondition( hass, config, condition )
)
}
const percentComplete = (
hass: HomeAssistant,
config: ThreedyConfig
) => {
return (hass.states[config.use_mqtt ? `${config.base_entity}_print_progress` : `${config.base_entity}_job_percentage`] || { state: -1.0 }).state;
}
export {
renderStats,
percentComplete
}