Add Tasmota timezone handling and dashboard updates
This commit is contained in:
@@ -22,6 +22,14 @@ var tasmotaTimeLayouts = []string{
|
||||
}
|
||||
|
||||
func ParseTasmota(message model.RawMessage) ([]model.Record, error) {
|
||||
return ParseTasmotaInLocation(message, time.UTC)
|
||||
}
|
||||
|
||||
func ParseTasmotaInLocation(message model.RawMessage, timezoneLessTimeLocation *time.Location) ([]model.Record, error) {
|
||||
if timezoneLessTimeLocation == nil {
|
||||
timezoneLessTimeLocation = time.UTC
|
||||
}
|
||||
|
||||
parts := strings.Split(message.Topic, "/")
|
||||
if len(parts) != 3 {
|
||||
return nil, fmt.Errorf("unsupported topic shape: %s", message.Topic)
|
||||
@@ -52,7 +60,7 @@ func ParseTasmota(message model.RawMessage) ([]model.Record, error) {
|
||||
return nil, fmt.Errorf("no usable fields in payload for topic %s", message.Topic)
|
||||
}
|
||||
|
||||
timestamp := parsePayloadTimestamp(payload, message.ReceivedAt)
|
||||
timestamp := parsePayloadTimestamp(payload, message.ReceivedAt, timezoneLessTimeLocation)
|
||||
|
||||
record := model.Record{
|
||||
Measurement: measurement,
|
||||
@@ -77,11 +85,14 @@ func parseLWT(message model.RawMessage, measurement string, tags map[string]stri
|
||||
}
|
||||
}
|
||||
|
||||
func parsePayloadTimestamp(payload map[string]any, fallback time.Time) time.Time {
|
||||
func parsePayloadTimestamp(payload map[string]any, fallback time.Time, timezoneLessTimeLocation *time.Location) time.Time {
|
||||
rawTime, ok := payload["Time"].(string)
|
||||
if !ok || strings.TrimSpace(rawTime) == "" {
|
||||
return fallback
|
||||
}
|
||||
if timezoneLessTimeLocation == nil {
|
||||
timezoneLessTimeLocation = time.UTC
|
||||
}
|
||||
|
||||
for _, layout := range tasmotaTimeLayouts {
|
||||
var (
|
||||
@@ -90,13 +101,13 @@ func parsePayloadTimestamp(payload map[string]any, fallback time.Time) time.Time
|
||||
)
|
||||
|
||||
if layout == "2006-01-02T15:04:05" {
|
||||
parsed, err = time.ParseInLocation(layout, rawTime, time.UTC)
|
||||
parsed, err = time.ParseInLocation(layout, rawTime, timezoneLessTimeLocation)
|
||||
} else {
|
||||
parsed, err = time.Parse(layout, rawTime)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
return parsed
|
||||
return parsed.UTC()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -85,6 +85,29 @@ func TestParseTasmotaFixtures(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTasmotaInLocationUsesConfiguredTimezoneForTimezoneLessTime(t *testing.T) {
|
||||
location := time.FixedZone("CET", 3600)
|
||||
receivedAt := time.Date(2026, time.March, 16, 11, 55, 30, 0, time.UTC)
|
||||
|
||||
records, err := ParseTasmotaInLocation(model.RawMessage{
|
||||
Topic: "tele/tasmota_67850B/SENSOR",
|
||||
Payload: []byte(`{"Time":"2026-03-16T11:55:29","ENERGY":{"Power":9}}`),
|
||||
ReceivedAt: receivedAt,
|
||||
}, location)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseTasmotaInLocation returned error: %v", err)
|
||||
}
|
||||
|
||||
if len(records) != 1 {
|
||||
t.Fatalf("expected 1 record, got %d", len(records))
|
||||
}
|
||||
|
||||
want := time.Date(2026, time.March, 16, 10, 55, 29, 0, time.UTC)
|
||||
if !records[0].Timestamp.Equal(want) {
|
||||
t.Fatalf("unexpected timestamp: got %s want %s", records[0].Timestamp.Format(time.RFC3339), want.Format(time.RFC3339))
|
||||
}
|
||||
}
|
||||
|
||||
func fieldEquals(got any, want any) bool {
|
||||
switch typedWant := want.(type) {
|
||||
case float64:
|
||||
|
||||
Reference in New Issue
Block a user