Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit bbc0d5c

Browse files
author
Dan Greco
committed
Finish graphical configurator
1 parent 9346755 commit bbc0d5c

File tree

8 files changed

+189
-290
lines changed

8 files changed

+189
-290
lines changed

README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
- [~~Method 1: HACS~~](#method-1-hacs)
1616
- [Method 2: Manual](#method-2-manual)
1717
- [Config](#-config)
18-
- [Required](#required)
19-
- [Optional](#optional)
18+
- [Graphical](#-Graphical)
19+
- [Manual](#manual)
20+
- [Required](#required)
21+
- [Optional](#optional)
2022
- [Example Config](#-example-config)
2123
- [Custom Theming](#-custom-theming)
2224
- [Screenshots](#-screenshots)
@@ -65,15 +67,22 @@
6567
## Config
6668
---
6769

68-
### Required
70+
### Graphical (Recommended)
71+
72+
![graphical](https://github.com/dangreco/threedy/raw/master/screenshots/graphical.png)
73+
74+
75+
### Manual
76+
77+
#### Required
6978

7079
- ```type``` — Always ```'custom:threedy-card'```
7180
- ```base_entity``` — Take the beginning of one of the OctoPrint sensors of your printer. Example: for ```sensor.ender_3_v2_current_state``` it would be ```sensor_ender_3_v2```.
7281
- ```name``` — Can be whatever you want!
7382
- ```printer_type``` — Use a printer style: ```'I3' | 'Cantilever' ```
7483
- ```monitored``` — A list of values to monitor throughout the print; gets displayed to the right of the printer.
7584

76-
### Optional
85+
#### Optional
7786

7887
- ```theme``` — Theme of the card: ```'Default' | 'Neumorphic' ```. Screenshots listed below.
7988
- ```font``` — Specify the font used in the card. By default it is ```sans-serif```.

dist/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
},
3737
'switch.ender': {
3838

39+
},
40+
'switch.light': {
41+
state: 'off'
3942
}
4043
},
4144
callService: (domain, service, data) => {
@@ -53,6 +56,7 @@
5356
theme: "Default",
5457
scale: 1,
5558
printer_type: 'I3',
59+
light_entity: 'switch.light',
5660
font: 'Assistant',
5761
monitored: [
5862
"Status",

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
"author": "Dan Greco",
88
"license": "MIT",
99
"dependencies": {
10-
"@material-ui/core": "^4.11.3",
1110
"framer-motion": "^3.2.2-rc.1",
12-
"jss": "^10.5.1",
1311
"moment": "^2.29.1",
1412
"react": "^17.0.1",
13+
"react-beautiful-dnd": "^13.0.0",
1514
"react-cool-dimensions": "^1.2.0",
1615
"react-dom": "^17.0.1",
1716
"react-icons": "^4.1.0"

src/Configurator/index.js

Lines changed: 100 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,123 @@
1-
import React, { useState } from 'react';
2-
import Select from '@material-ui/core/Select';
3-
import MenuItem from '@material-ui/core/MenuItem';
4-
import TextField from '@material-ui/core/TextField';
1+
import React, { useEffect, useState } from 'react';
2+
import DragDrop from './Components/DragDrop';
3+
import Input from './Components/Input';
4+
import MultiSelector from './Components/MultiSelector';
5+
import Select from './Components/Select';
56

67
import styles from './styles';
78

8-
const Configurator = ({ hass, config, configChanged }) => {
99

10-
const [selected, setSelected] = useState(undefined);
10+
const defaultConditions = [
11+
'Status',
12+
'ETA',
13+
'Elapsed',
14+
'Hotend',
15+
'Bed'
16+
]
1117

12-
const printers = Object.keys(hass.states).filter(entityId => (/sensor\..*_current_state/g).test(entityId)).map(
18+
19+
const Configurator = ({ hass, config, threedy }) => {
20+
21+
const printers = [];
22+
const [modifiedConfig, setModifiedConfig] = useState(config);
23+
24+
Object.keys(hass.states).filter(entityId => (/sensor\..*_current_state/g).test(entityId)).map(
1325
entityId => {
1426

15-
const printerSlug = entityId.replace('sensor.', '').replace('_current_state', '');
27+
const base_entity = entityId.replace('_current_state', '');
28+
const printerSlug = base_entity.replace('sensor.', '');
1629
const printerName = printerSlug.split("_").map(s => s.charAt(0).toUpperCase() + s.substring(1)).join(" ");
1730

18-
return {
19-
name: printerName,
20-
baseEntity: entityId.replace('_current_state', '')
21-
}
31+
32+
printers[base_entity] = printerName;
2233
}
2334
)
2435

36+
useEffect(() => {
37+
setModifiedConfig(config);
38+
}, [config])
39+
40+
const updateConfig = (updates) => {
41+
42+
const event = new Event("config-changed", {
43+
bubbles: true,
44+
composed: true
45+
});
46+
event.detail = {
47+
config: {
48+
...modifiedConfig,
49+
...updates
50+
}
51+
};
52+
threedy.dispatchEvent(event);
53+
setModifiedConfig(event.detail.config);
54+
}
55+
56+
const changePrinter = ({ key: base_entity, value: name }) => {
57+
updateConfig({
58+
base_entity,
59+
name
60+
});
61+
}
62+
63+
const changePrinterType = ({ key: printer_type, value: _ }) => {
64+
updateConfig({
65+
printer_type
66+
})
67+
}
68+
69+
const changePrinterName = (printerName) => {
70+
updateConfig({
71+
name: printerName
72+
})
73+
}
74+
75+
const changeMonitored = (monitored) => {
76+
updateConfig({
77+
monitored
78+
})
79+
}
80+
81+
if (!config) return <div></div>
82+
2583
return (
2684
<div style={{ ...styles.Configurator }}>
2785
<p style={{ ...styles.Label }}>Printer</p>
28-
<Select style={{ ...styles.PrinterSelect }} value={selected} onChange={(v) => setSelected(v)}>
29-
<MenuItem value={"test"}>Test</MenuItem>
30-
{
31-
printers.map(printer => (
32-
<MenuItem value={printer.baseEntity}>{printer.name}</MenuItem>
33-
))
34-
}
35-
</Select>
86+
<Select
87+
placeholder="Select..."
88+
options={printers}
89+
onSelect={changePrinter}
90+
initial={config.base_entity}
91+
/>
92+
3693
{
37-
selected ? (
94+
modifiedConfig ? modifiedConfig.name ? (
3895
<>
39-
<TextField label="Name" />
40-
<p style={{ ...styles.Label }}>Type</p>
96+
<p style={{ ...styles.Label }}>Printer Type</p>
97+
<Select
98+
placeholder="Select..."
99+
options={{
100+
"I3": "I3",
101+
"Cantilever": "Cantilever"
102+
}}
103+
onSelect={changePrinterType}
104+
initial={config.printer_type}
105+
/>
106+
107+
<p style={{ ...styles.Label }}>Name</p>
108+
<Input
109+
onUpdate={changePrinterName}
110+
initial={config.name || modifiedConfig.name}
111+
/>
112+
41113
<p style={{ ...styles.Label }}>Monitored</p>
114+
<MultiSelector items={defaultConditions} initial={config.monitored} onChange={changeMonitored} />
42115
</>
43-
) : (null)
116+
) : (null) : (null)
44117
}
118+
119+
120+
45121
</div>
46122
)
47123

src/Configurator/styles.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ const styles = {
66
width: '100%'
77
},
88
Label: {
9+
marginBottom: 4,
10+
fontWeight: 'bold',
11+
fontSize: 14
912
}
1013
};
1114

src/Printers/I3/index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ const I3 = ({ printerConfig }) => {
1818

1919
const { ref, width, height, entry, unobserve, observe } = useDimensions({
2020
onResize: ({ width, height, entry, unobserve, observe }) => {
21-
console.log("Foo")
2221
setDimensions(
2322
getDimensions(printerConfig, {width, height}, config.scale || 1.0)
2423
)
2524
},
2625
});
2726

28-
console.log(width, height)
2927

3028
const printing = (hass.states[`${config.base_entity}_current_state`] || { state: "unknown" }).state === 'Printing';
3129
const progress = (hass.states[`${config.base_entity}_job_percentage`] || { state: 0 }).state / 100;

src/index.js

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ import ReactDOM from 'react-dom';
33
import NotConfigured from './Components/NotConfigured';
44
import ThreedyWrapper from './Components/ThreedyWrapper';
55
import Configurator from './Configurator';
6-
import { StylesProvider, jssPreset } from '@material-ui/styles';
7-
import { create } from 'jss';
86

97
class ThreedyEditor extends HTMLElement {
108

11-
_jss;
129
_hass;
1310
_config;
1411

15-
set hass(hass)
12+
constructor()
1613
{
14+
super();
15+
}
16+
17+
set hass(hass) {
1718
this._hass = hass;
1819
this._render();
1920
}
@@ -23,32 +24,16 @@ class ThreedyEditor extends HTMLElement {
2324
this._render();
2425
}
2526

26-
configChanged(newConfig) {
27-
const event = new Event("config-changed", {
28-
bubbles: true,
29-
composed: true
30-
});
31-
event.detail = { config: newConfig };
32-
this.dispatchEvent(event);
33-
}
34-
3527
connectedCallback() {
36-
this._jss = create({
37-
...jssPreset(),
38-
insertionPoint: this
39-
});
4028
this._render();
4129
}
4230

43-
_render()
44-
{
45-
if (!this._hass || !this._jss)
31+
_render() {
32+
if (!this._hass)
4633
return
4734

4835
ReactDOM.render(
49-
(<StylesProvider jss={this._jss}>
50-
<Configurator hass={this._hass} config={this._config} configChanged={this.configChanged} />
51-
</StylesProvider>),
36+
<Configurator hass={this._hass} config={this._config} threedy={this} />,
5237
this
5338
);
5439
}
@@ -98,8 +83,7 @@ class ThreedyCard extends HTMLElement {
9883
}
9984

10085

101-
static getConfigElement()
102-
{
86+
static getConfigElement() {
10387
return document.createElement('threedy-editor');
10488
}
10589

0 commit comments

Comments
 (0)