net-daemon / netdaemon

An application daemon for Home Assistant written in .NET
MIT License
219 stars 64 forks source link

HomeAssistantConnectionExtensions.GetDevicesAsync errors when sw_version is integer #1125

Closed bperkinspdx closed 2 months ago

bperkinspdx commented 2 months ago

The problem

NetDaemon fails to start due to an error in HomeAssistantConnectionExtensions.GetDevicesAsync when a device's sw_version is returned as an integer.

Environment

Link to or paste code that causes the issue

Looks like the issue is the result of this commit on pull #1114 https://github.com/net-daemon/netdaemon/blob/7f9914ec87b66c2a4e45eb46261c48f15360f931/src/Client/NetDaemon.HassClient/Common/HomeAssistant/Model/HassDevice.cs#L26

Traceback/Error logs

System.Text.Json.JsonException: The JSON value could not be converted to System.String. Path: $[14].sw_version | LineNumber: 0 | BytePositionInLine: 7227.
 ---> System.InvalidOperationException: Cannot get the value of a token type 'Number' as a string.
   at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_ExpectedString(JsonTokenType tokenType)
   at System.Text.Json.Utf8JsonReader.GetString()
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.ReadJsonAndSetMember(Object obj, ReadStack& state, Utf8JsonReader& reader)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
   at System.Text.Json.Serialization.JsonCollectionConverter`2.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TCollection& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value, Boolean& isPopulatedValue)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, Utf8JsonReader& reader, Exception ex)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo`1 jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](JsonElement element, JsonSerializerOptions options)
   at NetDaemon.Client.Internal.HomeAssistantConnection.SendCommandAndReturnResponseAsync[T,TResult](T command, CancellationToken cancelToken)
   at NetDaemon.Client.HomeAssistant.Extensions.HomeAssistantConnectionExtensions.GetDevicesAsync(IHomeAssistantConnection connection, CancellationToken cancelToken)
   at NetDaemon.HassModel.Internal.RegistryCache.ReloadDevices()
   at NetDaemon.HassModel.Internal.RegistryCache.InitializeAsync(CancellationToken cancellationToken)
   at NetDaemon.HassModel.Internal.CacheManager.InitializeAsync(CancellationToken cancellationToken)
   at NetDaemon.Runtime.Internal.NetDaemonRuntime.OnHomeAssistantClientConnected(IHomeAssistantConnection haConnection, CancellationToken cancelToken)
   at NetDaemon.Runtime.Internal.NetDaemonRuntime.StartAsync(CancellationToken stoppingToken)
   at NetDaemon.Runtime.Internal.RuntimeService.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>b__15_1(IHostedService service, CancellationToken token)
   at Microsoft.Extensions.Hosting.Internal.Host.ForeachService[T](IEnumerable`1 services, CancellationToken token, Boolean concurrent, Boolean abortOnFirstException, List`1 exceptions, Func`3 operation)

Additional information

The device json that causes the error:

{
    "area_id": "living_room",
    "configuration_url": null,
    "config_entries": [
        "3d8e377d4709d69f7c5c26508afdd72b"
    ],
    "connections": [
        [
            "mac",
            "xx:xx:xx:xx:xx:xx"
        ]
    ],
    "disabled_by": null,
    "entry_type": null,
    "hw_version": null,
    "id": "bc3cb5785286c93075a8246d028fe219",
    "identifiers": [
        [
            "broadlink",
            "e8165653c954"
        ]
    ],
    "labels": [
        "humidity",
        "wifi",
        "ir",
        "heat"
    ],
    "manufacturer": "Broadlink",
    "model": "RM4 pro",
    "name_by_user": null,
    "name": "Living Room IR Blaster",
    "serial_number": null,
    "sw_version": 62093,
    "via_device_id": null
}
helto4real commented 2 months ago

@bperkinspdx we have a fix on the works, when released please can you confirm it works and close the issue then?

bperkinspdx commented 2 months ago

@helto4real Yep all fixed now, thanks!