Skip to content

Commit c3494e9

Browse files
Fix: Support PEM-format CA certificates for Home Assistant
Co-authored-by: thomasneuberger <23504477+thomasneuberger@users.noreply.github.com>
1 parent c0d9af9 commit c3494e9

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

TgHomeBot.SmartHome.HomeAssistant/HomeAssistantMonitor.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class HomeAssistantMonitor(
2727
private readonly CancellationTokenSource _cancellationTokenSource = new();
2828
private readonly X509Certificate2? _caCertificate = string.IsNullOrEmpty(options.Value.CertificateAuthorityPath)
2929
? null
30-
: X509CertificateLoader.LoadCertificateFromFile(options.Value.CertificateAuthorityPath);
30+
: LoadCertificate(options.Value.CertificateAuthorityPath);
3131

3232
private bool _reconnect;
3333

@@ -276,6 +276,15 @@ private async Task ProcessEvent(string message)
276276
}
277277
}
278278

279+
private static X509Certificate2 LoadCertificate(string path)
280+
{
281+
using var reader = new StreamReader(path);
282+
var firstLine = reader.ReadLine();
283+
return firstLine?.Contains("-----BEGIN CERTIFICATE-----") == true
284+
? X509Certificate2.CreateFromPemFile(path)
285+
: X509CertificateLoader.LoadCertificateFromFile(path);
286+
}
287+
279288
private static DeviceState GetState(DeviceStateThresholds deviceThresholds, string state)
280289
{
281290
if (!float.TryParse(state, out var value))

0 commit comments

Comments
 (0)