@@ -7,9 +7,10 @@ import { customElement, property, state } from 'lit/decorators';
77import { repeat } from 'lit/directives/repeat' ;
88import { EntityConfig , SankeyChartConfig , SectionConfig } from '../types' ;
99import { localize } from '../localize/localize' ;
10- import { getEntityId } from '../utils' ;
10+ import { getEntityId , normalizeConfig } from '../utils' ;
1111import './entity' ;
1212import { EntityConfigOrStr } from '../types' ;
13+ import { UNIT_PREFIXES } from '../const' ;
1314
1415@customElement ( 'sankey-chart-editor' )
1516export class SankeyChartEditor extends LitElement implements LovelaceCardEditor {
@@ -70,6 +71,8 @@ export class SankeyChartEditor extends LitElement implements LovelaceCardEditor
7071 [ target . configValue ] : target . checked !== undefined ? target . checked : target . value ,
7172 } ;
7273 }
74+ } else {
75+ this . _config = { ...ev . detail . value } ;
7376 }
7477 this . _updateConfig ( ) ;
7578 }
@@ -141,12 +144,63 @@ export class SankeyChartEditor extends LitElement implements LovelaceCardEditor
141144 fireEvent ( this , 'config-changed' , { config : this . _config } ) ;
142145 }
143146
147+ private _computeSchema ( ) {
148+ return [
149+ // {
150+ // type: 'grid',
151+ // name: '',
152+ // schema: [
153+ // { name: 'autoconfig', selector: { boolean: {} } },
154+ // { name: 'autoconfig.print_yaml', selector: { boolean: {} } },
155+ // ],
156+ // },
157+ { name : 'title' , selector : { text : { } } } ,
158+ { name : 'show_names' , selector : { boolean : { } } } ,
159+ { name : 'show_icons' , selector : { boolean : { } } } ,
160+ { name : 'show_states' , selector : { boolean : { } } } ,
161+ { name : 'show_units' , selector : { boolean : { } } } ,
162+ { name : 'energy_date_selection' , selector : { boolean : { } } } ,
163+ {
164+ type : 'grid' ,
165+ name : '' ,
166+ schema : [
167+ { name : 'wide' , selector : { boolean : { } } } ,
168+ { name : 'height' , selector : { number : { mode : 'box' , unit_of_measurement : 'px' } } } ,
169+ { name : 'min_box_height' , selector : { number : { mode : 'box' , unit_of_measurement : 'px' } } } ,
170+ { name : 'min_box_distance' , selector : { number : { mode : 'box' , unit_of_measurement : 'px' } } } ,
171+ ] ,
172+ } ,
173+ {
174+ type : 'grid' ,
175+ name : '' ,
176+ schema : [
177+ { name : 'min_state' , selector : { number : { mode : 'box' } } } ,
178+ { name : 'round' , selector : { number : { mode : 'box' , unit_of_measurement : localize ( 'editor.decimals' ) } } } ,
179+ { name : 'throttle' , selector : { number : { mode : 'box' , unit_of_measurement : 'ms' } } } ,
180+ {
181+ name : 'unit_prefix' ,
182+ selector : {
183+ select : {
184+ mode : 'dropdown' ,
185+ options : [ { value : '' } , ...Object . keys ( UNIT_PREFIXES ) . map ( key => ( { value : key , label : key } ) ) ] ,
186+ } ,
187+ } ,
188+ } ,
189+ ] ,
190+ } ,
191+ ] ;
192+ }
193+
194+ private _computeLabel = ( schema : { name : string } ) => {
195+ return localize ( 'editor.fields.' + schema . name ) ;
196+ } ;
197+
144198 protected render ( ) : TemplateResult | void {
145199 if ( ! this . hass || ! this . _helpers ) {
146200 return html `` ;
147201 }
148202
149- const config = this . _config || ( { } as SankeyChartConfig ) ;
203+ const config = normalizeConfig ( this . _config || ( { } as SankeyChartConfig ) ) ;
150204 const { autoconfig } = config ;
151205 const sections : SectionConfig [ ] = config . sections || [ ] ;
152206
@@ -166,31 +220,40 @@ export class SankeyChartEditor extends LitElement implements LovelaceCardEditor
166220 return html `
167221 <div class= "card-config" >
168222 <div class= "options" >
169- <h3> ${ localize ( 'editor.autoconfig' ) } </ h3>
170- <ha- for mfield .label = ${ localize ( 'editor.enable' ) } >
171- <ha- switch
172- .checked = ${ ! ! autoconfig }
173- .configValue = ${ ( conf , val : boolean ) => {
174- const newConf = { ...conf } ;
175- if ( val && ! conf . autoconfig ) {
176- newConf . autoconfig = { print_yaml : false } ;
177- } else if ( ! val && conf . autoconfig ) {
178- delete newConf . autoconfig ;
179- }
180- return newConf ;
181- } }
182- @change = ${ this . _valueChanged }
183- > </ ha- switch>
184- </ ha- for mfield>
185- ${ autoconfig
186- ? html `<ha- for mfield .label = ${ localize ( 'editor.print' ) } >
187- <ha- switch
188- .checked = ${ ! ! autoconfig ?. print_yaml }
189- .configValue = ${ ( conf , print_yaml ) => ( { ...conf , autoconfig : { print_yaml } } ) }
190- @change = ${ this . _valueChanged }
191- > </ ha- switch>
192- </ ha- for mfield> `
193- : nothing }
223+ <div class= "autoconfig" >
224+ <ha- for mfield .label = ${ localize ( 'editor.fields.autoconfig' ) } >
225+ <ha- switch
226+ .checked = ${ ! ! autoconfig }
227+ .configValue = ${ ( conf , val : boolean ) => {
228+ const newConf = { ...conf } ;
229+ if ( val && ! conf . autoconfig ) {
230+ newConf . autoconfig = { print_yaml : false } ;
231+ } else if ( ! val && conf . autoconfig ) {
232+ delete newConf . autoconfig ;
233+ }
234+ return newConf ;
235+ } }
236+ @change = ${ this . _valueChanged }
237+ > </ ha- switch>
238+ </ ha- for mfield>
239+ ${ autoconfig
240+ ? html `<ha- for mfield .label = ${ localize ( 'editor.fields.print_yaml' ) } >
241+ <ha- switch
242+ .checked = ${ ! ! autoconfig ?. print_yaml }
243+ .configValue = ${ ( conf , print_yaml ) => ( { ...conf , autoconfig : { print_yaml } } ) }
244+ @change = ${ this . _valueChanged }
245+ > </ ha- switch>
246+ </ ha- for mfield> `
247+ : nothing }
248+ </ div>
249+
250+ <ha- for m
251+ .hass = ${ this . hass }
252+ .data = ${ config }
253+ .schema = ${ this . _computeSchema ( ) }
254+ .computeLabel = ${ this . _computeLabel }
255+ @value-changed = ${ this . _valueChanged }
256+ > </ ha- for m>
194257 </ div>
195258 ${ autoconfig ? nothing : this . _renderSections ( sections ) }
196259 <p> ${ localize ( 'editor.yaml_disclaimer' ) } </ p>
@@ -269,6 +332,11 @@ export class SankeyChartEditor extends LitElement implements LovelaceCardEditor
269332 display : grid;
270333 margin-bottom : 20px ;
271334 }
335+ .autoconfig {
336+ display : flex;
337+ justify-content : space-between;
338+ margin-bottom : 16px ;
339+ }
272340 .sections {
273341 display : flex;
274342 flex-direction : column;
0 commit comments