Skip to content

Commit

Permalink
set correct type and handle simple_commands
Browse files Browse the repository at this point in the history
  • Loading branch information
splattner committed May 3, 2024
1 parent b2b4f26 commit 87e64a0
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion pkg/entities/remote.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package entities

import "slices"

type RemoteEntityState EntityState
type RemoteEntityFeatures EntityFeature
type RemoteEntityAttributes EntityAttribute
Expand All @@ -14,7 +16,7 @@ const (
const (
SendCmdRemoteEntityFeatures RemoteEntityFeatures = "send_cmd"
OnOffRemoteEntityFeatures RemoteEntityFeatures = "on_off"
ToggleRemoteEntityyFeatures RemoteEntityFeatures = "toggle"
ToggleRemoteEntityFeatures RemoteEntityFeatures = "toggle"
)

const (
Expand Down Expand Up @@ -47,9 +49,15 @@ func NewRemoteEntity(id string, name LanguageText, area string) *RemoteEntity {
remoteEntity.Name = name
remoteEntity.Area = area

remoteEntity.EntityType.Type = "remote"

remoteEntity.Commands = make(map[RemoteEntityCommand]func(RemoteEntity, map[string]interface{}) int)
remoteEntity.Attributes = make(map[string]interface{})

// SendCmdRemoteEntityFeatures is always present even if not specified
// https://github.com/unfoldedcircle/core-api/blob/main/doc/entities/entity_remote.md
remoteEntity.AddFeature(SendCmdRemoteEntityFeatures)

remoteEntity.Options = make(map[RemoteEntityOption]interface{})

return &remoteEntity
Expand Down Expand Up @@ -92,6 +100,15 @@ func (e *RemoteEntity) HandleCommand(cmd_id string, params map[string]interface{
return e.Commands[RemoteEntityCommand(cmd_id)](*e, params)
}

// When simple_commands are enabled and the command exists, call the regstisered function if one is set
if e.Options[SimpleCommandsRemoteEntityOption] != nil &&
slices.Contains(e.Options[SimpleCommandsRemoteEntityOption].([]string), cmd_id) {

if e.Commands[RemoteEntityCommand(cmd_id)] != nil {
return e.Commands[RemoteEntityCommand(cmd_id)](*e, params)
}
}

return 404
}

Expand Down

0 comments on commit 87e64a0

Please sign in to comment.