Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-organize code and minor refactors #2

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
name: release

on:
pull_request:
push:
# run only against tags
tags:
Expand Down
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)
}