Skip to content

Commit

Permalink
refactor(tracker,sensor): combine registration and update sensor structs
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Nov 28, 2023
1 parent a5ff5c6 commit 3aa8a79
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 156 deletions.
49 changes: 20 additions & 29 deletions internal/hass/sensor/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,11 @@ const (
// SensorRegistrationInfo is the JSON structure required to register a sensor
// with HA.
type SensorRegistrationInfo struct {
State interface{} `json:"state"`
StateAttributes interface{} `json:"attributes,omitempty"`
UniqueID string `json:"unique_id"`
Type string `json:"type"`
Name string `json:"name"`
UnitOfMeasurement string `json:"unit_of_measurement,omitempty"`
StateClass string `json:"state_class,omitempty"`
EntityCategory string `json:"entity_category,omitempty"`
Icon string `json:"icon,omitempty"`
DeviceClass string `json:"device_class,omitempty"`
Disabled bool `json:"disabled,omitempty"`
}

func (reg *SensorRegistrationInfo) RequestType() api.RequestType {
return api.RequestTypeRegisterSensor
}

func (reg *SensorRegistrationInfo) RequestData() json.RawMessage {
data, err := json.Marshal(reg)
if err != nil {
log.Debug().Err(err).
Msg("Unable to marshal sensor to json.")
return nil
}
return data
Name string `json:"name,omitempty"`
UnitOfMeasurement string `json:"unit_of_measurement,omitempty"`
StateClass string `json:"state_class,omitempty"`
EntityCategory string `json:"entity_category,omitempty"`
DeviceClass string `json:"device_class,omitempty"`
}

// SensorUpdateInfo is the JSON structure required to update HA with the current
Expand All @@ -56,12 +36,23 @@ type SensorUpdateInfo struct {
UniqueID string `json:"unique_id"`
}

func (upd *SensorUpdateInfo) RequestType() api.RequestType {
return api.RequestTypeUpdateSensorStates
type SensorState struct {
SensorUpdateInfo
SensorRegistrationInfo
Disabled bool `json:"disabled,omitempty"`
Registered bool `json:"-"`
}

func (s *SensorState) RequestType() api.RequestType {
if s.Registered {
return api.RequestTypeUpdateSensorStates
} else {
return api.RequestTypeRegisterSensor
}
}

func (upd *SensorUpdateInfo) RequestData() json.RawMessage {
data, err := json.Marshal(upd)
func (s *SensorState) RequestData() json.RawMessage {
data, err := json.Marshal(s)
if err != nil {
log.Debug().Err(err).
Msg("Unable to marshal sensor to json.")
Expand Down
39 changes: 16 additions & 23 deletions internal/tracker/sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,23 @@ func prettyPrintState(s Sensor) string {
return b.String()
}

func marshalSensorUpdate(s Sensor) *sensor.SensorUpdateInfo {
return &sensor.SensorUpdateInfo{
StateAttributes: s.Attributes(),
Icon: s.Icon(),
State: s.State(),
Type: marshalClass(s.SensorType()),
UniqueID: s.ID(),
}
}

func marshalSensorRegistration(s Sensor) *sensor.SensorRegistrationInfo {
return &sensor.SensorRegistrationInfo{
StateAttributes: s.Attributes(),
DeviceClass: marshalClass(s.DeviceClass()),
Icon: s.Icon(),
Name: s.Name(),
State: s.State(),
Type: marshalClass(s.SensorType()),
UniqueID: s.ID(),
UnitOfMeasurement: s.Units(),
StateClass: marshalClass(s.StateClass()),
EntityCategory: s.Category(),
Disabled: false,
func marshallSensorState(state Sensor, registered bool) *sensor.SensorState {
s := &sensor.SensorState{}
s.StateAttributes = state.Attributes()
s.Icon = state.Icon()
s.State = state.State()
s.Type = marshalClass(state.SensorType())
s.UniqueID = state.ID()
s.Registered = registered
if !s.Registered {
s.Name = state.Name()
s.DeviceClass = marshalClass(state.DeviceClass())
s.StateClass = marshalClass(state.StateClass())
s.UnitOfMeasurement = state.Units()
s.EntityCategory = state.Category()
s.Disabled = false
}
return s
}

type ComparableStringer interface {
Expand Down
98 changes: 0 additions & 98 deletions internal/tracker/sensor_test.go

This file was deleted.

7 changes: 1 addition & 6 deletions internal/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,7 @@ func (t *SensorTracker) send(ctx context.Context, sensorUpdate Sensor) {
return
}
registered := <-t.registry.IsRegistered(sensorUpdate.ID())
switch registered {
case true:
req = marshalSensorUpdate(sensorUpdate)
case false:
req = marshalSensorRegistration(sensorUpdate)
}
req = marshallSensorState(sensorUpdate, registered)
responseCh := make(chan interface{}, 1)
go api.ExecuteRequest(ctx, req, responseCh)
response := <-responseCh
Expand Down

0 comments on commit 3aa8a79

Please sign in to comment.