|
| 1 | +package homeassistant |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/mutablelogic/go-client/pkg/client" |
| 9 | +) |
| 10 | + |
| 11 | +/////////////////////////////////////////////////////////////////////////////// |
| 12 | +// TYPES |
| 13 | + |
| 14 | +type State struct { |
| 15 | + Entity string `json:"entity_id"` |
| 16 | + LastChanged time.Time `json:"last_changed"` |
| 17 | + State string `json:"state"` |
| 18 | + Attributes map[string]any `json:"attributes"` |
| 19 | +} |
| 20 | + |
| 21 | +type Sensor struct { |
| 22 | + Type string `json:"type"` |
| 23 | + Entity string `json:"entity_id"` |
| 24 | + Name string `json:"friendly_name"` |
| 25 | + Value string `json:"state,omitempty"` |
| 26 | + Unit string `json:"unit_of_measurement,omitempty"` |
| 27 | + Class string `json:"device_class,omitempty"` |
| 28 | +} |
| 29 | + |
| 30 | +/////////////////////////////////////////////////////////////////////////////// |
| 31 | +// API CALLS |
| 32 | + |
| 33 | +// States returns all the entities and their state |
| 34 | +func (c *Client) States() ([]State, error) { |
| 35 | + // Return the response |
| 36 | + var response []State |
| 37 | + payload := client.NewRequest(client.ContentTypeJson) |
| 38 | + if err := c.Do(payload, &response, client.OptPath("states")); err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + |
| 42 | + // Return success |
| 43 | + return response, nil |
| 44 | +} |
| 45 | + |
| 46 | +// Sensors returns all sensor entities and their state |
| 47 | +func (c *Client) Sensors() ([]Sensor, error) { |
| 48 | + // Return the response |
| 49 | + var response []State |
| 50 | + payload := client.NewRequest(client.ContentTypeJson) |
| 51 | + if err := c.Do(payload, &response, client.OptPath("states")); err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + // Filter out sensors |
| 56 | + var sensors []Sensor |
| 57 | + for _, state := range response { |
| 58 | + if !strings.HasPrefix(state.Entity, "sensor.") && !strings.HasPrefix(state.Entity, "binary_sensor.") { |
| 59 | + continue |
| 60 | + } |
| 61 | + sensors = append(sensors, Sensor{ |
| 62 | + Type: "sensor", |
| 63 | + Entity: state.Entity, |
| 64 | + Name: state.Name(), |
| 65 | + Value: state.State, |
| 66 | + Unit: state.UnitOfMeasurement(), |
| 67 | + Class: state.DeviceClass(), |
| 68 | + }) |
| 69 | + } |
| 70 | + |
| 71 | + // Return success |
| 72 | + return sensors, nil |
| 73 | +} |
| 74 | + |
| 75 | +// Actuators returns all button, switch and lock entities and their state |
| 76 | +func (c *Client) Actuators() ([]Sensor, error) { |
| 77 | + // Return the response |
| 78 | + var response []State |
| 79 | + payload := client.NewRequest(client.ContentTypeJson) |
| 80 | + if err := c.Do(payload, &response, client.OptPath("states")); err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + // Filter out buttons, locks, and switches |
| 85 | + var sensors []Sensor |
| 86 | + for _, state := range response { |
| 87 | + if !strings.HasPrefix(state.Entity, "button.") && !strings.HasPrefix(state.Entity, "lock.") && !strings.HasPrefix(state.Entity, "switch.") { |
| 88 | + continue |
| 89 | + } |
| 90 | + sensors = append(sensors, Sensor{ |
| 91 | + Type: "actuator", |
| 92 | + Entity: state.Entity, |
| 93 | + Name: state.Name(), |
| 94 | + Value: state.State, |
| 95 | + Class: state.DeviceClass(), |
| 96 | + }) |
| 97 | + } |
| 98 | + |
| 99 | + // Return success |
| 100 | + return sensors, nil |
| 101 | +} |
| 102 | + |
| 103 | +// Lights returns all light entities and their state |
| 104 | +func (c *Client) Lights() ([]Sensor, error) { |
| 105 | + // Return the response |
| 106 | + var response []State |
| 107 | + payload := client.NewRequest(client.ContentTypeJson) |
| 108 | + if err := c.Do(payload, &response, client.OptPath("states")); err != nil { |
| 109 | + return nil, err |
| 110 | + } |
| 111 | + |
| 112 | + // Filter out sensors |
| 113 | + var lights []Sensor |
| 114 | + for _, state := range response { |
| 115 | + if !strings.HasPrefix(state.Entity, "light.") { |
| 116 | + continue |
| 117 | + } |
| 118 | + lights = append(lights, Sensor{ |
| 119 | + Type: "light", |
| 120 | + Entity: state.Entity, |
| 121 | + Name: state.Name(), |
| 122 | + Value: state.State, |
| 123 | + }) |
| 124 | + } |
| 125 | + |
| 126 | + // Return success |
| 127 | + return lights, nil |
| 128 | +} |
| 129 | + |
| 130 | +/////////////////////////////////////////////////////////////////////////////// |
| 131 | +// STRINGIFY |
| 132 | + |
| 133 | +func (s State) String() string { |
| 134 | + data, _ := json.MarshalIndent(s, "", " ") |
| 135 | + return string(data) |
| 136 | +} |
| 137 | + |
| 138 | +func (s Sensor) String() string { |
| 139 | + data, _ := json.MarshalIndent(s, "", " ") |
| 140 | + return string(data) |
| 141 | +} |
| 142 | + |
| 143 | +/////////////////////////////////////////////////////////////////////////////// |
| 144 | +// METHODS |
| 145 | + |
| 146 | +func (s State) Name() string { |
| 147 | + name, ok := s.Attributes["friendly_name"] |
| 148 | + if !ok { |
| 149 | + return s.Entity |
| 150 | + } else if name_, ok := name.(string); !ok { |
| 151 | + return s.Entity |
| 152 | + } else { |
| 153 | + return name_ |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func (s State) DeviceClass() string { |
| 158 | + class, ok := s.Attributes["device_class"] |
| 159 | + if !ok { |
| 160 | + return "" |
| 161 | + } else if class_, ok := class.(string); !ok { |
| 162 | + return "" |
| 163 | + } else { |
| 164 | + return class_ |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +func (s State) UnitOfMeasurement() string { |
| 169 | + unit, ok := s.Attributes["unit_of_measurement"] |
| 170 | + if !ok { |
| 171 | + return "" |
| 172 | + } else if unit_, ok := unit.(string); !ok { |
| 173 | + return "" |
| 174 | + } else { |
| 175 | + return unit_ |
| 176 | + } |
| 177 | +} |
0 commit comments