From db57434589461f77e7a71c63596f7e86bf0c5c10 Mon Sep 17 00:00:00 2001 From: Jonas Hansen Date: Wed, 10 Jan 2024 20:24:45 +0100 Subject: [PATCH 1/6] Add readme, examples and more discoverydocs. --- README.md | 152 ++++++++++++ ToMqttNet.sln | 8 +- .../MqttAlarmControlPanelDiscoveryConfig.cs | 2 +- .../MqttBinarySensorDiscoveryConfig.cs | 2 +- .../Entities}/MqttButtonDiscoveryConfig.cs | 10 +- .../Entities}/MqttCameraDiscoveryConfig.cs | 2 +- .../Entities}/MqttClimateDiscoveryConfig.cs | 2 +- .../Entities}/MqttCoverDiscoveryConfig.cs | 2 +- .../MqttDefaultLightDiscoveryConfig.cs | 2 +- .../MqttDeviceTrackerDiscoveryConfig.cs | 2 +- .../MqttDeviceTriggerDiscoveryConfig.cs | 2 +- .../Entities}/MqttEventDiscoveryConfig.cs | 2 +- .../Entities}/MqttFanDiscoveryConfig.cs | 2 +- .../MqttHumidifierDiscoveryConfig.cs | 2 +- .../Entities/MqttImageDiscoveryConfig.cs | 85 +++++++ .../Entities}/MqttJsonLightDiscoveryConfig.cs | 2 +- .../Entities/MqttLawnMowerDiscoveryConfig.cs | 121 ++++++++++ .../Entities}/MqttLockDiscoveryConfig.cs | 2 +- .../Entities}/MqttNumberDiscoveryConfig.cs | 2 +- .../Entities}/MqttSceneDiscoveryConfig.cs | 2 +- .../Entities}/MqttSelectDiscoveryConfig.cs | 2 +- .../Entities}/MqttSensorDiscoveryConfig.cs | 2 +- .../Entities/MqttSirenDiscoveryConfig.cs | 171 ++++++++++++++ .../Entities}/MqttSwitchDiscoveryConfig.cs | 2 +- .../MqttTagScannerDiscoveryConfig.cs | 2 +- .../MqttTemplateLightDiscoveryConfig.cs | 2 +- .../Entities/MqttTextDiscoveryConfig.cs | 115 +++++++++ .../Entities/MqttUpdateDiscoveryConfig.cs | 133 +++++++++++ .../Entities}/MqttVacuumDiscoveryConfig.cs | 2 +- .../Entities/MqttValveDiscoveryConfig.cs | 187 +++++++++++++++ .../MqttWaterHeaterDiscoveryConfig.cs | 221 ++++++++++++++++++ .../HomeAssistantDeviceClass.cs | 11 +- .../HomeAssistantDiscoveryNet.csproj | 24 ++ .../HomeAssistantUnits.cs | 11 +- .../MqttDiscoveryConfig.cs | 28 ++- .../MqttDiscoveryJsonContext.cs | 8 +- .../Parsing/CustomMqttDiscoveryConfigType.cs | 5 + .../Parsing/CustomMqttDiscoveryConfigType1.cs | 7 + .../Parsing/IMqttDiscoveryConfigParser.cs | 2 +- .../Parsing/MqttDiscoveryConfigParser.cs | 40 ++-- ...coveryParserServiceCollectionExtensions.cs | 2 +- .../packages.lock.json | 33 +++ src/ToMqttNet/IMqttConnectionService.cs | 1 - src/ToMqttNet/MqttConnectionOptions.cs | 5 +- src/ToMqttNet/MqttConnectionService.cs | 33 +-- ...ttConnectionServiceCollectionExtensions.cs | 11 +- .../MqttConnectionServiceExtensions.cs | 25 +- src/ToMqttNet/MqttCounters.cs | 9 +- .../Parsing/CustomMqttDiscoveryConfigType.cs | 8 - .../Parsing/CustomMqttDiscoveryConfigType1.cs | 13 -- src/ToMqttNet/ToMqttNet.csproj | 9 +- src/ToMqttNet/packages.lock.json | 29 ++- .../MqttBinarySensorDiscoveryConfigTests.cs | 1 + .../MqttConnectionServiceTests.cs | 11 +- .../MqttDefaultLightDiscoveryConfigTests.cs | 1 + .../MqttDiscoveryConfigParserTests.cs | 19 +- .../MqttDiscoveryConfigTests.cs | 7 + .../MqttJsonLightDiscoveryConfigTests.cs | 1 + .../MqttSensorDiscoveryConfigTests.cs | 3 +- .../MqttTemplateLightDiscoveryConfigTests.cs | 1 + .../ToMqttNet.Test.Unit.csproj | 4 +- test/ToMqttNet.Test.Unit/packages.lock.json | 58 ++--- 62 files changed, 1480 insertions(+), 185 deletions(-) create mode 100644 README.md rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttAlarmControlPanelDiscoveryConfig.cs (99%) rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttBinarySensorDiscoveryConfig.cs (99%) rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttButtonDiscoveryConfig.cs (90%) rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttCameraDiscoveryConfig.cs (97%) rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttClimateDiscoveryConfig.cs (99%) rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttCoverDiscoveryConfig.cs (99%) rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttDefaultLightDiscoveryConfig.cs (99%) rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttDeviceTrackerDiscoveryConfig.cs (98%) rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttDeviceTriggerDiscoveryConfig.cs (98%) rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttEventDiscoveryConfig.cs (98%) rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttFanDiscoveryConfig.cs (99%) rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttHumidifierDiscoveryConfig.cs (99%) create mode 100644 src/HomeAssistantDiscoveryNet/Entities/MqttImageDiscoveryConfig.cs rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttJsonLightDiscoveryConfig.cs (99%) create mode 100644 src/HomeAssistantDiscoveryNet/Entities/MqttLawnMowerDiscoveryConfig.cs rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttLockDiscoveryConfig.cs (99%) rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttNumberDiscoveryConfig.cs (99%) rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttSceneDiscoveryConfig.cs (98%) rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttSelectDiscoveryConfig.cs (99%) rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttSensorDiscoveryConfig.cs (99%) create mode 100644 src/HomeAssistantDiscoveryNet/Entities/MqttSirenDiscoveryConfig.cs rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttSwitchDiscoveryConfig.cs (99%) rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttTagScannerDiscoveryConfig.cs (94%) rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttTemplateLightDiscoveryConfig.cs (99%) create mode 100644 src/HomeAssistantDiscoveryNet/Entities/MqttTextDiscoveryConfig.cs create mode 100644 src/HomeAssistantDiscoveryNet/Entities/MqttUpdateDiscoveryConfig.cs rename src/{ToMqttNet/DeviceTypes => HomeAssistantDiscoveryNet/Entities}/MqttVacuumDiscoveryConfig.cs (99%) create mode 100644 src/HomeAssistantDiscoveryNet/Entities/MqttValveDiscoveryConfig.cs create mode 100644 src/HomeAssistantDiscoveryNet/Entities/MqttWaterHeaterDiscoveryConfig.cs rename src/{ToMqttNet => HomeAssistantDiscoveryNet}/HomeAssistantDeviceClass.cs (97%) create mode 100644 src/HomeAssistantDiscoveryNet/HomeAssistantDiscoveryNet.csproj rename src/{ToMqttNet => HomeAssistantDiscoveryNet}/HomeAssistantUnits.cs (98%) rename src/{ToMqttNet => HomeAssistantDiscoveryNet}/MqttDiscoveryConfig.cs (91%) rename src/{ToMqttNet => HomeAssistantDiscoveryNet}/MqttDiscoveryJsonContext.cs (81%) create mode 100644 src/HomeAssistantDiscoveryNet/Parsing/CustomMqttDiscoveryConfigType.cs create mode 100644 src/HomeAssistantDiscoveryNet/Parsing/CustomMqttDiscoveryConfigType1.cs rename src/{ToMqttNet => HomeAssistantDiscoveryNet}/Parsing/IMqttDiscoveryConfigParser.cs (83%) rename src/{ToMqttNet => HomeAssistantDiscoveryNet}/Parsing/MqttDiscoveryConfigParser.cs (72%) rename src/{ToMqttNet => HomeAssistantDiscoveryNet}/Parsing/MqttDiscoveryParserServiceCollectionExtensions.cs (93%) create mode 100644 src/HomeAssistantDiscoveryNet/packages.lock.json delete mode 100644 src/ToMqttNet/Parsing/CustomMqttDiscoveryConfigType.cs delete mode 100644 src/ToMqttNet/Parsing/CustomMqttDiscoveryConfigType1.cs diff --git a/README.md b/README.md new file mode 100644 index 0000000..d63a879 --- /dev/null +++ b/README.md @@ -0,0 +1,152 @@ +# HomeAssistantDiscoveryNet + +This project contains two libraries that are both published to Nuget + +1. `HomeAssistantDiscoveryNet` Library, contains all HA Discovery Documents +2. `ToMqttNet` a managed way to keep a MQTT connection in ASP.NET + +## HomeAssistantDiscoveryNet Library + +Contains the following (Home Assistant MQTT Discovery Documents)[https://www.home-assistant.io/integrations/mqtt/#mqtt-discovery] as C\# classes: + +- AlarmControlPanel +- BinarySensor +- Button +- Camera +- Climate +- Cover +- DefaultLight +- DeviceTracker +- DeviceTrigger +- Event +- Fan +- Humidifier +- Image +- JsonLight +- LawnMower +- Lock +- Number +- Scene +- Select +- Sensor +- Siren +- Switch +- TagScanner +- TemplateLight +- Text +- Update +- Vacuum +- Valve +- WaterHeater + +This makes it easy to create discovery documents from C\# without having to worry about mistyping property names etc. + +It also including de(serilization) using `System.Text.Json` source generators (AoT Compatible) + +### How to serialize discovery docs + +```csharp +var cfg = new MqttBinarySensorDiscoveryConfig +{ + Name = $"Recommend Charging", + ValueTemplate = "{{ 'ON' if value_json.recommend else 'OFF' }}", + StateTopic = $"some-node/status/recommend-charging", + UniqueId = $"some-node-recommend-charging", +}; + +var json = cfg.ToJson(); +``` + +Results in + +```json +{ + "component": "binary_sensor", + "state_topic": "some-node/status/recommend-charging", + "value_template": "{{ 'ON' if value_json.recommend else 'OFF' }}", + "name": "Recommend Charging", + "unique_id": "some-node-recommend-charging" +} +``` + +### How to deserialize discovery docs + +```csharp +var parser = new MqttDiscoveryConfigParser(NullLoggerFactory.Instance, []); +var topic = "some-node/binary/some_sensor"; +var message = "{\"component\": \"binary_sensor\", \"state_topic\": \"some-node/status/recommend-charging\", \"value_template\": \"{{ 'ON' if value_json.recommend else 'OFF' }}\", \"name\": \"Recommend Charging\", \"unique_id\": \"some-node-recommend-charging\"}"; + +var result = parser.Parse(topic, message); + +Assert.IsType(result); +``` + +## ToMqttNet Library + +This sets up a managed connection to a MQTT broker with + +- Makes use of [MQTTnet](https://github.com/dotnet/MQTTnet) for the connection +- Dependency injection +- `System.Diagnostics.Metrics` Based metrics +- AoT compatible +- Home Assistant Discovery integration + +### How to set up the ToMqttNet Library + +In `Program.cs`/`Startup.cs` + +```csharp +var builder = WebApplication.CreateSlimBuilder(args); + +builder.Services.AddMqttConnection() + .Configure>((options, mqttConfI) => + { + var mqttConf = mqttConfI.Value; + options.NodeId = "myintegration"; + options.ClientOptions.ChannelOptions = new MqttClientTcpOptions + { + Server = mqttConf.Server, + Port = mqttConf.Port, + }; + }); + +// ... +``` + +### How to use the ToMqttNet Library + +```csharp +// Remember to register this service in ASP.NET (Or call the .Execute) +public class CounterMqttBackgroundService(MqttConnectionService mqtt) : BackgroundService +{ + private readonly MqttConnectionService _mqtt = mqtt; + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + var discoveryDoc = new MqttSensorDiscoveryConfig() + { + UniqueId = "myintegration_mycounter", + Name = "My Counter", + ValueTemplate = "{{ value_json.value }}", + StateTopic = _mqtt.MqttOptions.NodeId + "/status/counter", // myintegration/status/counter + }; + + await _mqtt.PublishDiscoveryDocument(discoveryDoc); + + var counter = 0L; + while (!stoppingToken.IsCancellationRequested) + { + await _mqtt.PublishAsync(new MqttApplicationMessageBuilder() + .WithTopic(_mqtt.MqttOptions.NodeId + "/status/counter") + .WithPayload(JsonSerializer.Serialize(new {value = counter})) + .Build()); + await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken); + counter++; + } + } +} +``` + +## Examples / usages of HomeAssistantDiscoveryNet and ToMqttNet + +- [JonasMH/Dantherm2Mqtt](https://github.com/JonasMH/Dantherm2Mqtt) diff --git a/ToMqttNet.sln b/ToMqttNet.sln index f6edb7c..3117f0d 100644 --- a/ToMqttNet.sln +++ b/ToMqttNet.sln @@ -5,7 +5,9 @@ VisualStudioVersion = 17.1.32104.313 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToMqttNet", "src\ToMqttNet\ToMqttNet.csproj", "{EBAC9526-DC18-4B97-931F-1962ABE0CA6D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ToMqttNet.Test.Unit", "test\ToMqttNet.Test.Unit\ToMqttNet.Test.Unit.csproj", "{B94A03BA-1137-4F1B-8FE1-99CFFF37A41A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ToMqttNet.Test.Unit", "test\ToMqttNet.Test.Unit\ToMqttNet.Test.Unit.csproj", "{B94A03BA-1137-4F1B-8FE1-99CFFF37A41A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HomeAssistantDiscoveryNet", "src\HomeAssistantDiscoveryNet\HomeAssistantDiscoveryNet.csproj", "{6B17165D-A7C1-48FA-ACAD-F5787C117D09}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,6 +23,10 @@ Global {B94A03BA-1137-4F1B-8FE1-99CFFF37A41A}.Debug|Any CPU.Build.0 = Debug|Any CPU {B94A03BA-1137-4F1B-8FE1-99CFFF37A41A}.Release|Any CPU.ActiveCfg = Release|Any CPU {B94A03BA-1137-4F1B-8FE1-99CFFF37A41A}.Release|Any CPU.Build.0 = Release|Any CPU + {6B17165D-A7C1-48FA-ACAD-F5787C117D09}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6B17165D-A7C1-48FA-ACAD-F5787C117D09}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6B17165D-A7C1-48FA-ACAD-F5787C117D09}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6B17165D-A7C1-48FA-ACAD-F5787C117D09}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/ToMqttNet/DeviceTypes/MqttAlarmControlPanelDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttAlarmControlPanelDiscoveryConfig.cs similarity index 99% rename from src/ToMqttNet/DeviceTypes/MqttAlarmControlPanelDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttAlarmControlPanelDiscoveryConfig.cs index 5863bd7..65ed86d 100644 --- a/src/ToMqttNet/DeviceTypes/MqttAlarmControlPanelDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttAlarmControlPanelDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt alarm panel platform enables the possibility to control MQTT capable alarm panels. The Alarm icon will change state after receiving a new state from state_topic. If these messages are published with RETAIN flag, the MQTT alarm panel will receive an instant state update after subscription and will start with the correct state. Otherwise, the initial state will be unknown. diff --git a/src/ToMqttNet/DeviceTypes/MqttBinarySensorDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttBinarySensorDiscoveryConfig.cs similarity index 99% rename from src/ToMqttNet/DeviceTypes/MqttBinarySensorDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttBinarySensorDiscoveryConfig.cs index 2be9b71..74ab3aa 100644 --- a/src/ToMqttNet/DeviceTypes/MqttBinarySensorDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttBinarySensorDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt binary sensor platform uses an MQTT message received to set the binary sensor’s state to on or off. diff --git a/src/ToMqttNet/DeviceTypes/MqttButtonDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttButtonDiscoveryConfig.cs similarity index 90% rename from src/ToMqttNet/DeviceTypes/MqttButtonDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttButtonDiscoveryConfig.cs index 54e63e1..f47ccde 100644 --- a/src/ToMqttNet/DeviceTypes/MqttButtonDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttButtonDiscoveryConfig.cs @@ -1,11 +1,11 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; - /// - /// The mqtt button platform lets you send an MQTT message when the button is pressed in the frontend or the button press service is called. This can be used to expose some service of a remote device, for example reboot. - /// - public class MqttButtonDiscoveryConfig : MqttDiscoveryConfig +/// +/// The mqtt button platform lets you send an MQTT message when the button is pressed in the frontend or the button press service is called. This can be used to expose some service of a remote device, for example reboot. +/// +public class MqttButtonDiscoveryConfig : MqttDiscoveryConfig { public override string Component => "button"; diff --git a/src/ToMqttNet/DeviceTypes/MqttCameraDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttCameraDiscoveryConfig.cs similarity index 97% rename from src/ToMqttNet/DeviceTypes/MqttCameraDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttCameraDiscoveryConfig.cs index ee826d1..7fbf30f 100644 --- a/src/ToMqttNet/DeviceTypes/MqttCameraDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttCameraDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt camera platform allows you to integrate the content of an image file sent through MQTT into Home Assistant as a camera. Every time a message under the topic in the configuration is received, the image displayed in Home Assistant will also be updated. diff --git a/src/ToMqttNet/DeviceTypes/MqttClimateDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttClimateDiscoveryConfig.cs similarity index 99% rename from src/ToMqttNet/DeviceTypes/MqttClimateDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttClimateDiscoveryConfig.cs index df33529..f976c8c 100644 --- a/src/ToMqttNet/DeviceTypes/MqttClimateDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttClimateDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt climate platform lets you control your MQTT enabled HVAC devices. diff --git a/src/ToMqttNet/DeviceTypes/MqttCoverDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttCoverDiscoveryConfig.cs similarity index 99% rename from src/ToMqttNet/DeviceTypes/MqttCoverDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttCoverDiscoveryConfig.cs index 38a11e3..ef4efc9 100644 --- a/src/ToMqttNet/DeviceTypes/MqttCoverDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttCoverDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt cover platform allows you to control an MQTT cover(such as blinds, a roller shutter or a garage door). diff --git a/src/ToMqttNet/DeviceTypes/MqttDefaultLightDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttDefaultLightDiscoveryConfig.cs similarity index 99% rename from src/ToMqttNet/DeviceTypes/MqttDefaultLightDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttDefaultLightDiscoveryConfig.cs index 2a525b9..f16be20 100644 --- a/src/ToMqttNet/DeviceTypes/MqttDefaultLightDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttDefaultLightDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt light platform lets you control your MQTT enabled lights through one of the supported message schemas, default, json or template. diff --git a/src/ToMqttNet/DeviceTypes/MqttDeviceTrackerDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttDeviceTrackerDiscoveryConfig.cs similarity index 98% rename from src/ToMqttNet/DeviceTypes/MqttDeviceTrackerDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttDeviceTrackerDiscoveryConfig.cs index fc65947..4b2e61c 100644 --- a/src/ToMqttNet/DeviceTypes/MqttDeviceTrackerDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttDeviceTrackerDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt device tracker platform allows you to define new device_trackers through manual YAML configuration in configuration.yaml and also to automatically discover device_trackers through a discovery schema using the MQTT Discovery protocol. diff --git a/src/ToMqttNet/DeviceTypes/MqttDeviceTriggerDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttDeviceTriggerDiscoveryConfig.cs similarity index 98% rename from src/ToMqttNet/DeviceTypes/MqttDeviceTriggerDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttDeviceTriggerDiscoveryConfig.cs index 17cc979..ab1685a 100644 --- a/src/ToMqttNet/DeviceTypes/MqttDeviceTriggerDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttDeviceTriggerDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt device trigger platform uses an MQTT message payload to generate device trigger events. diff --git a/src/ToMqttNet/DeviceTypes/MqttEventDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttEventDiscoveryConfig.cs similarity index 98% rename from src/ToMqttNet/DeviceTypes/MqttEventDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttEventDiscoveryConfig.cs index fea7360..fb0ab89 100644 --- a/src/ToMqttNet/DeviceTypes/MqttEventDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttEventDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; public class MqttEventDiscoveryConfig : MqttDiscoveryConfig { diff --git a/src/ToMqttNet/DeviceTypes/MqttFanDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttFanDiscoveryConfig.cs similarity index 99% rename from src/ToMqttNet/DeviceTypes/MqttFanDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttFanDiscoveryConfig.cs index aa245f3..21eb8ae 100644 --- a/src/ToMqttNet/DeviceTypes/MqttFanDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttFanDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt fan platform lets you control your MQTT enabled fans. diff --git a/src/ToMqttNet/DeviceTypes/MqttHumidifierDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttHumidifierDiscoveryConfig.cs similarity index 99% rename from src/ToMqttNet/DeviceTypes/MqttHumidifierDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttHumidifierDiscoveryConfig.cs index 1b30a3d..f9f3426 100644 --- a/src/ToMqttNet/DeviceTypes/MqttHumidifierDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttHumidifierDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt humidifier platform lets you control your MQTT enabled humidifiers. diff --git a/src/HomeAssistantDiscoveryNet/Entities/MqttImageDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttImageDiscoveryConfig.cs new file mode 100644 index 0000000..02bad75 --- /dev/null +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttImageDiscoveryConfig.cs @@ -0,0 +1,85 @@ +using System.Text.Json.Serialization; + +namespace HomeAssistantDiscoveryNet; + +/// +/// The mqtt image platform allows you to integrate the content of an image file sent through MQTT into Home Assistant as an image. The image platform is a simplified version of the camera platform that only accepts images. Every time a message under the image_topic in the configuration is received, the image displayed in Home Assistant will also be updated. Messages received on image_topic should contain the full contents of an image file, for example, a JPEG image, without any additional encoding or metadata. +/// +public class MqttImageDiscoveryConfig : MqttDiscoveryConfig +{ + public override string Component => "image"; + + /// + /// The content type of and image data message received on image_topic. This option cannot be used with the url_topic because the content type is derived when downloading the image. + /// , default: image/jpeg + /// + [JsonPropertyName("content_type")] + public string? ContentType { get; set; } + + /// + /// Flag which defines if the entity should be enabled when first added. + /// , default: true + /// + [JsonPropertyName("enabled_by_default")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] + public bool? EnabledByDefault { get; set; } + + /// + /// The encoding of the payloads received. Set to "" to disable decoding of incoming payload. Use image_encoding to enable Base64 decoding on image_topic. + /// , default: utf-8 + /// + [JsonPropertyName("encoding")] + public string? Encoding { get; set; } + + /// + /// The category of the entity. + /// , default: None + /// + [JsonPropertyName("entity_category")] + public string? EntityCategory { get; set; } + + /// + /// The encoding of the image payloads received. Set to "b64" to enable base64 decoding of image payload. If not set, the image payload must be raw binary data. + /// , default: None + /// + [JsonPropertyName("image_encoding")] + public string? ImageEncoding { get; set; } + + /// + /// The MQTT topic to subscribe to receive the image payload of the image to be downloaded. Ensure the content_type type option is set to the corresponding content type. This option cannot be used together with the url_topic option. But at least one of these option is required. + /// + [JsonPropertyName("image_topic")] + public string? ImageTopic { get; set; } + + /// + /// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. + /// + [JsonPropertyName("json_attributes_template")] + public string? JsonAttributesTemplate { get; set; } + + /// + /// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Implies force_update of the current sensor state when a message is received on this topic. + /// + [JsonPropertyName("json_attributes_topic")] + public string? JsonAttributesTopic { get; set; } + + /// + /// Used instead of name for automatic generation of entity_id + /// + [JsonPropertyName("object_id")] + public string? ObjectId { get; set; } + + /// + /// Defines a template to extract the image URL from a message received at url_topic. + /// + [JsonPropertyName("url_template")] + public string? UrlTemplate { get; set; } + + /// + /// The MQTT topic to subscribe to receive an image URL. A url_template option can extract the URL from the message. The content_type will be derived from the image when downloaded. This option cannot be used together with the image_topic option, but at least one of these options is required. + /// + [JsonPropertyName("url_topic")] + public string? UrlTopic { get; set; } +} + + diff --git a/src/ToMqttNet/DeviceTypes/MqttJsonLightDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttJsonLightDiscoveryConfig.cs similarity index 99% rename from src/ToMqttNet/DeviceTypes/MqttJsonLightDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttJsonLightDiscoveryConfig.cs index 23ae278..71ed550 100644 --- a/src/ToMqttNet/DeviceTypes/MqttJsonLightDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttJsonLightDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt light platform lets you control your MQTT enabled lights through one of the supported message schemas, default, json or template. diff --git a/src/HomeAssistantDiscoveryNet/Entities/MqttLawnMowerDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttLawnMowerDiscoveryConfig.cs new file mode 100644 index 0000000..51faf75 --- /dev/null +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttLawnMowerDiscoveryConfig.cs @@ -0,0 +1,121 @@ +using System.Text.Json.Serialization; + +namespace HomeAssistantDiscoveryNet; + +/// +/// The mqtt Select platform allows you to integrate devices that might expose configuration options through MQTT into Home Assistant as a Select. Every time a message under the topic in the configuration is received, the select entity will be updated in Home Assistant and vice-versa, keeping the device and Home Assistant in sync. +/// +public class MqttLawnMowerDiscoveryConfig : MqttDiscoveryConfig +{ + public override string Component => "lawn_mower"; + + /// + /// The MQTT topic subscribed to receive an update of the activity. Valid activities are mowing, paused, docked, and error. Use value_template to extract the activity state from a custom payload. When payload none is received, the activity state will be reset to unknown. + /// + [JsonPropertyName("activity_state_topic")] + public string? ActivityStateTopic { get; set; } + + /// + /// Defines a template to extract the value. + /// + [JsonPropertyName("activity_value_template")] + public string? ActivityValueTemplate { get; set; } + + /// + /// Defines a template to generate the payload to send to dock_command_topic. The value parameter in the template will be set to dock. + /// + [JsonPropertyName("dock_command_template")] + public string? DockCommandTemplate { get; set; } + + /// + /// The MQTT topic that publishes commands when the service lawn_mower.dock service call is executed. The value dock is published when the service is called. Use a dock_command_template to publish a custom format. + /// + [JsonPropertyName("dock_command_topic")] + public string? DockCommandTopic { get; set; } + + /// + /// Flag which defines if the entity should be enabled when first added. + /// , default: true + /// + [JsonPropertyName("enabled_by_default")] + public bool? EnabledByDefault { get; set; } + + /// + /// The encoding of the payloads received and published messages. Set to "" to disable decoding of the incoming payload. + /// , default: utf-8 + /// + [JsonPropertyName("encoding")] + public string? Encoding { get; set; } + + /// + /// The category of the entity. + /// , default: None + /// + [JsonPropertyName("entity_category")] + public string? EntityCategory { get; set; } + + /// + /// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. + /// + [JsonPropertyName("json_attributes_template")] + public string? JsonAttributesTemplate { get; set; } + + /// + /// The MQTT topic subscribed to receive a JSON dictionary payload and then set as entity attributes. Implies force_update of the current activity state when a message is received on this topic. + /// + [JsonPropertyName("json_attributes_topic")] + public string? JsonAttributesTopic { get; set; } + + /// + /// Used instead of name for automatic generation of entity_id + /// + [JsonPropertyName("object_id")] + public string? ObjectId { get; set; } + + /// + /// Flag that defines if the lawn mower works in optimistic mode. + /// Default: + /// + ///true if no activity_state_topic defined, else false. + /// + [JsonPropertyName("optimistic")] + public bool? Optimistic { get; set; } + + /// + /// Defines a template to generate the payload to send to pause_command_topic. The value parameter in the template will be set to pause. + /// + [JsonPropertyName("pause_command_template")] + public string? PauseCommandTemplate { get; set; } + + /// + /// The MQTT topic that publishes commands when the service lawn_mower.pause service call is executed. The value pause is published when the service is called. Use a pause_command_template to publish a custom format. + /// + [JsonPropertyName("pause_command_topic")] + public string? PauseCommandTopic { get; set; } + + /// + /// The maximum QoS level to be used when receiving and publishing messages. + /// , default: 0 + /// + [JsonPropertyName("qos")] + public long? Qos { get; set; } + + /// + /// Defines a template to generate the payload to send to dock_command_topic. The value parameter in the template will be set to dock. + /// + [JsonPropertyName("start_mowing_template")] + public string? StartMowingTemplate { get; set; } + + /// + /// The MQTT topic that publishes commands when the service lawn_mower.start_mowing service call is executed. The value start_mowing is published when the service is called. Use a start_mowing_command_template to publish a custom format. + /// + [JsonPropertyName("start_mowing_command_topic")] + public string? StartMowingCommandTopic { get; set; } + + /// + /// If the published message should have the retain flag on or not. + /// , default: false + /// + [JsonPropertyName("retain")] + public bool? Retain { get; set; } +} diff --git a/src/ToMqttNet/DeviceTypes/MqttLockDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttLockDiscoveryConfig.cs similarity index 99% rename from src/ToMqttNet/DeviceTypes/MqttLockDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttLockDiscoveryConfig.cs index db05c0b..1b7448b 100644 --- a/src/ToMqttNet/DeviceTypes/MqttLockDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttLockDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt lock platform lets you control your MQTT enabled locks. diff --git a/src/ToMqttNet/DeviceTypes/MqttNumberDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttNumberDiscoveryConfig.cs similarity index 99% rename from src/ToMqttNet/DeviceTypes/MqttNumberDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttNumberDiscoveryConfig.cs index 573dee6..6de3b37 100644 --- a/src/ToMqttNet/DeviceTypes/MqttNumberDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttNumberDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt Number platform allows you to integrate devices that might expose configuration options through MQTT into Home Assistant as a Number. Every time a message under the topic in the configuration is received, the number entity will be updated in Home Assistant and vice-versa, keeping the device and Home Assistant in-sync. diff --git a/src/ToMqttNet/DeviceTypes/MqttSceneDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttSceneDiscoveryConfig.cs similarity index 98% rename from src/ToMqttNet/DeviceTypes/MqttSceneDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttSceneDiscoveryConfig.cs index 7baade6..2587f31 100644 --- a/src/ToMqttNet/DeviceTypes/MqttSceneDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttSceneDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// diff --git a/src/ToMqttNet/DeviceTypes/MqttSelectDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttSelectDiscoveryConfig.cs similarity index 99% rename from src/ToMqttNet/DeviceTypes/MqttSelectDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttSelectDiscoveryConfig.cs index 6d6b8cd..19348a6 100644 --- a/src/ToMqttNet/DeviceTypes/MqttSelectDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttSelectDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt Select platform allows you to integrate devices that might expose configuration options through MQTT into Home Assistant as a Select. Every time a message under the topic in the configuration is received, the select entity will be updated in Home Assistant and vice-versa, keeping the device and Home Assistant in sync. diff --git a/src/ToMqttNet/DeviceTypes/MqttSensorDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttSensorDiscoveryConfig.cs similarity index 99% rename from src/ToMqttNet/DeviceTypes/MqttSensorDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttSensorDiscoveryConfig.cs index 68b1e70..e9925cc 100644 --- a/src/ToMqttNet/DeviceTypes/MqttSensorDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttSensorDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// This mqtt sensor platform uses the MQTT message payload as the sensor value. If messages in this state_topic are published with RETAIN flag, the sensor will receive an instant update with last known value. Otherwise, the initial state will be undefined. diff --git a/src/HomeAssistantDiscoveryNet/Entities/MqttSirenDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttSirenDiscoveryConfig.cs new file mode 100644 index 0000000..f841774 --- /dev/null +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttSirenDiscoveryConfig.cs @@ -0,0 +1,171 @@ +using System.Text.Json.Serialization; + +namespace HomeAssistantDiscoveryNet; + +/// +/// The mqtt siren platform lets you control your MQTT enabled sirens and text based notification devices. +/// +public class MqttSirenDiscoveryConfig : MqttDiscoveryConfig +{ + public override string Component => "siren"; + + + /// + /// A list of available tones the siren supports. When configured, this enables the support for setting a tone and enables the tone state attribute. + /// + [JsonPropertyName("available_tones")] + public List? AvailableTones { get; set; } + + /// + /// Defines a template to generate a custom payload to send to command_topic. The variable value will be assigned with the configured payload_on or payload_off setting. The siren turn on service parameters tone, volume_level or duration can be used as variables in the template. When operation in optimistic mode the corresponding state attributes will be set. Turn on parameters will be filtered if a device misses the support. + /// + [JsonPropertyName("command_template")] + public string? CommandTemplate { get; set; } + + /// + /// Defines a template to generate a custom payload to send to command_topic when the siren turn off service is called. By default command_template will be used as template for service turn off. The variable value will be assigned with the configured payload_off setting. + /// + [JsonPropertyName("command_off_template")] + public string? CommandOffTemplate { get; set; } + + /// + /// The MQTT topic to publish commands to change the siren state. Without command templates, a default JSON payload like {"state":"ON", "tone": "bell", "duration": 10, "volume_level": 0.5 } is published. When the siren turn on service is called, the startup parameters will be added to the JSON payload. The state value of the JSON payload will be set to the the payload_on or payload_off configured payload. + /// + [JsonPropertyName("command_topic")] + public string? CommandTopic { get; set; } + + /// + /// Flag which defines if the entity should be enabled when first added. + /// , default: true + /// + [JsonPropertyName("enabled_by_default")] + public bool? EnabledByDefault { get; set; } + + /// + /// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload. + /// , default: utf-8 + /// + [JsonPropertyName("encoding")] + public string? Encoding { get; set; } + + /// + /// The category of the entity. + /// , default: None + /// + [JsonPropertyName("entity_category")] + public string? EntityCategory { get; set; } + + + /// + /// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation. + /// + [JsonPropertyName("json_attributes_template")] + public string? JsonAttributesTemplate { get; set; } + + /// + /// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Usage example can be found in MQTT sensor documentation. + /// + [JsonPropertyName("json_attributes_topic")] + public string? JsonAttributesTopic { get; set; } + + /// + /// Used instead of name for automatic generation of entity_id + /// + [JsonPropertyName("object_id")] + public string? ObjectId { get; set; } + + /// + /// Flag that defines if siren works in optimistic mode. + /// Default: + /// + ///true if no state_topic defined, else false. + /// + [JsonPropertyName("optimistic")] + public bool? Optimistic { get; set; } + + /// + /// The payload that represents the available state. + /// , default: online + /// + [JsonPropertyName("payload_available")] + public string? PayloadAvailable { get; set; } + + /// + /// The payload that represents the unavailable state. + /// , default: offline + /// + [JsonPropertyName("payload_not_available")] + public string? PayloadNotAvailable { get; set; } + + /// + /// The payload that represents off state. If specified, will be used for both comparing to the value in the state_topic (see value_template and state_off for details) and sending as off command to the command_topic. + /// , default: OFF + /// + [JsonPropertyName("payload_off")] + public string? PayloadOff { get; set; } + + /// + /// The payload that represents on state. If specified, will be used for both comparing to the value in the state_topic (see value_template and state_on for details) and sending as on command to the command_topic. + /// , default: ON + /// + [JsonPropertyName("payload_on")] + public string? PayloadOn { get; set; } + + /// + /// The maximum QoS level to be used when receiving and publishing messages. + /// , default: 0 + /// + [JsonPropertyName("qos")] + public long? Qos { get; set; } + + /// + /// If the published message should have the retain flag on or not. + /// , default: false + /// + [JsonPropertyName("retain")] + public bool? Retain { get; set; } + + /// + /// The payload that represents the off state. Used when value that represents off state in the state_topic is different from value that should be sent to the command_topic to turn the device off. + /// Default: + /// + ///payload_off if defined, else 'OFF' + /// + [JsonPropertyName("state_off")] + public string? StateOff { get; set; } + + /// + /// The payload that represents the on state. Used when value that represents on state in the state_topic is different from value that should be sent to the command_topic to turn the device on. + /// Default: + /// + ///payload_on if defined, else 'ON' + /// + [JsonPropertyName("state_on")] + public string? StateOn { get; set; } + + /// + /// The MQTT topic subscribed to receive state updates. The state update may be either JSON or a simple string. When a JSON payload is detected, the state value of the JSON payload should supply the payload_on or payload_off defined payload to turn the siren on or off. Additionally, the state attributes duration, tone and volume_level can be updated. Use value_template to transform the received state udpate to a compliant JSON payload. Attributes will only be set if the function is supported by the device and a valid value is supplied. When a non JSON payload is detected, it should be either of the payload_on or payload_off defined payloads or None to reset the siren’s state to unknown. The initial state will be unknown. The state will be reset to unknown if a None payload or null JSON value is received as a state update. + /// + [JsonPropertyName("state_topic")] + public string? StateTopic { get; set; } + + /// + /// Defines a template to extract device’s state from the state_topic. To determine the siren’s state result of this template will be compared to state_on and state_off. Alternatively value_template can be used to render to a valid JSON payload. + /// + [JsonPropertyName("state_value_template")] + public string? StateValueTemplate { get; set; } + + /// + /// Set to true if the MQTT siren supports the duration service turn on parameter and enables the duration state attribute. + /// , default: true + /// + [JsonPropertyName("support_duration")] + public bool? SupportDuration { get; set; } + + /// + /// Set to true if the MQTT siren supports the volume_set service turn on parameter and enables the volume_level state attribute. + /// , default: true + /// + [JsonPropertyName("support_volume_set")] + public bool? SupportVolumeSet { get; set; } +} diff --git a/src/ToMqttNet/DeviceTypes/MqttSwitchDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttSwitchDiscoveryConfig.cs similarity index 99% rename from src/ToMqttNet/DeviceTypes/MqttSwitchDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttSwitchDiscoveryConfig.cs index 8aa90f7..8e3d7c5 100644 --- a/src/ToMqttNet/DeviceTypes/MqttSwitchDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttSwitchDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt switch platform lets you control your MQTT enabled switches. diff --git a/src/ToMqttNet/DeviceTypes/MqttTagScannerDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttTagScannerDiscoveryConfig.cs similarity index 94% rename from src/ToMqttNet/DeviceTypes/MqttTagScannerDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttTagScannerDiscoveryConfig.cs index faa078b..f42962c 100644 --- a/src/ToMqttNet/DeviceTypes/MqttTagScannerDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttTagScannerDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt tag scanner platform uses an MQTT message payload to generate tag scanned events. diff --git a/src/ToMqttNet/DeviceTypes/MqttTemplateLightDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttTemplateLightDiscoveryConfig.cs similarity index 99% rename from src/ToMqttNet/DeviceTypes/MqttTemplateLightDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttTemplateLightDiscoveryConfig.cs index ecd89c1..16f25a3 100644 --- a/src/ToMqttNet/DeviceTypes/MqttTemplateLightDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttTemplateLightDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt light platform lets you control your MQTT enabled lights through one of the supported message schemas, default, json or template. diff --git a/src/HomeAssistantDiscoveryNet/Entities/MqttTextDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttTextDiscoveryConfig.cs new file mode 100644 index 0000000..e1f4219 --- /dev/null +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttTextDiscoveryConfig.cs @@ -0,0 +1,115 @@ +using System.Text.Json.Serialization; + +namespace HomeAssistantDiscoveryNet; + +/// +/// The mqtt Text platform allows you to integrate devices that show text that can be set remotely. Optionally the text state can be monitored too using MQTT. +/// +public class MqttTextDiscoveryConfig : MqttDiscoveryConfig +{ + public override string Component => "text"; + + /// + /// Defines a template to generate the payload to send to command_topic. + /// + [JsonPropertyName("command_template")] + public string? CommandTemplate { get; set; } + + /// + /// The MQTT topic to publish the text value that is set. + /// + [JsonPropertyName("command_topic")] + public string CommandTopic { get; set; } = ""; + + /// + /// Flag which defines if the entity should be enabled when first added. + /// , default: true + /// + [JsonPropertyName("enabled_by_default")] + public bool? EnabledByDefault { get; set; } + + /// + /// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload. + /// , default: utf-8 + /// + [JsonPropertyName("encoding")] + public string? Encoding { get; set; } + + /// + /// The category of the entity. + /// , default: None + /// + [JsonPropertyName("entity_category")] + public string? EntityCategory { get; set; } + + /// + /// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. + /// + [JsonPropertyName("json_attributes_template")] + public string? JsonAttributesTemplate { get; set; } + + /// + /// The MQTT topic subscribed to receive a JSON dictionary payload and then set as entity attributes. Implies force_update of the current select state when a message is received on this topic. + /// + [JsonPropertyName("json_attributes_topic")] + public string? JsonAttributesTopic { get; set; } + + /// + /// The maximum size of a text being set or received (maximum is 255). + /// , default: 255 + /// + [JsonPropertyName("max")] + public long? Max { get; set; } + + /// + /// The minimum size of a text being set or received. + /// , default: 0 + /// + [JsonPropertyName("min")] + public long? Min { get; set; } + + /// + /// The mode off the text entity. Must be either text or password. + /// , default: text + /// + [JsonPropertyName("mode")] + public string? Mode { get; set; } + + /// + /// Used instead of name for automatic generation of entity_id + /// + [JsonPropertyName("object_id")] + public string? ObjectId { get; set; } + + /// + /// A valid regular expression the text being set or received must match with. + /// + [JsonPropertyName("pattern")] + public string? Pattern { get; set; } + + /// + /// The maximum QoS level to be used when receiving and publishing messages. + /// , default: 0 + /// + [JsonPropertyName("qos")] + public long? Qos { get; set; } + + /// + /// If the published message should have the retain flag on or not. + /// , default: false + /// + [JsonPropertyName("retain")] + public bool? Retain { get; set; } + + /// + /// The MQTT topic subscribed to receive text state updates. Text state updates should match the pattern (if set) and meet the size constraints min and max. Can be used with value_template to render the incoming payload to a text update. + /// + [JsonPropertyName("state_topic")] + public string? StateTopic { get; set; } + + /// + /// Defines a template to extract the text state value from the payload received on state_topic. + /// + [JsonPropertyName("value_template")] + public string? ValueTemplate { get; set; } +} diff --git a/src/HomeAssistantDiscoveryNet/Entities/MqttUpdateDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttUpdateDiscoveryConfig.cs new file mode 100644 index 0000000..4d12174 --- /dev/null +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttUpdateDiscoveryConfig.cs @@ -0,0 +1,133 @@ +using System.Text.Json.Serialization; + +namespace HomeAssistantDiscoveryNet; + +/// +/// The mqtt tag scanner platform uses an MQTT message payload to generate tag scanned events. +/// +public class MqttUpdateDiscoveryConfig : MqttDiscoveryConfig +{ + public override string Component => "update"; + + + /// + /// The MQTT topic to publish payload_install to start installing process. + /// + [JsonPropertyName("command_topic")] + public string? CommandTopic { get; set; } + + /// + /// The type/class of the update to set the icon in the frontend. The device_class can be null. + /// , default: None + /// + [JsonPropertyName("device_class")] + public string? DeviceClass { get; set; } + + /// + /// Flag which defines if the entity should be enabled when first added. + /// , default: true + /// + [JsonPropertyName("enabled_by_default")] + public bool? EnabledByDefault { get; set; } + + /// + /// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload. + /// , default: utf-8 + /// + [JsonPropertyName("encoding")] + public string? Encoding { get; set; } + + /// + /// The category of the entity. + /// , default: None + /// + [JsonPropertyName("entity_category")] + public string? EntityCategory { get; set; } + + /// + /// Picture URL for the entity. + /// + [JsonPropertyName("entity_picture")] + public string? EntityPicture { get; set; } + + /// + /// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. + /// + [JsonPropertyName("json_attributes_template")] + public string? JsonAttributesTemplate { get; set; } + + /// + /// The MQTT topic subscribed to receive a JSON dictionary payload and then set as entity attributes. Implies force_update of the current select state when a message is received on this topic. + /// + [JsonPropertyName("json_attributes_topic")] + public string? JsonAttributesTopic { get; set; } + + /// + /// Defines a template to extract the latest version value. + /// + [JsonPropertyName("latest_version_template")] + public string? LatestVersionTemplate { get; set; } + + /// + /// The MQTT topic subscribed to receive an update of the latest version. + /// + [JsonPropertyName("latest_version_topic")] + public string? LatestVersionTopic { get; set; } + + + /// + /// Used instead of name for automatic generation of entity_id + /// + [JsonPropertyName("object_id")] + public string? ObjectId { get; set; } + + /// + /// The MQTT payload to start installing process. + /// + [JsonPropertyName("payload_install")] + public string? PayloadInstall { get; set; } + + /// + /// The maximum QoS level to be used when receiving and publishing messages. + /// , default: 0 + /// + [JsonPropertyName("qos")] + public long? Qos { get; set; } + + /// + /// Summary of the release notes or changelog. This is suitable a brief update description of max 255 characters. + /// + [JsonPropertyName("release_summary")] + public string? ReleaseSummary { get; set; } + + /// + /// URL to the full release notes of the latest version available. + /// + [JsonPropertyName("release_url")] + public string? ReleaseUrl { get; set; } + + /// + /// If the published message should have the retain flag on or not. + /// , default: false + /// + [JsonPropertyName("retain")] + public bool? Retain { get; set; } + + /// + /// The MQTT topic subscribed to receive state updates. The state update may be either JSON or a simple string with installed_version value. When a JSON payload is detected, the state value of the JSON payload should supply the installed_version and can optional supply: latest_version, title, release_summary, release_url or an entity_picture URL. + /// + [JsonPropertyName("state_topic")] + public string? StateTopic { get; set; } + + /// + /// Title of the software, or firmware update. This helps to differentiate between the device or entity name versus the title of the software installed. + /// + [JsonPropertyName("title")] + public string? Title { get; set; } + + /// + /// Defines a template to extract the installed_version state value or to render to a valid JSON payload on from the payload received on state_topic. + /// + [JsonPropertyName("value_template")] + public string? ValueTemplate { get; set; } +} diff --git a/src/ToMqttNet/DeviceTypes/MqttVacuumDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttVacuumDiscoveryConfig.cs similarity index 99% rename from src/ToMqttNet/DeviceTypes/MqttVacuumDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/Entities/MqttVacuumDiscoveryConfig.cs index 912ea16..800ffd0 100644 --- a/src/ToMqttNet/DeviceTypes/MqttVacuumDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttVacuumDiscoveryConfig.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /// /// The mqtt vacuum integration allows you to control your MQTT-enabled vacuum. There are two possible message schemas - legacy and state, chosen by setting the schema configuration parameter. New installations should use the state schema as legacy is deprecated and might be removed someday in the future. The state schema is preferred because the vacuum will then be represented as a StateVacuumDevice which is the preferred parent vacuum entity. diff --git a/src/HomeAssistantDiscoveryNet/Entities/MqttValveDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttValveDiscoveryConfig.cs new file mode 100644 index 0000000..48899ba --- /dev/null +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttValveDiscoveryConfig.cs @@ -0,0 +1,187 @@ +using System.Text.Json.Serialization; + +namespace HomeAssistantDiscoveryNet; + +/// +/// The mqtt valve platform allows you to control an MQTT valve (such a gas or water valve). +/// +public class MqttValveDiscoveryConfig : MqttDiscoveryConfig +{ + public override string Component => "valve"; + + /// + /// Defines a template to generate the payload to send to command_topic. + /// + [JsonPropertyName("command_template")] + public string? CommandTemplate { get; set; } + + /// + /// The MQTT topic to publish commands to control the valve. The value sent can be a value defined by payload_open, payload_close or payload_stop. If reports_position is set to true, a numeric value will be published instead. + /// + [JsonPropertyName("command_topic")] + public string? CommandTopic { get; set; } + + /// + /// Sets the class of the device, changing the device state and icon that is displayed on the frontend. The device_class can be null. + /// , default: None + /// + [JsonPropertyName("device_class")] + public string? DeviceClass { get; set; } + + /// + /// Flag which defines if the entity should be enabled when first added. + /// , default: true + /// + [JsonPropertyName("enabled_by_default")] + public bool? EnabledByDefault { get; set; } + + /// + /// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload. + /// , default: utf-8 + /// + [JsonPropertyName("encoding")] + public string? Encoding { get; set; } + + /// + /// The category of the entity. + /// , default: None + /// + [JsonPropertyName("entity_category")] + public string? EntityCategory { get; set; } + + /// + /// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. A usage example can be found in the MQTT sensor documentation. + /// + [JsonPropertyName("json_attributes_template")] + public string? JsonAttributesTemplate { get; set; } + + /// + /// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. A usage example can be found in MQTT sensor documentation. + /// + [JsonPropertyName("json_attributes_topic")] + public string? JsonAttributesTopic { get; set; } + + /// + /// Used instead of name to have the entity_id generated automatically. + /// + [JsonPropertyName("object_id")] + public string? ObjectId { get; set; } + + /// + /// Flag that defines if a switch works in optimistic mode. + /// Default: + /// + ///false if the state or position topic is defined; true otherwise. + /// + [JsonPropertyName("optimistic")] + public bool? Optimistic { get; set; } + + /// + /// The payload that represents the online state. + /// , default: online + /// + [JsonPropertyName("payload_available")] + public string? PayloadAvailable { get; set; } + + /// + /// The command payload that closes the valve. Is only used when reports_position is set to false (default). The payload_close is not allowed if reports_position is set to true. Can be set to null to disable the valve’s close option. + /// , default: CLOSE + /// + [JsonPropertyName("payload_close")] + public string? PayloadClose { get; set; } + + /// + /// The payload that represents the offline state. + /// , default: offline + /// + [JsonPropertyName("payload_not_available")] + public string? PayloadNotAvailable { get; set; } + + /// + /// The command payload that opens the valve. Is only used when reports_position is set to false (default). The payload_open is not allowed if reports_position is set to true. Can be set to null to disable the valve’s open option. + /// , default: OPEN + /// + [JsonPropertyName("payload_open")] + public string? PayloadOpen { get; set; } + + /// + /// The command payload that stops the valve. When not configured, the valve will not support the valve.stop service. + /// + [JsonPropertyName("payload_stop")] + public string? PayloadStop { get; set; } + + /// + /// Number which represents closed position. The valve’s position will be scaled to the(position_closed…position_open) range when a service is called and scaled back when a value is received. + /// , default: 0 + /// + [JsonPropertyName("position_closed")] + public long? PositionClosed { get; set; } + + /// + /// Number which represents open position. The valve’s position will be scaled to (position_closed…position_open) range when a service is called and scaled back when a value is received. + /// , default: 100 + /// + [JsonPropertyName("position_open")] + public long? PositionOpen { get; set; } + + /// + /// The maximum QoS level to be used when receiving and publishing messages. + /// , default: 0 + /// + [JsonPropertyName("qos")] + public long? Qos { get; set; } + + /// + /// Set to true if the value reports the position or supports setting the position. Enabling the reports_position option will cause the position to be published instead of a payload defined by payload_open, payload_close or payload_stop. When receiving messages, state_topic will accept numeric payloads or one of the following state messages: open, opening, closed, or closing. + /// , default: false + /// + [JsonPropertyName("reports_position")] + public bool? ReportsPosition { get; set; } + + /// + /// Defines if published messages should have the retain flag set. + /// , default: false + /// + [JsonPropertyName("retain")] + public bool? Retain { get; set; } + + /// + /// The payload that represents the closed state. Is only allowed when reports_position is set to False (default). + /// , default: closed + /// + [JsonPropertyName("state_closed")] + public string? StateClosed { get; set; } + + /// + /// The payload that represents the closing state. + /// , default: closing + /// + [JsonPropertyName("state_closing")] + public string? StateClosing { get; set; } + + /// + /// The payload that represents the open state. Is only allowed when reports_position is set to False (default). + /// , default: open + /// + [JsonPropertyName("state_open")] + public string? StateOpen { get; set; } + + /// + /// The payload that represents the opening state. + /// , default: opening + /// + [JsonPropertyName("state_opening")] + public string? StateOpening { get; set; } + + /// + /// The MQTT topic subscribed to receive valve state messages. State topic accepts a state payload (open, opening, closed, or closing) or, if reports_position is supported, a numeric value representing the position. In a JSON format with variables state and position both values can received together. + /// + [JsonPropertyName("state_topic")] + public string? StateTopic { get; set; } + + /// + /// Defines a template that can be used to extract the payload for the state_topic topic. The rendered value should be a defined state payload or, if reporting a position is supported and reports_position is set to true, a numeric value is expected representing the position. See also state_topic. + /// + [JsonPropertyName("value_template")] + public string? ValueTemplate { get; set; } +} diff --git a/src/HomeAssistantDiscoveryNet/Entities/MqttWaterHeaterDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttWaterHeaterDiscoveryConfig.cs new file mode 100644 index 0000000..b8710d7 --- /dev/null +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttWaterHeaterDiscoveryConfig.cs @@ -0,0 +1,221 @@ +using System.Text.Json.Serialization; + +namespace HomeAssistantDiscoveryNet; + +/// +/// The mqtt tag scanner platform uses an MQTT message payload to generate tag scanned events. +/// +public class MqttWaterHeaterDiscoveryConfig : MqttDiscoveryConfig +{ + public override string Component => "water_heater"; + + /// + /// A template with which the value received on current_temperature_topic will be rendered. + /// + [JsonPropertyName("current_temperature_template")] + public string? CurrentTemperatureTemplate { get; set; } + + /// + /// The MQTT topic on which to listen for the current temperature. A "None" value received will reset the current temperature. Empty values (''') will be ignored. + /// + [JsonPropertyName("current_temperature_topic")] + public string? CurrentTemperatureTopic { get; set; } + + /// + /// Flag which defines if the entity should be enabled when first added. + /// , default: true + /// + [JsonPropertyName("enabled_by_default")] + public bool? EnabledByDefault { get; set; } + + /// + /// The encoding of the payloads received and published messages. Set to "" to disable decoding of incoming payload. + /// , default: utf-8 + /// + [JsonPropertyName("encoding")] + public string? Encoding { get; set; } + + /// + /// The category of the entity. + /// , default: None + /// + [JsonPropertyName("entity_category")] + public string? EntityCategory { get; set; } + + /// + /// Set the initial target temperature. The default value depends on the temperature unit, and will be 43.3°C or 110°F. + /// + [JsonPropertyName("initial")] + public long? Initial { get; set; } + + /// + /// Defines a template to extract the JSON dictionary from messages received on the json_attributes_topic. Usage example can be found in MQTT sensor documentation. + /// + [JsonPropertyName("json_attributes_template")] + public string? JsonAttributesTemplate { get; set; } + + /// + /// The MQTT topic subscribed to receive a JSON dictionary payload and then set as sensor attributes. Usage example can be found in MQTT sensor documentation. + /// + [JsonPropertyName("json_attributes_topic")] + public string? JsonAttributesTopic { get; set; } + + /// + /// Maximum set point available. The default value depends on the temperature unit, and will be 60°C or 140°F. + /// + [JsonPropertyName("max_temp")] + public double? MaxTemp { get; set; } + + /// + /// Minimum set point available. The default value depends on the temperature unit, and will be 43.3°C or 110°F. + /// + [JsonPropertyName("min_temp")] + public double? MinTemp { get; set; } + + /// + /// A template to render the value sent to the mode_command_topic with. + /// + [JsonPropertyName("mode_command_template")] + public string? ModeCommandTemplate { get; set; } + + /// + /// The MQTT topic to publish commands to change the water heater operation mode. + /// + [JsonPropertyName("mode_command_topic")] + public string? ModeCommandTopic { get; set; } + + /// + /// A template to render the value received on the mode_state_topic with. + /// + [JsonPropertyName("mode_state_template")] + public string? ModeStateTemplate { get; set; } + + /// + /// The MQTT topic to subscribe for changes of the water heater operation mode. If this is not set, the operation mode works in optimistic mode (see below). + /// + [JsonPropertyName("mode_state_topic")] + public string? ModeStateTopic { get; set; } + + /// + /// A list of supported modes. Needs to be a subset of the default values. + /// Default: + /// + ///[“off”, “eco”, “electric”, “gas”, “heat_pump”, “high_demand”, “performance”] + /// + [JsonPropertyName("modes")] + public List? Modes { get; set; } + + /// + /// Used instead of name for automatic generation of entity_id + /// + [JsonPropertyName("object_id")] + public string? ObjectId { get; set; } + + /// + /// Flag that defines if the water heater works in optimistic mode + /// Default: + /// + ///true if no state topic defined, else false. + /// + [JsonPropertyName("optimistic")] + public bool? Optimistic { get; set; } + + /// + /// The payload that represents the available state. + /// , default: online + /// + [JsonPropertyName("payload_available")] + public string? PayloadAvailable { get; set; } + + /// + /// The payload that represents the unavailable state. + /// , default: offline + /// + [JsonPropertyName("payload_not_available")] + public string? PayloadNotAvailable { get; set; } + + /// + /// The payload that represents disabled state. + /// , default: OFF + /// + [JsonPropertyName("payload_off")] + public string? PayloadOff { get; set; } + + /// + /// The payload that represents enabled state. + /// , default: ON + /// + [JsonPropertyName("payload_on")] + public string? PayloadOn { get; set; } + + /// + /// A template to render the value sent to the power_command_topic with. The value parameter is the payload set for payload_on or payload_off. + /// + [JsonPropertyName("power_command_template")] + public string? PowerCommandTemplate { get; set; } + + /// + /// The MQTT topic to publish commands to change the water heater power state. Sends the payload configured with payload_on if the water heater is turned on via the water_heater.turn_on, or the payload configured with payload_off if the water heater is turned off via the water_heater.turn_off service. Note that optimistic mode is not supported through water_heater.turn_on and water_heater.turn_off services. When called, these services will send a power command to the device but will not optimistically update the state of the water heater. The water heater device should report its state back via mode_state_topic. + /// + [JsonPropertyName("power_command_topic")] + public string? PowerCommandTopic { get; set; } + + /// + /// The desired precision for this device. Can be used to match your actual water heater’s precision. Supported values are 0.1, 0.5 and 1.0. + /// Default: + /// + ///0.1 for Celsius and 1.0 for Fahrenheit. + /// + [JsonPropertyName("precision")] + public double? Precision { get; set; } + + /// + /// The maximum QoS level to be used when receiving and publishing messages. + /// , default: 0 + /// + [JsonPropertyName("qos")] + public long? Qos { get; set; } + + /// + /// Defines if published messages should have the retain flag set. + /// , default: false + /// + [JsonPropertyName("retain")] + public bool? Retain { get; set; } + + /// + /// A template to render the value sent to the temperature_command_topic with. + /// + [JsonPropertyName("temperature_command_template")] + public string? TemperatureCommandTemplate { get; set; } + + /// + /// The MQTT topic to publish commands to change the target temperature. + /// + [JsonPropertyName("temperature_command_topic")] + public string? TemperatureCommandTopic { get; set; } + + /// + /// A template to render the value received on the temperature_state_topic with. + /// + [JsonPropertyName("temperature_state_template")] + public string? TemperatureStateTemplate { get; set; } + + /// + /// The MQTT topic to subscribe for changes in the target temperature. If this is not set, the target temperature works in optimistic mode (see below). A "None" value received will reset the temperature set point. Empty values (''') will be ignored. + /// + [JsonPropertyName("temperature_state_topic")] + public string? TemperatureStateTopic { get; set; } + + /// + /// Defines the temperature unit of the device, C or F. If this is not set, the temperature unit is set to the system temperature unit. + /// + [JsonPropertyName("temperature_unit")] + public string? TemperatureUnit { get; set; } + + /// + /// Default template to render the payloads on all *_state_topics with. + /// + [JsonPropertyName("value_template")] + public string? ValueTemplate { get; set; } +} diff --git a/src/ToMqttNet/HomeAssistantDeviceClass.cs b/src/HomeAssistantDiscoveryNet/HomeAssistantDeviceClass.cs similarity index 97% rename from src/ToMqttNet/HomeAssistantDeviceClass.cs rename to src/HomeAssistantDiscoveryNet/HomeAssistantDeviceClass.cs index 308e766..7c3f34c 100644 --- a/src/ToMqttNet/HomeAssistantDeviceClass.cs +++ b/src/HomeAssistantDiscoveryNet/HomeAssistantDeviceClass.cs @@ -1,13 +1,8 @@ -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; -public class HomeAssistantDeviceClass +public class HomeAssistantDeviceClass(string value) { - public string Value { get; } - - public HomeAssistantDeviceClass(string value) - { - Value = value; - } + public string Value { get; } = value; public override string ToString() { diff --git a/src/HomeAssistantDiscoveryNet/HomeAssistantDiscoveryNet.csproj b/src/HomeAssistantDiscoveryNet/HomeAssistantDiscoveryNet.csproj new file mode 100644 index 0000000..8a050e5 --- /dev/null +++ b/src/HomeAssistantDiscoveryNet/HomeAssistantDiscoveryNet.csproj @@ -0,0 +1,24 @@ + + + + net8.0 + enable + enable + true + + true + + 0.1.4 + JonasMH + ASP.NET Hosted MQTT Connection and models to make it easier to work with Home Assistant MQTT Discovery + https://github.com/JonasMH/HomeAssistantDiscoveryNet + true + + $(NoWarn);1591 + + + + + + + diff --git a/src/ToMqttNet/HomeAssistantUnits.cs b/src/HomeAssistantDiscoveryNet/HomeAssistantUnits.cs similarity index 98% rename from src/ToMqttNet/HomeAssistantUnits.cs rename to src/HomeAssistantDiscoveryNet/HomeAssistantUnits.cs index cafe9b5..9e87d98 100644 --- a/src/ToMqttNet/HomeAssistantUnits.cs +++ b/src/HomeAssistantDiscoveryNet/HomeAssistantUnits.cs @@ -1,13 +1,8 @@ -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; -public class HomeAssistantUnits +public class HomeAssistantUnits(string value) { - public string Value { get; } - - public HomeAssistantUnits(string value) - { - Value = value; - } + public string Value { get; } = value; public override string ToString() { diff --git a/src/ToMqttNet/MqttDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/MqttDiscoveryConfig.cs similarity index 91% rename from src/ToMqttNet/MqttDiscoveryConfig.cs rename to src/HomeAssistantDiscoveryNet/MqttDiscoveryConfig.cs index 0edb02b..751606d 100644 --- a/src/ToMqttNet/MqttDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/MqttDiscoveryConfig.cs @@ -1,7 +1,6 @@ using System.Text.Json.Serialization; -using System.Runtime.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; /* * const snakeToPascal = (string) => { @@ -103,9 +102,34 @@ public abstract class MqttDiscoveryConfig [JsonPropertyName("icon")] public string? Icon { get; set; } + [JsonPropertyName("origin")] + public MqttDiscoveryConfigOrigin? Origin { get; set; } + + public abstract string Component { get; } } +public class MqttDiscoveryConfigOrigin +{ + /// + /// The name of the application that is the origin the discovered MQTT item. This option is required. + /// + [JsonPropertyName("name")] + public string Name { get; set; } = ""; + + /// + /// Software version of the application that supplies the discovered MQTT item. + /// + [JsonPropertyName("sw_version")] + public string? SoftwareVersion { get; set; } + + /// + /// Support URL of the application that supplies the discovered MQTT item. + /// + [JsonPropertyName("support_url")] + public string? SupportUrl { get; set; } +} + [JsonConverter(typeof(SnaekCaseJsonStringEnumConverter))] public enum MqttDiscoveryAvailabilityMode { diff --git a/src/ToMqttNet/MqttDiscoveryJsonContext.cs b/src/HomeAssistantDiscoveryNet/MqttDiscoveryJsonContext.cs similarity index 81% rename from src/ToMqttNet/MqttDiscoveryJsonContext.cs rename to src/HomeAssistantDiscoveryNet/MqttDiscoveryJsonContext.cs index d0eb8e6..e609535 100644 --- a/src/ToMqttNet/MqttDiscoveryJsonContext.cs +++ b/src/HomeAssistantDiscoveryNet/MqttDiscoveryJsonContext.cs @@ -1,7 +1,7 @@ using System.Text.Json.Nodes; using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; [JsonSerializable(typeof(MqttAlarmControlPanelDiscoveryConfig))] [JsonSerializable(typeof(MqttBinarySensorDiscoveryConfig))] @@ -25,6 +25,12 @@ namespace ToMqttNet; [JsonSerializable(typeof(MqttTagScannerDiscoveryConfig))] [JsonSerializable(typeof(MqttTemplateLightDiscoveryConfig))] [JsonSerializable(typeof(MqttVacuumDiscoveryConfig))] +[JsonSerializable(typeof(MqttImageDiscoveryConfig))] +[JsonSerializable(typeof(MqttLawnMowerDiscoveryConfig))] +[JsonSerializable(typeof(MqttUpdateDiscoveryConfig))] +[JsonSerializable(typeof(MqttTextDiscoveryConfig))] +[JsonSerializable(typeof(MqttValveDiscoveryConfig))] +[JsonSerializable(typeof(MqttWaterHeaterDiscoveryConfig))] [JsonSerializable(typeof(JsonObject))] [JsonSourceGenerationOptions( WriteIndented = true, diff --git a/src/HomeAssistantDiscoveryNet/Parsing/CustomMqttDiscoveryConfigType.cs b/src/HomeAssistantDiscoveryNet/Parsing/CustomMqttDiscoveryConfigType.cs new file mode 100644 index 0000000..edc1e59 --- /dev/null +++ b/src/HomeAssistantDiscoveryNet/Parsing/CustomMqttDiscoveryConfigType.cs @@ -0,0 +1,5 @@ +namespace HomeAssistantDiscoveryNet; + +public class CustomMqttDiscoveryConfigType(string key) : CustomMqttDiscoveryConfigType(key, typeof(T)) where T : MqttDiscoveryConfig +{ +} diff --git a/src/HomeAssistantDiscoveryNet/Parsing/CustomMqttDiscoveryConfigType1.cs b/src/HomeAssistantDiscoveryNet/Parsing/CustomMqttDiscoveryConfigType1.cs new file mode 100644 index 0000000..5cb0633 --- /dev/null +++ b/src/HomeAssistantDiscoveryNet/Parsing/CustomMqttDiscoveryConfigType1.cs @@ -0,0 +1,7 @@ +namespace HomeAssistantDiscoveryNet; + +public abstract class CustomMqttDiscoveryConfigType(string key, Type type) +{ + public string Key { get; set; } = key; + public Type Type { get; set; } = type; +} diff --git a/src/ToMqttNet/Parsing/IMqttDiscoveryConfigParser.cs b/src/HomeAssistantDiscoveryNet/Parsing/IMqttDiscoveryConfigParser.cs similarity index 83% rename from src/ToMqttNet/Parsing/IMqttDiscoveryConfigParser.cs rename to src/HomeAssistantDiscoveryNet/Parsing/IMqttDiscoveryConfigParser.cs index 360715c..69e1a83 100644 --- a/src/ToMqttNet/Parsing/IMqttDiscoveryConfigParser.cs +++ b/src/HomeAssistantDiscoveryNet/Parsing/IMqttDiscoveryConfigParser.cs @@ -1,6 +1,6 @@ using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; public interface IMqttDiscoveryConfigParser { diff --git a/src/ToMqttNet/Parsing/MqttDiscoveryConfigParser.cs b/src/HomeAssistantDiscoveryNet/Parsing/MqttDiscoveryConfigParser.cs similarity index 72% rename from src/ToMqttNet/Parsing/MqttDiscoveryConfigParser.cs rename to src/HomeAssistantDiscoveryNet/Parsing/MqttDiscoveryConfigParser.cs index 796cf85..0ab988f 100644 --- a/src/ToMqttNet/Parsing/MqttDiscoveryConfigParser.cs +++ b/src/HomeAssistantDiscoveryNet/Parsing/MqttDiscoveryConfigParser.cs @@ -1,14 +1,15 @@ using Microsoft.Extensions.Logging; using System.Collections.ObjectModel; using System.Text.Json; -using System.Text.Json.Nodes; using System.Text.Json.Serialization; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; -public class MqttDiscoveryConfigParser : IMqttDiscoveryConfigParser +public class MqttDiscoveryConfigParser( + ILoggerFactory loggerFactory, + IEnumerable customConfigTypes) : IMqttDiscoveryConfigParser { - public static ReadOnlyDictionary OfficalDiscoveryConfigTypes = new(new Dictionary() + public static ReadOnlyDictionary OfficalDiscoveryConfigTypes { get; } = new(new Dictionary() { // Official { "alarm_control_panel", typeof(MqttAlarmControlPanelDiscoveryConfig) }, @@ -29,22 +30,23 @@ public class MqttDiscoveryConfigParser : IMqttDiscoveryConfigParser { "tag", typeof(MqttTagScannerDiscoveryConfig) }, { "vacuum", typeof(MqttVacuumDiscoveryConfig) }, { "event", typeof(MqttEventDiscoveryConfig) }, + { "image", typeof(MqttImageDiscoveryConfig) }, + { "lawn_mower", typeof(MqttLawnMowerDiscoveryConfig) }, + { "siren", typeof(MqttSirenDiscoveryConfig) }, + { "update", typeof(MqttUpdateDiscoveryConfig) }, + { "text", typeof(MqttTextDiscoveryConfig) }, + { "valve", typeof(MqttValveDiscoveryConfig) }, + { "water_heater", typeof(MqttWaterHeaterDiscoveryConfig) }, }); - private readonly ILogger _logger; - private readonly Dictionary _discoveryConfigMap; - - public MqttDiscoveryConfigParser( - ILogger logger, - IEnumerable customConfigTypes) - { - _logger = logger; - - _discoveryConfigMap = OfficalDiscoveryConfigTypes + private readonly ILogger _logger = loggerFactory.CreateLogger(); + private readonly Dictionary _discoveryConfigMap = OfficalDiscoveryConfigTypes .Concat(customConfigTypes.Select(x => new KeyValuePair(x.Key, x.Type))) .ToDictionary(x => x.Key, x => x.Value); - } + /// + /// Returns null if unable to parse it + /// public MqttDiscoveryConfig? Parse(string topic, string message, JsonSerializerContext? jsonContext = null) { jsonContext ??= MqttDiscoveryJsonContext.Default; @@ -64,13 +66,7 @@ public MqttDiscoveryConfigParser( if (_discoveryConfigMap.TryGetValue(componentType, out var discoveryConfigType)) { - var jsonTypeInfo = jsonContext.GetTypeInfo(discoveryConfigType); - - if(jsonTypeInfo == null) - { - throw new InvalidOperationException("The JsonTypeInfo for " + discoveryConfigType.FullName + " was not found in the provided JsonSerializerContext. If you have a custom Discovery Document you might need to provide your own JsonSerializerContext"); - } - + var jsonTypeInfo = jsonContext.GetTypeInfo(discoveryConfigType) ?? throw new InvalidOperationException("The JsonTypeInfo for " + discoveryConfigType.FullName + " was not found in the provided JsonSerializerContext. If you have a custom Discovery Document you might need to provide your own JsonSerializerContext"); return (MqttDiscoveryConfig?)JsonSerializer.Deserialize(message, jsonTypeInfo); } diff --git a/src/ToMqttNet/Parsing/MqttDiscoveryParserServiceCollectionExtensions.cs b/src/HomeAssistantDiscoveryNet/Parsing/MqttDiscoveryParserServiceCollectionExtensions.cs similarity index 93% rename from src/ToMqttNet/Parsing/MqttDiscoveryParserServiceCollectionExtensions.cs rename to src/HomeAssistantDiscoveryNet/Parsing/MqttDiscoveryParserServiceCollectionExtensions.cs index ffc58d1..5ac3683 100644 --- a/src/ToMqttNet/Parsing/MqttDiscoveryParserServiceCollectionExtensions.cs +++ b/src/HomeAssistantDiscoveryNet/Parsing/MqttDiscoveryParserServiceCollectionExtensions.cs @@ -1,7 +1,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; -namespace ToMqttNet; +namespace HomeAssistantDiscoveryNet; public static class MqttDiscoveryParserServiceCollectionExtensions { diff --git a/src/HomeAssistantDiscoveryNet/packages.lock.json b/src/HomeAssistantDiscoveryNet/packages.lock.json new file mode 100644 index 0000000..1b65462 --- /dev/null +++ b/src/HomeAssistantDiscoveryNet/packages.lock.json @@ -0,0 +1,33 @@ +{ + "version": 1, + "dependencies": { + "net8.0": { + "Microsoft.DotNet.ILCompiler": { + "type": "Direct", + "requested": "[8.0.0, )", + "resolved": "8.0.0", + "contentHash": "zuTbpcWDCCvESnOAvpHuwum1C3jJIeDl+grYcRDqyoHsPAUutIB6wb7iJV3Y2Q96crKC6cObqf92eOVEiG7keQ==" + }, + "Microsoft.Extensions.Logging.Abstractions": { + "type": "Direct", + "requested": "[8.0.0, )", + "resolved": "8.0.0", + "contentHash": "arDBqTgFCyS0EvRV7O3MZturChstm50OJ0y9bDJvAcmEPJm0FFpFyjU/JLYyStNGGey081DvnQYlncNX5SJJGA==", + "dependencies": { + "Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0" + } + }, + "Microsoft.NET.ILLink.Tasks": { + "type": "Direct", + "requested": "[8.0.0, )", + "resolved": "8.0.0", + "contentHash": "B3etT5XQ2nlWkZGO2m/ytDYrOmSsQG1XNBaM6ZYlX5Ch/tDrMFadr0/mK6gjZwaQc55g+5+WZMw4Cz3m8VEF7g==" + }, + "Microsoft.Extensions.DependencyInjection.Abstractions": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "cjWrLkJXK0rs4zofsK4bSdg+jhDLTaxrkXu4gS6Y7MAlCvRyNNgwY/lJi5RDlQOnSZweHqoyvgvbdvQsRIW+hg==" + } + } + } +} \ No newline at end of file diff --git a/src/ToMqttNet/IMqttConnectionService.cs b/src/ToMqttNet/IMqttConnectionService.cs index 6fe12e1..70f29f8 100644 --- a/src/ToMqttNet/IMqttConnectionService.cs +++ b/src/ToMqttNet/IMqttConnectionService.cs @@ -1,7 +1,6 @@ using MQTTnet; using MQTTnet.Client; using MQTTnet.Packets; -using static System.Formats.Asn1.AsnWriter; namespace ToMqttNet; diff --git a/src/ToMqttNet/MqttConnectionOptions.cs b/src/ToMqttNet/MqttConnectionOptions.cs index 260b8eb..2d99e07 100644 --- a/src/ToMqttNet/MqttConnectionOptions.cs +++ b/src/ToMqttNet/MqttConnectionOptions.cs @@ -1,4 +1,5 @@ -using MQTTnet.Client; +using HomeAssistantDiscoveryNet; +using MQTTnet.Client; using System.ComponentModel.DataAnnotations; namespace ToMqttNet; @@ -9,4 +10,6 @@ public class MqttConnectionOptions public string NodeId { get; set; } = null!; public MqttClientOptions ClientOptions { get; set; } = new MqttClientOptions { }; + + public MqttDiscoveryConfigOrigin? OriginConfig { get; set; } } diff --git a/src/ToMqttNet/MqttConnectionService.cs b/src/ToMqttNet/MqttConnectionService.cs index 67967d8..840a7c9 100644 --- a/src/ToMqttNet/MqttConnectionService.cs +++ b/src/ToMqttNet/MqttConnectionService.cs @@ -10,31 +10,23 @@ namespace ToMqttNet; -public class MqttConnectionService : BackgroundService, IMqttConnectionService +public class MqttConnectionService( + ILogger logger, + IOptions mqttOptions, + [FromKeyedServices(typeof(MqttConnectionService))] IManagedMqttClient managedMqttClient, + MqttCounters counters) : BackgroundService, IMqttConnectionService { - private readonly ILogger _logger; - private readonly MqttCounters _counters; - private string _instanceId = Guid.NewGuid().ToString(); + private readonly ILogger _logger = logger; + private readonly MqttCounters _counters = counters; + private readonly string _instanceId = Guid.NewGuid().ToString(); - public MqttConnectionOptions MqttOptions { get; } - private readonly IManagedMqttClient _mqttClient; + public MqttConnectionOptions MqttOptions { get; } = mqttOptions.Value; + private readonly IManagedMqttClient _mqttClient = managedMqttClient; public event EventHandler? OnApplicationMessageReceived; public event EventHandler? OnConnect; public event EventHandler? OnDisconnect; - public MqttConnectionService( - ILogger logger, - IOptions mqttOptions, - [FromKeyedServices(typeof(MqttConnectionService))] IManagedMqttClient managedMqttClient, - MqttCounters counters) - { - _logger = logger; - _counters = counters; - _mqttClient = managedMqttClient; - MqttOptions = mqttOptions.Value; - } - protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogInformation("Executing {backgroundService}", GetType().FullName); @@ -49,13 +41,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) options.WillTopic = $"{MqttOptions.NodeId}/connected"; options.WillRetain = true; - if(options.ChannelOptions == null) { - options.ChannelOptions = new MqttClientTcpOptions + options.ChannelOptions ??= new MqttClientTcpOptions { Server = "mosquitto", Port = 1883 }; - } var optionsBuilder = new ManagedMqttClientOptionsBuilder() .WithAutoReconnectDelay(TimeSpan.FromSeconds(5)) @@ -77,6 +67,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) _mqttClient.ConnectedAsync += async (evnt) => { + _counters.NewConnection(); _logger.LogInformation("Connected to mqtt: {reason}", evnt.ConnectResult.ReasonString); await _mqttClient.EnqueueAsync( new MqttApplicationMessageBuilder() diff --git a/src/ToMqttNet/MqttConnectionServiceCollectionExtensions.cs b/src/ToMqttNet/MqttConnectionServiceCollectionExtensions.cs index e190610..94b8c32 100644 --- a/src/ToMqttNet/MqttConnectionServiceCollectionExtensions.cs +++ b/src/ToMqttNet/MqttConnectionServiceCollectionExtensions.cs @@ -1,5 +1,4 @@ -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using MQTTnet; using MQTTnet.Extensions.ManagedClient; @@ -13,13 +12,7 @@ public static OptionsBuilder AddMqttConnection(this IServ { services.AddSingleton(x => { - var meterFactory = x.GetService(); - - if(meterFactory == null) - { - throw new InvalidOperationException("Unable to find IMeterFactory in service collection. You may need to call .AddMetrics(). This should be automatically called in .NET8"); - } - + var meterFactory = x.GetService() ?? throw new InvalidOperationException("Unable to find IMeterFactory in service collection. You may need to call .AddMetrics(). This should be automatically called in .NET8"); return new MqttCounters(meterFactory); }); services.AddSingleton(); diff --git a/src/ToMqttNet/MqttConnectionServiceExtensions.cs b/src/ToMqttNet/MqttConnectionServiceExtensions.cs index 0ad963a..ac402b8 100644 --- a/src/ToMqttNet/MqttConnectionServiceExtensions.cs +++ b/src/ToMqttNet/MqttConnectionServiceExtensions.cs @@ -1,4 +1,5 @@ -using MQTTnet; +using HomeAssistantDiscoveryNet; +using MQTTnet; using System.Text.Json; using System.Text.Json.Serialization; @@ -9,22 +10,28 @@ public static class MqttDiscoveryConfigExtensions { public static string ToJson(this T config, JsonSerializerContext? ctx = null) where T : MqttDiscoveryConfig { - ctx = ctx ?? MqttDiscoveryJsonContext.Default; - var jsonTypeInfo = ctx.GetTypeInfo(config.GetType()); - - if (jsonTypeInfo == null) - { - throw new InvalidOperationException("The JsonTypeInfo for " + config.GetType().FullName + " was not found in the provided JsonSerializerContext. If you have a custom Discovery Document you might need to provide your own JsonSerializerContext"); - } - + ctx ??= MqttDiscoveryJsonContext.Default; + var jsonTypeInfo = ctx.GetTypeInfo(config.GetType()) ?? throw new InvalidOperationException("The JsonTypeInfo for " + config.GetType().FullName + " was not found in the provided JsonSerializerContext. If you have a custom Discovery Document you might need to provide your own JsonSerializerContext"); return JsonSerializer.Serialize(config, jsonTypeInfo); } } public static class MqttConnectionServiceExtensions { + /// + /// Publishes the given discovery document. + /// + /// If .Origin is null, it will be set to MqttOptions.Origin if set. + /// + /// + /// + /// + /// + /// public static Task PublishDiscoveryDocument(this IMqttConnectionService connection, T config, JsonSerializerContext? ctx = null) where T : MqttDiscoveryConfig { + config.Origin ??= connection.MqttOptions.OriginConfig; + return connection.PublishAsync( new MqttApplicationMessageBuilder() .WithTopic($"homeassistant/{config.Component}/{connection.MqttOptions.NodeId}/{config.UniqueId}/config") diff --git a/src/ToMqttNet/MqttCounters.cs b/src/ToMqttNet/MqttCounters.cs index 1529c14..8c773a4 100644 --- a/src/ToMqttNet/MqttCounters.cs +++ b/src/ToMqttNet/MqttCounters.cs @@ -8,6 +8,7 @@ public class MqttCounters private readonly Counter _messagesSent; private readonly Counter _messagesHandled; + private readonly Counter _connectionsCreated; private Func _pendingMessages = () => 0; private int _connections = 0; @@ -17,7 +18,8 @@ public MqttCounters(IMeterFactory meterFactory) _messagesHandled = _scope.CreateCounter("mqtt_client.messages_handled_total", "messages", description: "Amount of MQTT packages received"); _messagesSent = _scope.CreateCounter("mqtt_client.messages_sent_total", "messages", description: "Amount of MQTT packages sent"); - _scope.CreateObservableGauge("mqtt_client.connections", () => _connections, unit: "connections", description: "Amount of MQTT connections created"); + _connectionsCreated = _scope.CreateCounter("mqtt_client.connections_opened", description: "Amount of MQTT connections created"); + _scope.CreateObservableGauge("mqtt_client.connections", () => _connections, unit: "connections", description: "Is MQTT connection active"); _scope.CreateObservableGauge("mqtt_client.pending_messages", _pendingMessages, unit: "messages", description: "Amount of ingoing MQTT messages pending to be processed by the client"); } @@ -31,6 +33,11 @@ public void IncreaseMessagesHandled(bool success) _messagesHandled.Add(1, new KeyValuePair("success", success)); } + public void NewConnection() + { + _connectionsCreated.Add(1); + } + public void SetConnections(int amount) { _connections = amount; diff --git a/src/ToMqttNet/Parsing/CustomMqttDiscoveryConfigType.cs b/src/ToMqttNet/Parsing/CustomMqttDiscoveryConfigType.cs deleted file mode 100644 index fec700f..0000000 --- a/src/ToMqttNet/Parsing/CustomMqttDiscoveryConfigType.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace ToMqttNet; - -public class CustomMqttDiscoveryConfigType : CustomMqttDiscoveryConfigType where T : MqttDiscoveryConfig -{ - public CustomMqttDiscoveryConfigType(string key) : base(key, typeof(T)) - { - } -} diff --git a/src/ToMqttNet/Parsing/CustomMqttDiscoveryConfigType1.cs b/src/ToMqttNet/Parsing/CustomMqttDiscoveryConfigType1.cs deleted file mode 100644 index f5622d8..0000000 --- a/src/ToMqttNet/Parsing/CustomMqttDiscoveryConfigType1.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace ToMqttNet; - -public abstract class CustomMqttDiscoveryConfigType -{ - public CustomMqttDiscoveryConfigType(string key, Type type) - { - Key = key; - Type = type; - } - - public string Key { get; set; } - public Type Type { get; set; } -} diff --git a/src/ToMqttNet/ToMqttNet.csproj b/src/ToMqttNet/ToMqttNet.csproj index de47148..63cff4e 100644 --- a/src/ToMqttNet/ToMqttNet.csproj +++ b/src/ToMqttNet/ToMqttNet.csproj @@ -12,7 +12,7 @@ 0.1.4 JonasMH ASP.NET Hosted MQTT Connection and models to make it easier to work with Home Assistant MQTT Discovery - https://github.com/JonasMH/ToMqttNet + https://github.com/JonasMH/HomeAssistantDiscoveryNet true $(NoWarn);1591 @@ -20,10 +20,13 @@ - - + + + + + diff --git a/src/ToMqttNet/packages.lock.json b/src/ToMqttNet/packages.lock.json index 0aec583..fc36f67 100644 --- a/src/ToMqttNet/packages.lock.json +++ b/src/ToMqttNet/packages.lock.json @@ -21,20 +21,11 @@ "Microsoft.Extensions.Logging.Abstractions": "8.0.0" } }, - "Microsoft.Extensions.Logging.Abstractions": { - "type": "Direct", - "requested": "[8.0.0, )", - "resolved": "8.0.0", - "contentHash": "arDBqTgFCyS0EvRV7O3MZturChstm50OJ0y9bDJvAcmEPJm0FFpFyjU/JLYyStNGGey081DvnQYlncNX5SJJGA==", - "dependencies": { - "Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0" - } - }, "Microsoft.Extensions.Options": { "type": "Direct", - "requested": "[8.0.0, )", - "resolved": "8.0.0", - "contentHash": "JOVOfqpnqlVLUzINQ2fox8evY2SKLYJ3BV8QDe/Jyp21u1T7r45x/R/5QdteURMR5r01GxeJSBBUOCOyaNXA3g==", + "requested": "[8.0.1, )", + "resolved": "8.0.1", + "contentHash": "wmpp+BSU3oGifaev6Z9rrlwHoITLFfpVOSbgBrOXjkbJSCXnZVCsoRGE5c3fJFI4VlNgnNkNlI9y+5jC4fmOEA==", "dependencies": { "Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0", "Microsoft.Extensions.Primitives": "8.0.0" @@ -102,6 +93,14 @@ "Microsoft.Extensions.Primitives": "8.0.0" } }, + "Microsoft.Extensions.Logging.Abstractions": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "arDBqTgFCyS0EvRV7O3MZturChstm50OJ0y9bDJvAcmEPJm0FFpFyjU/JLYyStNGGey081DvnQYlncNX5SJJGA==", + "dependencies": { + "Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0" + } + }, "Microsoft.Extensions.Primitives": { "type": "Transitive", "resolved": "8.0.0", @@ -111,6 +110,12 @@ "type": "Transitive", "resolved": "8.0.0", "contentHash": "c9xLpVz6PL9lp/djOWtk5KPDZq3cSYpmXoJQY524EOtuFl5z9ZtsotpsyrDW40U1DRnQSYvcPKEUV0X//u6gkQ==" + }, + "homeassistantdiscoverynet": { + "type": "Project", + "dependencies": { + "Microsoft.Extensions.Logging.Abstractions": "[8.0.0, )" + } } } } diff --git a/test/ToMqttNet.Test.Unit/MqttBinarySensorDiscoveryConfigTests.cs b/test/ToMqttNet.Test.Unit/MqttBinarySensorDiscoveryConfigTests.cs index f494e59..84f2b49 100644 --- a/test/ToMqttNet.Test.Unit/MqttBinarySensorDiscoveryConfigTests.cs +++ b/test/ToMqttNet.Test.Unit/MqttBinarySensorDiscoveryConfigTests.cs @@ -1,6 +1,7 @@ using Newtonsoft.Json.Linq; using Newtonsoft.Json; using Xunit; +using HomeAssistantDiscoveryNet; namespace ToMqttNet.Test.Unit; diff --git a/test/ToMqttNet.Test.Unit/MqttConnectionServiceTests.cs b/test/ToMqttNet.Test.Unit/MqttConnectionServiceTests.cs index 7ef2e95..b8f5943 100644 --- a/test/ToMqttNet.Test.Unit/MqttConnectionServiceTests.cs +++ b/test/ToMqttNet.Test.Unit/MqttConnectionServiceTests.cs @@ -3,7 +3,6 @@ using MQTTnet.Client; using MQTTnet.Extensions.ManagedClient; using MQTTnet.Packets; -using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Diagnostics.Metrics; @@ -16,9 +15,9 @@ namespace ToMqttNet.Test.Unit; public class MqttConnectionServiceTests { - private MqttConnectionService _sut; - private MeterFactoryStub _meterFactoryStub; - private MqttClientStub _clientStub; + private readonly MqttConnectionService _sut; + private readonly MeterFactoryStub _meterFactoryStub; + private readonly MqttClientStub _clientStub; public MqttConnectionServiceTests(ITestOutputHelper testOutputHelper) { @@ -138,8 +137,8 @@ public Task CallDisconnectedAsync(MqttClientDisconnectedEventArgs args) } public event Func SynchronizingSubscriptionsFailedAsync = null!; - public List EnqueuedMessage = new List(); - public List Subscriptions = new List(); + public List EnqueuedMessage = []; + public List Subscriptions = []; public void Dispose(){} diff --git a/test/ToMqttNet.Test.Unit/MqttDefaultLightDiscoveryConfigTests.cs b/test/ToMqttNet.Test.Unit/MqttDefaultLightDiscoveryConfigTests.cs index 6a32703..f10de1b 100644 --- a/test/ToMqttNet.Test.Unit/MqttDefaultLightDiscoveryConfigTests.cs +++ b/test/ToMqttNet.Test.Unit/MqttDefaultLightDiscoveryConfigTests.cs @@ -1,6 +1,7 @@ using Newtonsoft.Json.Linq; using Newtonsoft.Json; using Xunit; +using HomeAssistantDiscoveryNet; namespace ToMqttNet.Test.Unit; diff --git a/test/ToMqttNet.Test.Unit/MqttDiscoveryConfigParserTests.cs b/test/ToMqttNet.Test.Unit/MqttDiscoveryConfigParserTests.cs index c499e02..02893e8 100644 --- a/test/ToMqttNet.Test.Unit/MqttDiscoveryConfigParserTests.cs +++ b/test/ToMqttNet.Test.Unit/MqttDiscoveryConfigParserTests.cs @@ -1,4 +1,7 @@ -using System.Collections.Generic; +using HomeAssistantDiscoveryNet; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using System.Collections.Generic; using Xunit; using Xunit.Abstractions; @@ -10,7 +13,7 @@ public class MqttDiscoveryConfigParserTests public MqttDiscoveryConfigParserTests(ITestOutputHelper testOutputHelper) { - _sut = new MqttDiscoveryConfigParser(testOutputHelper.CreateLogger(), new List()); + _sut = new MqttDiscoveryConfigParser(NullLoggerFactory.Instance, []); } [Fact] @@ -36,4 +39,16 @@ public void Parse_ParseLightConfig_ParseOkay() Assert.IsType(result); Assert.Equal(254, ((MqttJsonLightDiscoveryConfig)result).BrightnessScale); } + + [Fact] + public void Parse_ParseReadmeExample() + { + var parser = new MqttDiscoveryConfigParser(NullLoggerFactory.Instance, []); + var topic = "some-node/binary_sensor/some_sensor"; + var message = "{\"component\": \"binary_sensor\", \"state_topic\": \"some-node/status/recommend-charging\", \"value_template\": \"{{ 'ON' if value_json.recommend else 'OFF' }}\", \"name\": \"Recommend Charging\", \"unique_id\": \"some-node-recommend-charging\"}"; + + var result = parser.Parse(topic, message); + + Assert.IsType(result); + } } diff --git a/test/ToMqttNet.Test.Unit/MqttDiscoveryConfigTests.cs b/test/ToMqttNet.Test.Unit/MqttDiscoveryConfigTests.cs index d759edd..42f9980 100644 --- a/test/ToMqttNet.Test.Unit/MqttDiscoveryConfigTests.cs +++ b/test/ToMqttNet.Test.Unit/MqttDiscoveryConfigTests.cs @@ -1,3 +1,4 @@ +using HomeAssistantDiscoveryNet; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; @@ -63,6 +64,12 @@ public void ToJson_ShouldIgnoreNullForObjects() [InlineData(typeof(MqttTagScannerDiscoveryConfig))] [InlineData(typeof(MqttTemplateLightDiscoveryConfig))] [InlineData(typeof(MqttVacuumDiscoveryConfig))] + [InlineData(typeof(MqttTextDiscoveryConfig))] + [InlineData(typeof(MqttValveDiscoveryConfig))] + [InlineData(typeof(MqttWaterHeaterDiscoveryConfig))] + [InlineData(typeof(MqttUpdateDiscoveryConfig))] + [InlineData(typeof(MqttSirenDiscoveryConfig))] + [InlineData(typeof(MqttImageDiscoveryConfig))] public void ToJson_OutputShouldNotContainNull(Type configType) { var instance = (Activator.CreateInstance(configType) as MqttDiscoveryConfig)!; diff --git a/test/ToMqttNet.Test.Unit/MqttJsonLightDiscoveryConfigTests.cs b/test/ToMqttNet.Test.Unit/MqttJsonLightDiscoveryConfigTests.cs index 92d40d0..bf65c5b 100644 --- a/test/ToMqttNet.Test.Unit/MqttJsonLightDiscoveryConfigTests.cs +++ b/test/ToMqttNet.Test.Unit/MqttJsonLightDiscoveryConfigTests.cs @@ -1,6 +1,7 @@ using Newtonsoft.Json.Linq; using Newtonsoft.Json; using Xunit; +using HomeAssistantDiscoveryNet; namespace ToMqttNet.Test.Unit; diff --git a/test/ToMqttNet.Test.Unit/MqttSensorDiscoveryConfigTests.cs b/test/ToMqttNet.Test.Unit/MqttSensorDiscoveryConfigTests.cs index 5bd2a02..43d0b94 100644 --- a/test/ToMqttNet.Test.Unit/MqttSensorDiscoveryConfigTests.cs +++ b/test/ToMqttNet.Test.Unit/MqttSensorDiscoveryConfigTests.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json; +using HomeAssistantDiscoveryNet; +using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Xunit; diff --git a/test/ToMqttNet.Test.Unit/MqttTemplateLightDiscoveryConfigTests.cs b/test/ToMqttNet.Test.Unit/MqttTemplateLightDiscoveryConfigTests.cs index b21a19c..0daee3b 100644 --- a/test/ToMqttNet.Test.Unit/MqttTemplateLightDiscoveryConfigTests.cs +++ b/test/ToMqttNet.Test.Unit/MqttTemplateLightDiscoveryConfigTests.cs @@ -1,6 +1,7 @@ using Newtonsoft.Json.Linq; using Newtonsoft.Json; using Xunit; +using HomeAssistantDiscoveryNet; namespace ToMqttNet.Test.Unit; diff --git a/test/ToMqttNet.Test.Unit/ToMqttNet.Test.Unit.csproj b/test/ToMqttNet.Test.Unit/ToMqttNet.Test.Unit.csproj index 39315d7..b18fbcd 100644 --- a/test/ToMqttNet.Test.Unit/ToMqttNet.Test.Unit.csproj +++ b/test/ToMqttNet.Test.Unit/ToMqttNet.Test.Unit.csproj @@ -10,8 +10,8 @@ - - + + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/ToMqttNet.Test.Unit/packages.lock.json b/test/ToMqttNet.Test.Unit/packages.lock.json index 4cdf7ab..5a03c96 100644 --- a/test/ToMqttNet.Test.Unit/packages.lock.json +++ b/test/ToMqttNet.Test.Unit/packages.lock.json @@ -20,20 +20,20 @@ }, "xunit": { "type": "Direct", - "requested": "[2.6.3, )", - "resolved": "2.6.3", - "contentHash": "4CoGp3iKZORZ9aaHdPrFcdlDBRI/ZFRyDZWJuP7r2idULEtWf+k0yGT80cF/v6nE/MQDXLMIuSGDIrHuPLC/+Q==", + "requested": "[2.6.5, )", + "resolved": "2.6.5", + "contentHash": "iPSL63kw21BdSsdA79bvbVNvyn17DWI4D6VbgNxYtvzgViKrmbRLr8sWPxSlc4AvnofEuFfAi/rrLSzSRomwCg==", "dependencies": { - "xunit.analyzers": "1.7.0", - "xunit.assert": "2.6.3", - "xunit.core": "[2.6.3]" + "xunit.analyzers": "1.9.0", + "xunit.assert": "2.6.5", + "xunit.core": "[2.6.5]" } }, "xunit.runner.visualstudio": { "type": "Direct", - "requested": "[2.5.5, )", - "resolved": "2.5.5", - "contentHash": "AJsBBn8S00ZxdXPSIp8s+NcBQMO9tcll1UVCfGNoEi1DUH2z3f4Y1epV9nYbhgG6wFoTKJP6YuLT2MeTPV9gnA==" + "requested": "[2.5.6, )", + "resolved": "2.5.6", + "contentHash": "CW6uhMXNaQQNMSG1IWhHkBT+V5eqHqn7MP0zfNMhU9wS/sgKX7FGL3rzoaUgt26wkY3bpf7pDVw3IjXhwfiP4w==" }, "Microsoft.CodeCoverage": { "type": "Transitive", @@ -93,8 +93,8 @@ }, "Microsoft.Extensions.Options": { "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "JOVOfqpnqlVLUzINQ2fox8evY2SKLYJ3BV8QDe/Jyp21u1T7r45x/R/5QdteURMR5r01GxeJSBBUOCOyaNXA3g==", + "resolved": "8.0.1", + "contentHash": "wmpp+BSU3oGifaev6Z9rrlwHoITLFfpVOSbgBrOXjkbJSCXnZVCsoRGE5c3fJFI4VlNgnNkNlI9y+5jC4fmOEA==", "dependencies": { "Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0", "Microsoft.Extensions.Primitives": "8.0.0" @@ -1058,27 +1058,27 @@ }, "xunit.analyzers": { "type": "Transitive", - "resolved": "1.7.0", - "contentHash": "lAcpaB0KnjWETbPDMsDPoQi6zS0uXUYopmyTQDWvILJWcL9jwMxXXGmDLptlMA6vGi5Z0LtudsYN+dEUTmuHaA==" + "resolved": "1.9.0", + "contentHash": "02ucFDty6Y9BBT5c35YueFfbM3uEzeFdRvlNtAPhZVUkGUlhl3jsV2XesgTj986/PZXIjpVoc2D8ee6p1ha/Fw==" }, "xunit.assert": { "type": "Transitive", - "resolved": "2.6.3", - "contentHash": "7JWrjxDIAwF1vBZyENu4BZaddNJijIQcIQcc+73sJPWaqzDmseysTYv2jR0IWBsWIGa6LSfMO/3+dH0fsHL5Wg==" + "resolved": "2.6.5", + "contentHash": "gb5uv7vjBFz7nhEa6aXK5sVJwsG/88xf8DN5wqK0ejCDsDybqICyNJIj+eoD43xbmdPZryNDPpeWDCfiKI/bnA==" }, "xunit.core": { "type": "Transitive", - "resolved": "2.6.3", - "contentHash": "rlrm//9cNAlCkh3p5mdggzfchZlcXowUVK3UPqKiaN7G0Wu7gOUNLLZej9twnwTCVe1h3FEi9PRwzXMMXpFwSQ==", + "resolved": "2.6.5", + "contentHash": "hpdMnSNlx4ejaxpaIAFaqHt4q9ZCnzZLnURrSa5CzYXxHhIQbV8/0yXLjRdublhreonGXVMmsQ1KHlS9WbfpCw==", "dependencies": { - "xunit.extensibility.core": "[2.6.3]", - "xunit.extensibility.execution": "[2.6.3]" + "xunit.extensibility.core": "[2.6.5]", + "xunit.extensibility.execution": "[2.6.5]" } }, "xunit.extensibility.core": { "type": "Transitive", - "resolved": "2.6.3", - "contentHash": "qWCpmK4nxFBsf68r35AgnrW6B+xjTCpjJYfJkaBaini4Myl/2P/Ro6WocVtJfpHilkatitVFgM3XVR0jthdJlQ==", + "resolved": "2.6.5", + "contentHash": "dSGRkVxzH27XaL83+Z9kNPllqgsmsiPayXw+0weCGsrZQxfSCBNNkSb9nYUpkVoEBCUviXOmo1tfApqhgqTjog==", "dependencies": { "NETStandard.Library": "1.6.1", "xunit.abstractions": "2.0.3" @@ -1086,21 +1086,27 @@ }, "xunit.extensibility.execution": { "type": "Transitive", - "resolved": "2.6.3", - "contentHash": "I+9OA9a6WVia40bxJi3KaxVcjTE1ouHqD0QcrjHA/hLl7Ozb5eQmV7ecLu3gRZlPLdcVN+If8IZcEmqytmLtkA==", + "resolved": "2.6.5", + "contentHash": "jUMr88e0lSqDq8Vut0XVqx7plFg91QsKW/rX6gaVnJL6Z19LmNSDmyqd7cg6HQGfboAmyoFZyydA4Kcgouu1BA==", "dependencies": { "NETStandard.Library": "1.6.1", - "xunit.extensibility.core": "[2.6.3]" + "xunit.extensibility.core": "[2.6.5]" + } + }, + "homeassistantdiscoverynet": { + "type": "Project", + "dependencies": { + "Microsoft.Extensions.Logging.Abstractions": "[8.0.0, )" } }, "tomqttnet": { "type": "Project", "dependencies": { + "HomeAssistantDiscoveryNet": "[1.0.0, )", "MQTTnet": "[4.3.3.952, )", "MQTTnet.Extensions.ManagedClient": "[4.3.3.952, )", "Microsoft.Extensions.Hosting.Abstractions": "[8.0.0, )", - "Microsoft.Extensions.Logging.Abstractions": "[8.0.0, )", - "Microsoft.Extensions.Options": "[8.0.0, )", + "Microsoft.Extensions.Options": "[8.0.1, )", "Microsoft.Extensions.Options.DataAnnotations": "[8.0.0, )" } } From 307108837c86a0196d218fbc34e3e31f78766c32 Mon Sep 17 00:00:00 2001 From: Jonas Hansen Date: Wed, 10 Jan 2024 20:24:45 +0100 Subject: [PATCH 2/6] Add readme, examples and more discoverydocs. --- src/ToMqttNet/ToMqttNet.csproj | 3 +- src/ToMqttNet/packages.lock.json | 43 ++++++++------------- test/ToMqttNet.Test.Unit/packages.lock.json | 3 +- 3 files changed, 19 insertions(+), 30 deletions(-) diff --git a/src/ToMqttNet/ToMqttNet.csproj b/src/ToMqttNet/ToMqttNet.csproj index 63cff4e..e8c1a63 100644 --- a/src/ToMqttNet/ToMqttNet.csproj +++ b/src/ToMqttNet/ToMqttNet.csproj @@ -21,12 +21,11 @@ - - + diff --git a/src/ToMqttNet/packages.lock.json b/src/ToMqttNet/packages.lock.json index fc36f67..071d779 100644 --- a/src/ToMqttNet/packages.lock.json +++ b/src/ToMqttNet/packages.lock.json @@ -21,16 +21,6 @@ "Microsoft.Extensions.Logging.Abstractions": "8.0.0" } }, - "Microsoft.Extensions.Options": { - "type": "Direct", - "requested": "[8.0.1, )", - "resolved": "8.0.1", - "contentHash": "wmpp+BSU3oGifaev6Z9rrlwHoITLFfpVOSbgBrOXjkbJSCXnZVCsoRGE5c3fJFI4VlNgnNkNlI9y+5jC4fmOEA==", - "dependencies": { - "Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0", - "Microsoft.Extensions.Primitives": "8.0.0" - } - }, "Microsoft.Extensions.Options.DataAnnotations": { "type": "Direct", "requested": "[8.0.0, )", @@ -100,23 +90,24 @@ "dependencies": { "Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0" } - }, - "Microsoft.Extensions.Primitives": { - "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "bXJEZrW9ny8vjMF1JV253WeLhpEVzFo1lyaZu1vQ4ZxWUlVvknZ/+ftFgVheLubb4eZPSwwxBeqS1JkCOjxd8g==" - }, - "System.Diagnostics.DiagnosticSource": { - "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "c9xLpVz6PL9lp/djOWtk5KPDZq3cSYpmXoJQY524EOtuFl5z9ZtsotpsyrDW40U1DRnQSYvcPKEUV0X//u6gkQ==" - }, - "homeassistantdiscoverynet": { - "type": "Project", - "dependencies": { - "Microsoft.Extensions.Logging.Abstractions": "[8.0.0, )" - } + } + }, + "Microsoft.Extensions.Primitives": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "bXJEZrW9ny8vjMF1JV253WeLhpEVzFo1lyaZu1vQ4ZxWUlVvknZ/+ftFgVheLubb4eZPSwwxBeqS1JkCOjxd8g==" + }, + "System.Diagnostics.DiagnosticSource": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "c9xLpVz6PL9lp/djOWtk5KPDZq3cSYpmXoJQY524EOtuFl5z9ZtsotpsyrDW40U1DRnQSYvcPKEUV0X//u6gkQ==" + }, + "homeassistantdiscoverynet": { + "type": "Project", + "dependencies": { + "Microsoft.Extensions.Logging.Abstractions": "[8.0.0, )" } } } +} } \ No newline at end of file diff --git a/test/ToMqttNet.Test.Unit/packages.lock.json b/test/ToMqttNet.Test.Unit/packages.lock.json index 5a03c96..21fe2ef 100644 --- a/test/ToMqttNet.Test.Unit/packages.lock.json +++ b/test/ToMqttNet.Test.Unit/packages.lock.json @@ -1102,11 +1102,10 @@ "tomqttnet": { "type": "Project", "dependencies": { - "HomeAssistantDiscoveryNet": "[1.0.0, )", + "HomeAssistantDiscoveryNet": "[0.1.4, )", "MQTTnet": "[4.3.3.952, )", "MQTTnet.Extensions.ManagedClient": "[4.3.3.952, )", "Microsoft.Extensions.Hosting.Abstractions": "[8.0.0, )", - "Microsoft.Extensions.Options": "[8.0.1, )", "Microsoft.Extensions.Options.DataAnnotations": "[8.0.0, )" } } From b546b258437d60346dba30f52a07214280aefc17 Mon Sep 17 00:00:00 2001 From: Jonas Hansen Date: Wed, 10 Jan 2024 20:29:01 +0100 Subject: [PATCH 3/6] Update lock file --- src/ToMqttNet/packages.lock.json | 42 +++++++++++++++++++------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/ToMqttNet/packages.lock.json b/src/ToMqttNet/packages.lock.json index 071d779..145430c 100644 --- a/src/ToMqttNet/packages.lock.json +++ b/src/ToMqttNet/packages.lock.json @@ -90,24 +90,32 @@ "dependencies": { "Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0" } - } - }, - "Microsoft.Extensions.Primitives": { - "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "bXJEZrW9ny8vjMF1JV253WeLhpEVzFo1lyaZu1vQ4ZxWUlVvknZ/+ftFgVheLubb4eZPSwwxBeqS1JkCOjxd8g==" - }, - "System.Diagnostics.DiagnosticSource": { - "type": "Transitive", - "resolved": "8.0.0", - "contentHash": "c9xLpVz6PL9lp/djOWtk5KPDZq3cSYpmXoJQY524EOtuFl5z9ZtsotpsyrDW40U1DRnQSYvcPKEUV0X//u6gkQ==" - }, - "homeassistantdiscoverynet": { - "type": "Project", - "dependencies": { - "Microsoft.Extensions.Logging.Abstractions": "[8.0.0, )" + }, + "Microsoft.Extensions.Options": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "JOVOfqpnqlVLUzINQ2fox8evY2SKLYJ3BV8QDe/Jyp21u1T7r45x/R/5QdteURMR5r01GxeJSBBUOCOyaNXA3g==", + "dependencies": { + "Microsoft.Extensions.DependencyInjection.Abstractions": "8.0.0", + "Microsoft.Extensions.Primitives": "8.0.0" + } + }, + "Microsoft.Extensions.Primitives": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "bXJEZrW9ny8vjMF1JV253WeLhpEVzFo1lyaZu1vQ4ZxWUlVvknZ/+ftFgVheLubb4eZPSwwxBeqS1JkCOjxd8g==" + }, + "System.Diagnostics.DiagnosticSource": { + "type": "Transitive", + "resolved": "8.0.0", + "contentHash": "c9xLpVz6PL9lp/djOWtk5KPDZq3cSYpmXoJQY524EOtuFl5z9ZtsotpsyrDW40U1DRnQSYvcPKEUV0X//u6gkQ==" + }, + "homeassistantdiscoverynet": { + "type": "Project", + "dependencies": { + "Microsoft.Extensions.Logging.Abstractions": "[8.0.0, )" + } } } } -} } \ No newline at end of file From d261132f900a9c671950599e380b05da89636831 Mon Sep 17 00:00:00 2001 From: Jonas Hansen Date: Wed, 10 Jan 2024 20:33:33 +0100 Subject: [PATCH 4/6] Specify dotnet version in actions --- .github/workflows/build.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index dd8fa66..319b249 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -33,7 +33,7 @@ jobs: - name: Setup .NET Core uses: actions/setup-dotnet@v3 with: - dotnet-version: 8.x + dotnet-version: "8.0.100" cache: true cache-dependency-path: src/ToMqttNet/packages.lock.json - name: Restore From 76e371131ad9eb065ffcec97f77fe17cfe9cabc3 Mon Sep 17 00:00:00 2001 From: Jonas Hansen Date: Wed, 10 Jan 2024 20:33:54 +0100 Subject: [PATCH 5/6] Specify dotnet version in actions --- .github/workflows/build.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 319b249..000c028 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -73,7 +73,7 @@ jobs: - name: Setup .NET Core uses: actions/setup-dotnet@v3 with: - dotnet-version: 8.x + dotnet-version: "8.0.100" cache: true cache-dependency-path: src/ToMqttNet/packages.lock.json - name: Create Release NuGet package From bf7af0ada44914cd6e34891a7e496dd3875c7485 Mon Sep 17 00:00:00 2001 From: Jonas Hansen Date: Wed, 10 Jan 2024 20:39:41 +0100 Subject: [PATCH 6/6] Fix nullability --- .../Entities/MqttLawnMowerDiscoveryConfig.cs | 4 ++++ .../Entities/MqttSirenDiscoveryConfig.cs | 6 ++++++ .../Entities/MqttTextDiscoveryConfig.cs | 5 +++++ .../Entities/MqttUpdateDiscoveryConfig.cs | 3 +++ .../Entities/MqttValveDiscoveryConfig.cs | 7 +++++++ .../Entities/MqttWaterHeaterDiscoveryConfig.cs | 8 ++++++++ src/HomeAssistantDiscoveryNet/MqttDiscoveryJsonContext.cs | 1 + 7 files changed, 34 insertions(+) diff --git a/src/HomeAssistantDiscoveryNet/Entities/MqttLawnMowerDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttLawnMowerDiscoveryConfig.cs index 51faf75..3d12f5f 100644 --- a/src/HomeAssistantDiscoveryNet/Entities/MqttLawnMowerDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttLawnMowerDiscoveryConfig.cs @@ -38,6 +38,7 @@ public class MqttLawnMowerDiscoveryConfig : MqttDiscoveryConfig /// , default: true /// [JsonPropertyName("enabled_by_default")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? EnabledByDefault { get; set; } /// @@ -79,6 +80,7 @@ public class MqttLawnMowerDiscoveryConfig : MqttDiscoveryConfig ///true if no activity_state_topic defined, else false. /// [JsonPropertyName("optimistic")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? Optimistic { get; set; } /// @@ -98,6 +100,7 @@ public class MqttLawnMowerDiscoveryConfig : MqttDiscoveryConfig /// , default: 0 /// [JsonPropertyName("qos")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public long? Qos { get; set; } /// @@ -117,5 +120,6 @@ public class MqttLawnMowerDiscoveryConfig : MqttDiscoveryConfig /// , default: false /// [JsonPropertyName("retain")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? Retain { get; set; } } diff --git a/src/HomeAssistantDiscoveryNet/Entities/MqttSirenDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttSirenDiscoveryConfig.cs index f841774..5f244ec 100644 --- a/src/HomeAssistantDiscoveryNet/Entities/MqttSirenDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttSirenDiscoveryConfig.cs @@ -39,6 +39,7 @@ public class MqttSirenDiscoveryConfig : MqttDiscoveryConfig /// , default: true /// [JsonPropertyName("enabled_by_default")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? EnabledByDefault { get; set; } /// @@ -81,6 +82,7 @@ public class MqttSirenDiscoveryConfig : MqttDiscoveryConfig ///true if no state_topic defined, else false. /// [JsonPropertyName("optimistic")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? Optimistic { get; set; } /// @@ -116,6 +118,7 @@ public class MqttSirenDiscoveryConfig : MqttDiscoveryConfig /// , default: 0 /// [JsonPropertyName("qos")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public long? Qos { get; set; } /// @@ -123,6 +126,7 @@ public class MqttSirenDiscoveryConfig : MqttDiscoveryConfig /// , default: false /// [JsonPropertyName("retain")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? Retain { get; set; } /// @@ -160,6 +164,7 @@ public class MqttSirenDiscoveryConfig : MqttDiscoveryConfig /// , default: true /// [JsonPropertyName("support_duration")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? SupportDuration { get; set; } /// @@ -167,5 +172,6 @@ public class MqttSirenDiscoveryConfig : MqttDiscoveryConfig /// , default: true /// [JsonPropertyName("support_volume_set")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? SupportVolumeSet { get; set; } } diff --git a/src/HomeAssistantDiscoveryNet/Entities/MqttTextDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttTextDiscoveryConfig.cs index e1f4219..450678f 100644 --- a/src/HomeAssistantDiscoveryNet/Entities/MqttTextDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttTextDiscoveryConfig.cs @@ -26,6 +26,7 @@ public class MqttTextDiscoveryConfig : MqttDiscoveryConfig /// , default: true /// [JsonPropertyName("enabled_by_default")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? EnabledByDefault { get; set; } /// @@ -59,6 +60,7 @@ public class MqttTextDiscoveryConfig : MqttDiscoveryConfig /// , default: 255 /// [JsonPropertyName("max")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public long? Max { get; set; } /// @@ -66,6 +68,7 @@ public class MqttTextDiscoveryConfig : MqttDiscoveryConfig /// , default: 0 /// [JsonPropertyName("min")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public long? Min { get; set; } /// @@ -92,6 +95,7 @@ public class MqttTextDiscoveryConfig : MqttDiscoveryConfig /// , default: 0 /// [JsonPropertyName("qos")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public long? Qos { get; set; } /// @@ -99,6 +103,7 @@ public class MqttTextDiscoveryConfig : MqttDiscoveryConfig /// , default: false /// [JsonPropertyName("retain")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? Retain { get; set; } /// diff --git a/src/HomeAssistantDiscoveryNet/Entities/MqttUpdateDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttUpdateDiscoveryConfig.cs index 4d12174..d593a75 100644 --- a/src/HomeAssistantDiscoveryNet/Entities/MqttUpdateDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttUpdateDiscoveryConfig.cs @@ -28,6 +28,7 @@ public class MqttUpdateDiscoveryConfig : MqttDiscoveryConfig /// , default: true /// [JsonPropertyName("enabled_by_default")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? EnabledByDefault { get; set; } /// @@ -92,6 +93,7 @@ public class MqttUpdateDiscoveryConfig : MqttDiscoveryConfig /// , default: 0 /// [JsonPropertyName("qos")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public long? Qos { get; set; } /// @@ -111,6 +113,7 @@ public class MqttUpdateDiscoveryConfig : MqttDiscoveryConfig /// , default: false /// [JsonPropertyName("retain")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? Retain { get; set; } /// diff --git a/src/HomeAssistantDiscoveryNet/Entities/MqttValveDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttValveDiscoveryConfig.cs index 48899ba..46a9d28 100644 --- a/src/HomeAssistantDiscoveryNet/Entities/MqttValveDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttValveDiscoveryConfig.cs @@ -33,6 +33,7 @@ public class MqttValveDiscoveryConfig : MqttDiscoveryConfig /// , default: true /// [JsonPropertyName("enabled_by_default")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? EnabledByDefault { get; set; } /// @@ -74,6 +75,7 @@ public class MqttValveDiscoveryConfig : MqttDiscoveryConfig ///false if the state or position topic is defined; true otherwise. /// [JsonPropertyName("optimistic")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? Optimistic { get; set; } /// @@ -115,6 +117,7 @@ public class MqttValveDiscoveryConfig : MqttDiscoveryConfig /// , default: 0 /// [JsonPropertyName("position_closed")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public long? PositionClosed { get; set; } /// @@ -122,6 +125,7 @@ public class MqttValveDiscoveryConfig : MqttDiscoveryConfig /// , default: 100 /// [JsonPropertyName("position_open")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public long? PositionOpen { get; set; } /// @@ -129,6 +133,7 @@ public class MqttValveDiscoveryConfig : MqttDiscoveryConfig /// , default: 0 /// [JsonPropertyName("qos")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public long? Qos { get; set; } /// @@ -136,6 +141,7 @@ public class MqttValveDiscoveryConfig : MqttDiscoveryConfig /// , default: false /// [JsonPropertyName("reports_position")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? ReportsPosition { get; set; } /// @@ -143,6 +149,7 @@ public class MqttValveDiscoveryConfig : MqttDiscoveryConfig /// , default: false /// [JsonPropertyName("retain")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? Retain { get; set; } /// diff --git a/src/HomeAssistantDiscoveryNet/Entities/MqttWaterHeaterDiscoveryConfig.cs b/src/HomeAssistantDiscoveryNet/Entities/MqttWaterHeaterDiscoveryConfig.cs index b8710d7..6972b91 100644 --- a/src/HomeAssistantDiscoveryNet/Entities/MqttWaterHeaterDiscoveryConfig.cs +++ b/src/HomeAssistantDiscoveryNet/Entities/MqttWaterHeaterDiscoveryConfig.cs @@ -26,6 +26,7 @@ public class MqttWaterHeaterDiscoveryConfig : MqttDiscoveryConfig /// , default: true /// [JsonPropertyName("enabled_by_default")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? EnabledByDefault { get; set; } /// @@ -46,6 +47,7 @@ public class MqttWaterHeaterDiscoveryConfig : MqttDiscoveryConfig /// Set the initial target temperature. The default value depends on the temperature unit, and will be 43.3°C or 110°F. /// [JsonPropertyName("initial")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public long? Initial { get; set; } /// @@ -64,12 +66,14 @@ public class MqttWaterHeaterDiscoveryConfig : MqttDiscoveryConfig /// Maximum set point available. The default value depends on the temperature unit, and will be 60°C or 140°F. /// [JsonPropertyName("max_temp")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public double? MaxTemp { get; set; } /// /// Minimum set point available. The default value depends on the temperature unit, and will be 43.3°C or 110°F. /// [JsonPropertyName("min_temp")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public double? MinTemp { get; set; } /// @@ -118,6 +122,7 @@ public class MqttWaterHeaterDiscoveryConfig : MqttDiscoveryConfig ///true if no state topic defined, else false. /// [JsonPropertyName("optimistic")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? Optimistic { get; set; } /// @@ -167,6 +172,7 @@ public class MqttWaterHeaterDiscoveryConfig : MqttDiscoveryConfig ///0.1 for Celsius and 1.0 for Fahrenheit. /// [JsonPropertyName("precision")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public double? Precision { get; set; } /// @@ -174,6 +180,7 @@ public class MqttWaterHeaterDiscoveryConfig : MqttDiscoveryConfig /// , default: 0 /// [JsonPropertyName("qos")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public long? Qos { get; set; } /// @@ -181,6 +188,7 @@ public class MqttWaterHeaterDiscoveryConfig : MqttDiscoveryConfig /// , default: false /// [JsonPropertyName("retain")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)] public bool? Retain { get; set; } /// diff --git a/src/HomeAssistantDiscoveryNet/MqttDiscoveryJsonContext.cs b/src/HomeAssistantDiscoveryNet/MqttDiscoveryJsonContext.cs index e609535..d18cde7 100644 --- a/src/HomeAssistantDiscoveryNet/MqttDiscoveryJsonContext.cs +++ b/src/HomeAssistantDiscoveryNet/MqttDiscoveryJsonContext.cs @@ -21,6 +21,7 @@ namespace HomeAssistantDiscoveryNet; [JsonSerializable(typeof(MqttSceneDiscoveryConfig))] [JsonSerializable(typeof(MqttSelectDiscoveryConfig))] [JsonSerializable(typeof(MqttSensorDiscoveryConfig))] +[JsonSerializable(typeof(MqttSirenDiscoveryConfig))] [JsonSerializable(typeof(MqttSwitchDiscoveryConfig))] [JsonSerializable(typeof(MqttTagScannerDiscoveryConfig))] [JsonSerializable(typeof(MqttTemplateLightDiscoveryConfig))]