loxhill / go-vee

An unofficial Go wrapper for the Govee API.
Other
1 stars 0 forks source link

Example of State Request and State methods #3

Open lordofscripts opened 1 year ago

lordofscripts commented 1 year ago

I noticed the documentation doesn't have any example of a State Request. Browsing through the code I noticed there was a structure for state requests. I think it would be nice to have a couple of methods for State() such as GetPoweredState() and IsConnected(). I managed to get the information this way:

    stateRequest := c.Client.Device(c.Address, c.Model).State()
    // {Code:200 Message:Success Data:{Device:..... Model:H5083 Properties:[map[online:true] map[powerState:off]] Devices:[]}}
    rsp, err := c.Client.Run(stateRequest)  // govee.GoveeResponse
    if rsp.Code != 200 {
        err = fmt.Errorf("State request failed: %q", rsp.Message)
    } else {
        fmt.Println("\tMAC    :", rsp.Data.Device)
        fmt.Println("\tModel  :", rsp.Data.Model)
        fmt.Println("\tOnline :", rsp.Data.Properties[0]["online"])
        fmt.Println("\tPowered:", rsp.Data.Properties[1]["powerState"])
    }
    return err

But I am ot sure if that is the proper way, especially the use of Data. Devices[]. I only have ONE Govee device so not much I can experiment with.

Other than that I am pretty satisfied, thanks for sharing this useful library. I know there are others but I tried this one and liked it, so it is the one I am using in my pet home project.

Grant3W commented 1 year ago

Hey! Thanks for checking out the package. They're very good points. I'm going to make a start on some additional helpers for state as you mentioned.

I'm also going to add built-in checking for the code returned from the Govee API, so theres no need to manually check like you have in your example.