Skip to content

Commit

Permalink
Remove name field from HA registration message
Browse files Browse the repository at this point in the history
This seems to fix the new HA error of:
> MQTT entity name starts with the device name in your config
  • Loading branch information
awilliams committed Oct 22, 2023
1 parent ec54fd9 commit 33b07e7
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ build/bin/*
cmd/wifi-presence/wifi-presence
notes.md
Makefile
out/*
6 changes: 3 additions & 3 deletions cmd/wifi-presence/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,13 +217,13 @@ func run(ctx context.Context, appName string) error {

statusCtx, statusCancel := context.WithTimeout(ctx, 2*time.Second)
defer statusCancel()
if err := mqtt.StatusOnline(statusCtx); err != nil {
if err = mqtt.StatusOnline(statusCtx); err != nil {
return err
}
defer func() {
// Cannot use main context since it may have already been cancelled.
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
_ = mqtt.StatusOffline(ctx)
mqttCloseCtx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
_ = mqtt.StatusOffline(mqttCloseCtx)
cancel()

mqtt.Close()
Expand Down
15 changes: 7 additions & 8 deletions internal/hass/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ type Configuration struct {

// TrackConfig describes a single Wifi station/device to monitor for state changes.
type TrackConfig struct {
Name string `json:"name"`
MAC string `json:"mac"`
Name string `json:"name"`
}

// DeviceTracker is used to configure HomeAssistant to track a device.
Expand All @@ -23,7 +23,6 @@ type DeviceTracker struct {
Device Device `json:"device,omitempty"` // Information about the device this device tracker is a part of that ties it into the device registry. At least one of identifiers or connections must be present to identify the device.
Icon string `json:"icon,omitempty"` // Icon for the entity. https://materialdesignicons.com
JSONAttributesTopic string `json:"json_attributes_topic,omitempty"` // The MQTT topic subscribed to receive a JSON dictionary payload and then set as device_tracker attributes. Usage example can be found in MQTT sensor documentation.
Name string `json:"name,omitempty"` // The name of the MQTT device_tracker.
ObjectID string `json:"object_id,omitempty"` // Used instead of name for automatic generation of entity_id.
PayloadAvailable string `json:"payload_available,omitempty"` // Default: online. The payload that represents the available state.
PayloadHome string `json:"payload_home,omitempty"` // Default: home. The payload value that represents the ‘home’ state for the device.
Expand All @@ -38,21 +37,21 @@ type DeviceTracker struct {
// Device is part of the DeviceTracker configuration.
type Device struct {
Connections [][2]string `json:"connections"` // A list of connections of the device to the outside world as a list of tuples [connection_type, connection_identifier]. For example the MAC address of a network interface: 'connections': ['mac', '02:5b:26:a8:dc:12'].
Name string `json:"name,omitempty"` // The name of the device.
ViaDevice string `json:"via_device,omitempty"` // The name of the device.
Manufacturer string `json:"manufacturer,omitempty"` // The manufacturer of the device.
Name string `json:"name,omitempty"` // The name of the device.
ViaDevice string `json:"via_device,omitempty"` // Identifier of a device that routes messages between this device and Home Assistant. Examples of such devices are hubs, or parent devices of a sub-device. This is used to show device topology in Home Assistant.
}

// Attrs are a device's attributes.
type Attrs struct {
Name string `json:"name"`
MAC string `json:"mac_address"`
IsConnected bool `json:"is_connected"`
APName string `json:"ap_name"`
SSID string `json:"ssid"`
BSSID string `json:"bssid"`
ConnectedAt *time.Time `json:"connected_at,omitempty"`
ConnectedFor int `json:"connected_for,omitempty"`
DisconnectedAt *time.Time `json:"disconnected_at,omitempty"`
DisconnectedFor int `json:"disconnected_for,omitempty"`
IsConnected bool `json:"is_connected"`
MAC string `json:"mac_address"`
Name string `json:"name"`
SSID string `json:"ssid"`
}
1 change: 0 additions & 1 deletion internal/hass/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ func (m *MQTT) RegisterDeviceTracker(ctx context.Context, dsc Discovery) error {
},
Icon: icon,
JSONAttributesTopic: m.topics.DeviceJSONAttrs(dsc.MAC),
Name: fmt.Sprintf("%s %s", dsc.Name, m.apName),
ObjectID: deviceID,
PayloadAvailable: StatusOnline,
PayloadNotAvailable: StatusOffline,
Expand Down
2 changes: 1 addition & 1 deletion internal/presence/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (s staChange) String() string {
case staUpdated:
return "updated"
default:
return "?"
return fmt.Sprintf("?:%d", s)
}
}

Expand Down

0 comments on commit 33b07e7

Please sign in to comment.