Skip to content

Commit

Permalink
More template stuff (#2164)
Browse files Browse the repository at this point in the history
- Configure now asks for the users knowledge level, so advanced mode can be reached without calling with `--advanced`
- Add template for solaredge inverter
- Add beta template for sungrow inverter
- Add template for MQTT based meter
- Add more template documentation
- Add check and error message for invalid config properties when using templates
- Option to specify language specific template descriptions
- Option to make params dependent on values of other params 
- Option to add sponsortoken in advanced mode
- Option to set minCurrent in advanced mode
- Remove sponsorship requirement from Daheimladen wallbox
- Multiple fixes (FritzDect meter, Kostsal Piko meter, ...)
  • Loading branch information
DerAndereAndi committed Jan 9, 2022
1 parent 8ba710f commit cfb174f
Show file tree
Hide file tree
Showing 144 changed files with 935 additions and 265 deletions.
2 changes: 1 addition & 1 deletion charger/template.go
Expand Up @@ -26,7 +26,7 @@ func NewChargerFromTemplateConfig(other map[string]interface{}) (api.Charger, er
return nil, err
}

b, _, err := tmpl.RenderResult(false, other)
b, _, err := tmpl.RenderResult(templates.TemplateRenderModeInstance, other)
if err != nil {
return nil, err
}
Expand Down
11 changes: 7 additions & 4 deletions charger/template_test.go
Expand Up @@ -35,14 +35,15 @@ func TestChargerTemplates(t *testing.T) {
tmpl := tmpl

// set default values for all params
values := tmpl.Defaults(true)
values := tmpl.Defaults(templates.TemplateRenderModeUnitTest)

// set the template value which is needed for rendering
values["template"] = tmpl.Template

// set modbus default test values
if values[templates.ParamModbus] != nil {
modbusChoices := tmpl.ModbusChoices()
// we only test one modbus setup
if funk.ContainsString(modbusChoices, templates.ModbusChoiceTCPIP) {
values[templates.ModbusTCPIP] = true
} else {
Expand All @@ -53,15 +54,17 @@ func TestChargerTemplates(t *testing.T) {
t.Run(tmpl.Template, func(t *testing.T) {
t.Parallel()

b, values, err := tmpl.RenderResult(true, values)
b, values, err := tmpl.RenderResult(templates.TemplateRenderModeUnitTest, values)
if err != nil {
t.Logf("%s: %s", tmpl.Template, b)
t.Logf("Template: %s", tmpl.Template)
t.Logf("%s", string(b))
t.Error(err)
}

_, err = NewFromConfig("template", values)
if err != nil && !test.Acceptable(err, acceptable) {
t.Logf("%s", tmpl.Template)
t.Logf("Template: %s", tmpl.Template)
t.Logf("%s", string(b))
t.Error(err)
}
})
Expand Down
1 change: 1 addition & 0 deletions cmd/configure/configure.go
Expand Up @@ -40,6 +40,7 @@ type config struct {
}
Hems string
EEBUS string
MQTT string
SponsorToken string
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/configure/devicetest.go
Expand Up @@ -49,7 +49,7 @@ func (d *DeviceTest) Test() (DeviceTestResult, error) {

// configure creates a configured device from a template so we can test it
func (d *DeviceTest) configure() (interface{}, error) {
b, _, err := d.Template.RenderResult(false, d.ConfigValues)
b, _, err := d.Template.RenderResult(templates.TemplateRenderModeInstance, d.ConfigValues)
if err != nil {
return nil, err
}
Expand Down
25 changes: 13 additions & 12 deletions cmd/configure/flow.go
Expand Up @@ -53,7 +53,7 @@ func (c *CmdConfigure) configureDeviceGuidedSetup() {
// we only ask for the configuration for the first usage
deviceCategory = supportedDeviceCategories[0]

values = c.processConfig(templateItem.Params, deviceCategory)
values = c.processConfig(templateItem, deviceCategory)

deviceItem, err = c.processDeviceValues(values, templateItem, deviceItem, deviceCategory)
if err != nil {
Expand Down Expand Up @@ -86,7 +86,7 @@ func (c *CmdConfigure) configureDeviceGuidedSetup() {
}

fmt.Println()
fmt.Println(templateItem.Description + " " + c.localizedString("Device_Added", nil))
fmt.Println(templateItem.Description.String(c.lang) + " " + c.localizedString("Device_Added", nil))

c.configureLinkedTypes(templateItem)
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func (c *CmdConfigure) configureLinkedTypes(templateItem templates.Template) {
category := DeviceCategory(linkedTemplate.Usage)

localizeMap := localizeMap{
"Linked": linkedTemplateItem.Description,
"Linked": linkedTemplateItem.Description.String(c.lang),
"Article": DeviceCategories[category].article,
"Additional": DeviceCategories[category].additional,
"Category": DeviceCategories[category].title,
Expand All @@ -148,7 +148,6 @@ func (c *CmdConfigure) configureLinkedTypes(templateItem templates.Template) {
}
}
}
fmt.Println("DONE")
}

// configureLinkedTemplate lets the user configure a device that is marked as being linked to a guided device
Expand All @@ -157,7 +156,7 @@ func (c *CmdConfigure) configureLinkedTemplate(template templates.Template, cate
for ok := true; ok; {
deviceItem := device{}

values := c.processConfig(template.Params, category)
values := c.processConfig(template, category)
deviceItem, err := c.processDeviceValues(values, template, deviceItem, category)
if err != nil {
if !errors.Is(err, c.errDeviceNotValid) {
Expand All @@ -173,7 +172,7 @@ func (c *CmdConfigure) configureLinkedTemplate(template templates.Template, cate
c.configuration.AddDevice(deviceItem, category)

fmt.Println()
fmt.Println(template.Description + " " + c.localizedString("Device_Added", nil))
fmt.Println(template.Description.String(c.lang) + " " + c.localizedString("Device_Added", nil))
return true
}
break
Expand All @@ -182,7 +181,7 @@ func (c *CmdConfigure) configureLinkedTemplate(template templates.Template, cate
}

// configureDeviceCategory lets the user select and configure a device from a specific category
func (c *CmdConfigure) configureDeviceCategory(deviceCategory DeviceCategory) (device, error) {
func (c *CmdConfigure) configureDeviceCategory(deviceCategory DeviceCategory) (device, templates.Capabilities, error) {
fmt.Println()
fmt.Printf("- %s %s\n", c.localizedString("Device_Configure", nil), DeviceCategories[deviceCategory].title)

Expand All @@ -191,18 +190,20 @@ func (c *CmdConfigure) configureDeviceCategory(deviceCategory DeviceCategory) (d
}

var deviceDescription string
var capabilities templates.Capabilities

// repeat until the device is added or the user chooses to continue without adding a device
for ok := true; ok; {
fmt.Println()

templateItem, err := c.processDeviceSelection(deviceCategory)
if err != nil {
return device, c.errItemNotPresent
return device, capabilities, c.errItemNotPresent
}

deviceDescription = templateItem.Description
values := c.processConfig(templateItem.Params, deviceCategory)
deviceDescription = templateItem.Description.String(c.lang)
capabilities = templateItem.Capabilities
values := c.processConfig(templateItem, deviceCategory)

device, err = c.processDeviceValues(values, templateItem, device, deviceCategory)
if err != nil {
Expand All @@ -213,7 +214,7 @@ func (c *CmdConfigure) configureDeviceCategory(deviceCategory DeviceCategory) (d
// ask if the user wants to add the
fmt.Println()
if !c.askConfigFailureNextStep() {
return device, err
return device, capabilities, err
}
continue
}
Expand All @@ -231,5 +232,5 @@ func (c *CmdConfigure) configureDeviceCategory(deviceCategory DeviceCategory) (d
fmt.Println()
fmt.Println(deviceDescription + deviceTitle + " " + c.localizedString("Device_Added", nil))

return device, nil
return device, capabilities, nil
}

0 comments on commit cfb174f

Please sign in to comment.