Skip to content

Commit

Permalink
Merge pull request #21 from JonasMH/develop
Browse files Browse the repository at this point in the history
Fix build warnings and options configure
  • Loading branch information
JonasMH authored Sep 23, 2023
2 parents f549992 + fb3fd84 commit 4481693
Show file tree
Hide file tree
Showing 21 changed files with 47 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class MqttAlarmControlPanelDiscoveryConfig : MqttDiscoveryConfig
/// The MQTT topic to publish commands to change the alarm state.
///</summary>
[JsonPropertyName("command_topic")]
public string CommandTopic { get; set; }
public string CommandTopic { get; set; } = null!;

///<summary>
/// Flag which defines if the entity should be enabled when first added.
Expand Down Expand Up @@ -170,7 +170,7 @@ public class MqttAlarmControlPanelDiscoveryConfig : MqttDiscoveryConfig
/// The MQTT topic subscribed to receive state updates.
///</summary>
[JsonPropertyName("state_topic")]
public string StateTopic { get; set; }
public string StateTopic { get; set; } = null!;

///<summary>
/// Defines a template to extract the value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public class MqttBinarySensorDiscoveryConfig : MqttDiscoveryConfig
/// The MQTT topic subscribed to receive sensor’s state.
///</summary>
[JsonPropertyName("state_topic")]
public string StateTopic { get; set; }
public string StateTopic { get; set; } = null!;

///<summary>
/// Defines a template that returns a string to be compared to payload_on/payload_off or an empty string, in which case the MQTT message will be removed. Available variables: entity_id. Remove this option when ‘payload_on’ and ‘payload_off’ are sufficient to match your payloads (i.e no pre-processing of original message is required).
Expand Down
2 changes: 1 addition & 1 deletion src/ToMqttNet/DeviceTypes/MqttCameraDiscoveryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ public class MqttCameraDiscoveryConfig : MqttDiscoveryConfig
/// The MQTT topic to subscribe to.
///</summary>
[JsonPropertyName("topic")]
public string Topic { get; set; }
public string Topic { get; set; } = null!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public class MqttDefaultLightDiscoveryConfig : MqttDiscoveryConfig
/// The MQTT topic to publish commands to change the switch state.
///</summary>
[JsonPropertyName("command_topic")]
public string CommandTopic { get; set; }
public string CommandTopic { get; set; } = null!;

///<summary>
/// Flag which defines if the entity should be enabled when first added.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class MqttDeviceTrackerDiscoveryConfig : MqttDiscoveryConfig
/// The MQTT topic subscribed to receive device tracker state changes.
///</summary>
[JsonPropertyName("state_topic")]
public string StateTopic { get; set; }
public string StateTopic { get; set; } = null!;

///<summary>
/// Defines a template that returns a device tracker state.
Expand Down
8 changes: 4 additions & 4 deletions src/ToMqttNet/DeviceTypes/MqttDeviceTriggerDiscoveryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MqttDeviceTriggerDiscoveryConfig : MqttDiscoveryConfig
/// The type of automation, must be ‘trigger’.
///</summary>
[JsonPropertyName("automation_type")]
public string AutomationType { get; set; }
public string AutomationType { get; set; } = null!;

///<summary>
/// Optional payload to match the payload being sent over the topic.
Expand All @@ -32,19 +32,19 @@ public class MqttDeviceTriggerDiscoveryConfig : MqttDiscoveryConfig
/// The MQTT topic subscribed to receive trigger events.
///</summary>
[JsonPropertyName("topic")]
public string Topic { get; set; }
public string Topic { get; set; } = null!;

///<summary>
/// The type of the trigger, e.g. button_short_press. Entries supported by the frontend: button_short_press, button_short_release, button_long_press, button_long_release, button_double_press, button_triple_press, button_quadruple_press, button_quintuple_press. If set to an unsupported value, will render as subtype type, e.g. button_1 spammed with type set to spammed and subtype set to button_1
///</summary>
[JsonPropertyName("type")]
public string Type { get; set; }
public string Type { get; set; } = null!;

///<summary>
/// The subtype of the trigger, e.g. button_1. Entries supported by the frontend: turn_on, turn_off, button_1, button_2, button_3, button_4, button_5, button_6. If set to an unsupported value, will render as subtype type, e.g. left_button pressed with type set to button_short_press and subtype set to left_button
///</summary>
[JsonPropertyName("subtype")]
public string Subtype { get; set; }
public string Subtype { get; set; } = null!;

///<summary>
/// Defines a template to extract the value.
Expand Down
22 changes: 11 additions & 11 deletions src/ToMqttNet/DeviceTypes/MqttEventDiscoveryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public class MqttEventDiscoveryConfig : MqttDiscoveryConfig
/// A list of valid event_type strings.
///</summary>
[JsonPropertyName("event_types")]
public List<string> EventTypes { get; set; }
public List<string> EventTypes { get; set; } = null!;


///<summary>
/// 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.
///</summary>
[JsonPropertyName("json_attributes_template")]
///<summary>
/// 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.
///</summary>
[JsonPropertyName("json_attributes_template")]
public string? JsonAttributesTemplate { get; set; }

///<summary>
Expand Down Expand Up @@ -80,12 +80,12 @@ public class MqttEventDiscoveryConfig : MqttDiscoveryConfig
[JsonPropertyName("qos")]
public long? Qos { get; set; }

///<summary>
/// The MQTT topic subscribed to receive JSON event payloads. The JSON payload should contain the event_type element. The event type should be one of the configured event_types.
/// , default: None
///</summary>
[JsonPropertyName("state_topic")]
public string StateTopic { get; set; }
///<summary>
/// The MQTT topic subscribed to receive JSON event payloads. The JSON payload should contain the event_type element. The event type should be one of the configured event_types.
/// , default: None
///</summary>
[JsonPropertyName("state_topic")]
public string StateTopic { get; set; } = null!;

///<summary>
/// Defines a template to extract the value and render it to a valid JSON event payload. If the template throws an error, the current state will be used instead.
Expand Down
2 changes: 1 addition & 1 deletion src/ToMqttNet/DeviceTypes/MqttFanDiscoveryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class MqttFanDiscoveryConfig : MqttDiscoveryConfig
/// The MQTT topic to publish commands to change the fan state.
///</summary>
[JsonPropertyName("command_topic")]
public string CommandTopic { get; set; }
public string CommandTopic { get; set; } = null!;

///<summary>
/// Flag which defines if the entity should be enabled when first added.
Expand Down
4 changes: 2 additions & 2 deletions src/ToMqttNet/DeviceTypes/MqttHumidifierDiscoveryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class MqttHumidifierDiscoveryConfig : MqttDiscoveryConfig
/// The MQTT topic to publish commands to change the humidifier state.
///</summary>
[JsonPropertyName("command_topic")]
public string CommandTopic { get; set; }
public string CommandTopic { get; set; } = null!;

///<summary>
/// The device class of the MQTT device. Must be either humidifier or dehumidifier.
Expand Down Expand Up @@ -142,7 +142,7 @@ public class MqttHumidifierDiscoveryConfig : MqttDiscoveryConfig
/// The MQTT topic to publish commands to change the humidifier target humidity state based on a percentage.
///</summary>
[JsonPropertyName("target_humidity_command_topic")]
public string TargetHumidityCommandTopic { get; set; }
public string TargetHumidityCommandTopic { get; set; } = null!;

///<summary>
/// The MQTT topic subscribed to receive humidifier target humidity.
Expand Down
2 changes: 1 addition & 1 deletion src/ToMqttNet/DeviceTypes/MqttJsonLightDiscoveryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class MqttJsonLightDiscoveryConfig : MqttDiscoveryConfig
/// The MQTT topic to publish commands to change the light’s state.
///</summary>
[JsonPropertyName("command_topic")]
public string CommandTopic { get; set; }
public string CommandTopic { get; set; } = null!;

///<summary>
/// Flag which defines if the entity should be enabled when first added.
Expand Down
2 changes: 1 addition & 1 deletion src/ToMqttNet/DeviceTypes/MqttLockDiscoveryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class MqttLockDiscoveryConfig : MqttDiscoveryConfig
/// The MQTT topic to publish commands to change the lock state.
///</summary>
[JsonPropertyName("command_topic")]
public string CommandTopic { get; set; }
public string CommandTopic { get; set; } = null!;

///<summary>
/// Flag which defines if the entity should be enabled when first added.
Expand Down
2 changes: 1 addition & 1 deletion src/ToMqttNet/DeviceTypes/MqttNumberDiscoveryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class MqttNumberDiscoveryConfig : MqttDiscoveryConfig
/// The MQTT topic to publish commands to change the number.
///</summary>
[JsonPropertyName("command_topic")]
public string CommandTopic { get; set; }
public string CommandTopic { get; set; } = null!;

///<summary>
/// Flag which defines if the entity should be enabled when first added.
Expand Down
4 changes: 2 additions & 2 deletions src/ToMqttNet/DeviceTypes/MqttSelectDiscoveryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class MqttSelectDiscoveryConfig : MqttDiscoveryConfig
/// The MQTT topic to publish commands to change the selected option.
///</summary>
[JsonPropertyName("command_topic")]
public string CommandTopic { get; set; }
public string CommandTopic { get; set; } = null!;

///<summary>
/// Flag which defines if the entity should be enabled when first added.
Expand Down Expand Up @@ -73,7 +73,7 @@ public class MqttSelectDiscoveryConfig : MqttDiscoveryConfig
/// List of options that can be selected. An empty list or a list with a single item is allowed.
///</summary>
[JsonPropertyName("options")]
public List<string> Options { get; set; }
public List<string> Options { get; set; } = null!;

///<summary>
/// The maximum QoS level of the state topic. Default is 0 and will also be used to publishing messages.
Expand Down
2 changes: 1 addition & 1 deletion src/ToMqttNet/DeviceTypes/MqttSensorDiscoveryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public class MqttSensorDiscoveryConfig : MqttDiscoveryConfig
/// The MQTT topic subscribed to receive sensor values.
///</summary>
[JsonPropertyName("state_topic")]
public string StateTopic { get; set; }
public string StateTopic { get; set; } = null!;

///<summary>
/// Defines the units of measurement of the sensor, if any.
Expand Down
2 changes: 1 addition & 1 deletion src/ToMqttNet/DeviceTypes/MqttTagScannerDiscoveryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class MqttTagScannerDiscoveryConfig : MqttDiscoveryConfig
/// The MQTT topic subscribed to receive tag scanned events.
///</summary>
[JsonPropertyName("topic")]
public string Topic { get; set; }
public string Topic { get; set; } = null!;

///<summary>
/// Defines a template that returns a tag ID.
Expand Down
8 changes: 4 additions & 4 deletions src/ToMqttNet/DeviceTypes/MqttTemplateLightDiscoveryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ public class MqttTemplateLightDiscoveryConfig : MqttDiscoveryConfig
/// The template for off state changes. Available variables: state and transition.
///</summary>
[JsonPropertyName("command_off_template")]
public string CommandOffTemplate { get; set; }
public string CommandOffTemplate { get; set; } = null!;

///<summary>
/// The template for on state changes. Available variables: state, brightness, color_temp, red, green, blue, flash, transition and effect. Values red, green, blue, brightness are provided as integers from range 0-255. Value of color_temp is provided as integer representing mired units.
///</summary>
[JsonPropertyName("command_on_template")]
public string CommandOnTemplate { get; set; }
public string CommandOnTemplate { get; set; } = null!;

///<summary>
/// The MQTT topic to publish commands to change the light’s state.
///</summary>
[JsonPropertyName("command_topic")]
public string CommandTopic { get; set; }
public string CommandTopic { get; set; } = null!;

///<summary>
/// Flag which defines if the entity should be enabled when first added.
Expand All @@ -72,7 +72,7 @@ public class MqttTemplateLightDiscoveryConfig : MqttDiscoveryConfig
/// List of possible effects.
///</summary>
[JsonPropertyName("effect_list")]
public List<string> EffectList { get; set; }
public List<string> EffectList { get; set; } = null!;

///<summary>
/// Template to extract effect from the state payload value.
Expand Down
4 changes: 2 additions & 2 deletions src/ToMqttNet/DeviceTypes/MqttVacuumDiscoveryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class MqttVacuumDiscoveryConfig : MqttDiscoveryConfig
/// List of possible fan speeds for the vacuum.
///</summary>
[JsonPropertyName("fan_speed_list")]
public List<string> FanSpeedList { get; set; }
public List<string> FanSpeedList { get; set; } = null!;

///<summary>
/// 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.
Expand Down Expand Up @@ -148,5 +148,5 @@ public class MqttVacuumDiscoveryConfig : MqttDiscoveryConfig
///start, stop, return_home, status, battery, clean_spot
///</summary>
[JsonPropertyName("supported_features")]
public List<string> SupportedFeatures { get; set; }
public List<string> SupportedFeatures { get; set; } = null!;
}
3 changes: 1 addition & 2 deletions src/ToMqttNet/MqttConnectionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ public class MqttConnectionOptions
[Required]
public string NodeId { get; set; } = null!;


public MqttClientOptions ClientOptions { get; set; } = new MqttClientOptions { };
public MqttClientOptions ClientOptions { get; set; } = new MqttClientOptions { };
}
6 changes: 4 additions & 2 deletions src/ToMqttNet/MqttConnectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,18 @@ await _mqttClient.EnqueueAsync(
OnConnect?.Invoke(this, new EventArgs());
};

_mqttClient.DisconnectedAsync += async (evnt) =>
_mqttClient.DisconnectedAsync += (evnt) =>
{
_logger.LogInformation(evnt.Exception, "Disconnected from mqtt: {reason}", evnt.Reason);
OnDisconnect?.Invoke(this, new EventArgs());
return Task.CompletedTask;
};

_mqttClient.ApplicationMessageReceivedAsync += async (evnt) =>
_mqttClient.ApplicationMessageReceivedAsync += (evnt) =>
{
_logger.LogTrace("{topic}: {message}", evnt.ApplicationMessage.Topic, evnt.ApplicationMessage.ConvertPayloadToString());
OnApplicationMessageReceived?.Invoke(this, evnt);
return Task.CompletedTask;
};

await _mqttClient.StartAsync(optionsBuilder.Build());
Expand Down
3 changes: 3 additions & 0 deletions src/ToMqttNet/MqttConnectionServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ public static class MqttConnectionServiceCollectionExtensions
{
public static IServiceCollection AddMqttConnection(this IServiceCollection services, Action<MqttConnectionOptions> configureOptions)
{
services.AddOptions<MqttConnectionOptions>()
.Configure(configureOptions);

services.AddSingleton<MqttConnectionService>();
services.AddSingleton<IMqttConnectionService>(x => x.GetRequiredService<MqttConnectionService>());
services.AddHostedService(x => x.GetRequiredService<MqttConnectionService>());
Expand Down
4 changes: 2 additions & 2 deletions src/ToMqttNet/MqttDiscoveryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public abstract class MqttDiscoveryConfig


/// <summary>
/// When <see cref="Availablilty"/> is configured, this controls the conditions needed to set the entity to available.
/// When <see cref="AvailabilityMode"/> is configured, this controls the conditions needed to set the entity to available.
/// <br/>
/// If set to <see cref="MqttDiscoveryAvailabilityMode.All"/>, payload_available must be received on all configured availability topics before the entity is marked as online.
/// <br/>
Expand Down Expand Up @@ -137,7 +137,7 @@ public class MqttDiscoveryAvailablilty
/// An MQTT topic subscribed to receive availability (online/offline) updates.
/// </summary>
[JsonPropertyName("topic")]
public string Topic { get; init; }
public string Topic { get; init; } = null!;

/// <summary>
/// Defines a template to extract device’s availability from the topic. To determine the devices’s availability result of this template will be compared to payload_available and payload_not_available.
Expand Down

0 comments on commit 4481693

Please sign in to comment.