ctucx.git: smartie-pwa

[js] smarthome web-gui

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 
"use strict";

import { LitElement, html, css } from "lit-element";
import { state } from "../state.js";
import { pad, formatDate, round, get } from "../util.js";
import { archive } from "../archive.js";

import "@authentic/mwc-icon";
import "../card.js";
import "../spinner.js";

class TemperatureLive extends LitElement {
	constructor() {
		super(...arguments);
		this._cachedDayArchive = {};
		state.subscribe(this.requestUpdate.bind(this));
		archive.subscribe(this.requestUpdate.bind(this));
	}

	render() {
		const config = state.config.views.filter(view => view.url == this.viewid)[0];
		return html`
			${state.data[config.sensors[0].device] ? html`
				<div id="connection-status">
					${state.connected ? html`
						<p><mwc-icon>cloud_queue</mwc-icon> Connected</p>
					` : html`
						<p><mwc-icon>cloud_off</mwc-icon> Disconnected</p>
						<p class="lastupdate">last updated ${formatDate(state.data.lastUpdated)}</p>
					`}
				</div>
				<smarthome-card>
					<div class="table">
						<div class="table-row table-head">
							<div class="table-column" style="width: 50%;">Name</div>
							<div class="table-column" style="width: 29%;">Temperature</div>
							<div class="table-column" style="width: 21%;">Humidity</div>
						</div>
						${config.sensors.map(d => html`
							<div class="table-row">
								<div class="table-column" style="width: 50%;">${d.name}${state.data[d.device].weakBattery ? html` <mwc-icon>battery_alert</mwc-icon>` : html``}</div>
								<div class="table-column" style="width: 29%;">${round(state.data[d.device].temperature, 2)}°C</div>
								<div class="table-column" style="width: 21%;">${(state.data[d.device].humidity != -1 ) ? html`${round(state.data[d.device].humidity, 2)}%` : html`-` }</div>
							</div>
						`)}
					</div>
				</smarthome-card>
			` : html`
				<smarthome-spinner>Trying to connect...</smarthome-spinner>
			`}
		`;
	}

	static get styles() {
		return css`
			:host {
				display: flex;
				flex-direction: column;
				--mdc-theme-on-primary: white;
				--mdc-theme-primary: #43a047;
				--mdc-theme-on-secondary: white;
				--mdc-theme-secondary: #616161;
			}
			#connection-status mwc-icon {
				padding-right: .5em;
			}
			#connection-status {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
			}
			#connection-status>p {
				display: flex;
				flex-direction: row;
				justify-content: center;
				align-items: center;
			}
			#connection-status>p.lastupdate {
				font-size: 1em;
			}
			.table {
				display: flex;
				flex-direction: column;
			}
			.table-row {
				display: flex;
				flex-direction: row;
				width: 100%;
			}
			.table-column {
				padding: 1em;
				height: 1em;
				display: flex;
				flex-direction: column;
				justify-content: center;
			}
			.table-row {
				background-color: #ddd;
			}
			.table-row:nth-child(2n) {
				background-color: #fff;
			}
			@media (min-width: 600px) {
				#connection-status {
					margin-top: 20px;
				}
			}
		`;
	}

	static get properties() {
		return {
			viewid: { type: String }
		};
	}
}

customElements.define("smarthome-temperature-live", TemperatureLive);