Skip to content

Commit

Permalink
Re-organize code and minor refactors
Browse files Browse the repository at this point in the history
* new pkg/rest/{turn,toggle}.go files to hold related functions
  • Loading branch information
xx4h committed Oct 1, 2024
1 parent b28cc8d commit cc16d8e
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 83 deletions.
84 changes: 1 addition & 83 deletions pkg/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,52 +100,6 @@ func (h *Hass) api(meth string, path string, payload map[string]string) ([]byte,
return rData, nil
}

func (h *Hass) turn(state string, sub string, obj string) error {
hasDomain, err := h.hasDomainWithService(sub, fmt.Sprintf("turn_%s", state))
if err != nil {
return err
} else if !hasDomain {
return fmt.Errorf("No such Domain with Service: %s with %s", sub, fmt.Sprintf("turn_%s", state))
}
if !h.hasEntityInDomain(obj, sub) {
return fmt.Errorf("No such Entity in Domain: %s in %s", obj, sub)
}
payload := map[string]string{"entity_id": fmt.Sprintf("%s.%s", sub, obj)}
res, err := h.api("POST", fmt.Sprintf("/services/%s/turn_%s", sub, state), payload)
if err != nil {
return err
}

if err := h.getResult(res); err != nil {
return err
}

return nil
}

func (h *Hass) toggle(sub string, obj string) error {
hasDomain, err := h.hasDomainWithService(sub, "toggle")
if err != nil {
return err
} else if !hasDomain {
return fmt.Errorf("No such Domain with Service: %s with %s", sub, "toggle")
}
if !h.hasEntityInDomain(obj, sub) {
return fmt.Errorf("No such Entity in Domain: %s in %s", obj, sub)
}
payload := map[string]string{"entity_id": fmt.Sprintf("%s.%s", sub, obj)}
res, err := h.api("POST", fmt.Sprintf("/services/%s/toggle", sub), payload)
if err != nil {
return err
}

if err := h.getResult(res); err != nil {
return err
}

return nil
}

// TODO: Rework to return result list and work with it
func (h *Hass) getResult(res []byte) error {
var result []HassResult
Expand Down Expand Up @@ -204,41 +158,5 @@ func (h *Hass) entityArgHandler(args []string, service string) (string, string,
} else if len(args) == 2 {
return args[0], args[1], nil
}
return "", "", fmt.Errorf("splitHandler has to many entries in args: %d", len(args))
}

func (h *Hass) TurnOff(args ...string) (string, string, string, error) {
sub, obj, err := h.entityArgHandler(args, "turn_off")
if err != nil {
return "", "", "", err
}
return obj, "off", sub, h.turn("off", sub, obj)
}

func (h *Hass) TurnOn(args ...string) (string, string, string, error) {
sub, obj, err := h.entityArgHandler(args, "turn_on")
if err != nil {
return "", "", "", err
}
return obj, "on", sub, h.turn("on", sub, obj)
}

func (h *Hass) Toggle(args ...string) (string, string, string, error) {
sub, obj, err := h.entityArgHandler(args, "toggle")
if err != nil {
return "", "", "", err
}
return obj, "toggle", sub, h.toggle(sub, obj)
}

func (h *Hass) TurnLightOff(obj string) (string, string, string, error) {
return h.TurnOff("light", obj)
}

func (h *Hass) TurnLightOn(obj string) (string, string, string, error) {
return h.TurnOn("light", obj)
}

func (h *Hass) ToggleLight(obj string) (string, string, string, error) {
return h.Toggle("light", obj)
return "", "", fmt.Errorf("entityArgHandler has to many entries in args: %d", len(args))
}
48 changes: 48 additions & 0 deletions pkg/rest/toggle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 Fabian `xx4h` Sylvester
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package rest

import "fmt"

func (h *Hass) toggle(sub string, obj string) error {
hasDomain, err := h.hasDomainWithService(sub, "toggle")
if err != nil {
return err
} else if !hasDomain {
return fmt.Errorf("No such Domain with Service: %s with %s", sub, "toggle")
}
if !h.hasEntityInDomain(obj, sub) {
return fmt.Errorf("No such Entity in Domain: %s in %s", obj, sub)
}
payload := map[string]string{"entity_id": fmt.Sprintf("%s.%s", sub, obj)}
res, err := h.api("POST", fmt.Sprintf("/services/%s/toggle", sub), payload)
if err != nil {
return err
}

if err := h.getResult(res); err != nil {
return err
}

return nil
}

func (h *Hass) Toggle(args ...string) (string, string, string, error) {
sub, obj, err := h.entityArgHandler(args, "toggle")
if err != nil {
return "", "", "", err
}
return obj, "toggle", sub, h.toggle(sub, obj)
}
68 changes: 68 additions & 0 deletions pkg/rest/turn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2024 Fabian `xx4h` Sylvester
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package rest

import "fmt"

func (h *Hass) turn(state string, sub string, obj string) error {
hasDomain, err := h.hasDomainWithService(sub, fmt.Sprintf("turn_%s", state))
if err != nil {
return err
} else if !hasDomain {
return fmt.Errorf("No such Domain with Service: %s with %s", sub, fmt.Sprintf("turn_%s", state))
}
if !h.hasEntityInDomain(obj, sub) {
return fmt.Errorf("No such Entity in Domain: %s in %s", obj, sub)
}
payload := map[string]string{"entity_id": fmt.Sprintf("%s.%s", sub, obj)}
res, err := h.api("POST", fmt.Sprintf("/services/%s/turn_%s", sub, state), payload)
if err != nil {
return err
}

if err := h.getResult(res); err != nil {
return err
}

return nil
}

func (h *Hass) TurnOff(args ...string) (string, string, string, error) {
sub, obj, err := h.entityArgHandler(args, "turn_off")
if err != nil {
return "", "", "", err
}
return obj, "off", sub, h.turn("off", sub, obj)
}

func (h *Hass) TurnOn(args ...string) (string, string, string, error) {
sub, obj, err := h.entityArgHandler(args, "turn_on")
if err != nil {
return "", "", "", err
}
return obj, "on", sub, h.turn("on", sub, obj)
}

func (h *Hass) TurnLightOff(obj string) (string, string, string, error) {
return h.TurnOff("light", obj)
}

func (h *Hass) TurnLightOn(obj string) (string, string, string, error) {
return h.TurnOn("light", obj)
}

func (h *Hass) ToggleLight(obj string) (string, string, string, error) {
return h.Toggle("light", obj)
}

0 comments on commit cc16d8e

Please sign in to comment.