Skip to content

Commit 1f9278e

Browse files
committed
fix: Improve editor error cases
Ref #233
1 parent 8a176ab commit 1f9278e

File tree

2 files changed

+12
-29
lines changed

2 files changed

+12
-29
lines changed

src/editor.ts

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import { CardConfig } from './config/card'
77
import { HASS } from './types'
88

99
function setValue(obj, path, value) {
10-
const a = path.split('.')
10+
const pathFragments = path.split('.')
1111
let o = obj
12-
while (a.length - 1) {
13-
var n = a.shift()
14-
if (!(n in o)) o[n] = {}
15-
o = o[n]
12+
while (pathFragments.length - 1) {
13+
var fragment = pathFragments.shift()
14+
if (!o.hasOwnProperty(fragment)) o[fragment] = {}
15+
o = o[fragment]
1616
}
17-
o[a[0]] = value
17+
o[pathFragments[0]] = value
1818
}
1919

2020
const OptionsDecimals = [0, 1]
@@ -31,7 +31,7 @@ const GithubReadMe =
3131
const stub = {
3232
header: {},
3333
layout: {
34-
modes: {},
34+
mode: {},
3535
},
3636
}
3737

@@ -59,23 +59,6 @@ export default class SimpleThermostatEditor extends LitElement {
5959
window.open(GithubReadMe)
6060
}
6161

62-
get _show_header() {
63-
if (this.config.header === false) {
64-
return false
65-
} else {
66-
return true
67-
}
68-
}
69-
70-
set _show_header(val) {
71-
console.log('set show header', val)
72-
if (val) {
73-
this.config.header = {}
74-
} else {
75-
this.config.header = false
76-
}
77-
}
78-
7962
render() {
8063
if (!this.hass) return html``
8164

@@ -97,7 +80,6 @@ export default class SimpleThermostatEditor extends LitElement {
9780
<ha-formfield label="Show header?">
9881
<ha-switch
9982
.checked=${this.config.header !== false}
100-
.configValue="${'header'}"
10183
@change=${this.toggleHeader}
10284
></ha-switch>
10385
</ha-formfield>

src/main.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export default class SimpleThermostat extends LitElement {
110110
@property()
111111
_hass: HASS = {}
112112
@property()
113-
entity: LooseObject = {}
113+
entity: LooseObject
114114
@property()
115115
sensors: Array<Sensor> = []
116116
@property()
@@ -144,16 +144,17 @@ export default class SimpleThermostat extends LitElement {
144144
}
145145

146146
setConfig(config: CardConfig) {
147-
if (!config.entity) {
148-
throw new Error('You need to define an entity')
149-
}
150147
this.config = {
151148
decimals: DECIMALS,
152149
...config,
153150
}
154151
}
155152

156153
set hass(hass: any) {
154+
if (!this.config.entity) {
155+
return
156+
}
157+
157158
const entity = hass.states[this.config.entity]
158159
if (typeof entity === undefined) {
159160
return

0 commit comments

Comments
 (0)