Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Home Assistant 2023.8 MQTT #20

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added object_id support
amadeo-alex committed Aug 4, 2023
commit d248eafaaf015471c4bff397ac8c55f4c4133a31
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ public abstract class DiscoveryConfigModel
/// </summary>
/// <value></value>
public string FriendlyName { get; set; }

/// <summary>
/// The MQTT topic subscribed to receive entity values.
/// </summary>
@@ -104,6 +104,30 @@ public class SensorDiscoveryConfigModel : DiscoveryConfigModel
/// <value></value>
public string Unique_id { get; set; }

private string _objectId = string.Empty;
/// <summary>
/// (Optional) An ID that will be used by Home Assistant to generate the entity ID.
/// If not provided, will be generated based on the sensor name and the device name.
/// If not provided and sensor name already includes the device name, will return the sensor name.
/// </summary>
/// <value></value>
public string Object_id
{
get
{
if (!string.IsNullOrEmpty(_objectId))
return _objectId;

// backward compatibility with HASS.Agent and HA versions below 2023.8 where device name was part of the entity ID
// will not mess with the "Home Assistant entity ID" if user already has their own naming convention with device ID included
if (Name.Contains(Device.Name))
return Name;

return $"{Device.Name}_{Name}";
}
set { _objectId = value; }
}

/// <summary>
/// (Optional) Defines the units of measurement of the sensor, if any.
/// </summary>
@@ -139,7 +163,7 @@ public class CommandDiscoveryConfigModel : DiscoveryConfigModel
public string Action_topic { get; set; }

/// <summary>
/// (Optional) The type/class of the sensor to set the icon in the frontend. See https://www.home-assistant.io/integrations/sensor/#device-class for options.
/// (Optional) The type/class of the command to set the icon in the frontend. See https://www.home-assistant.io/integrations/sensor/#device-class for options.
/// </summary>
/// <value></value>
public string Device_class { get; set; }
@@ -151,7 +175,7 @@ public class CommandDiscoveryConfigModel : DiscoveryConfigModel
public bool? Force_update { get; set; }

/// <summary>
/// (Optional) The icon for the sensor.
/// (Optional) The icon for the command.
/// </summary>
/// <value></value>
public string Icon { get; set; }
@@ -163,7 +187,7 @@ public class CommandDiscoveryConfigModel : DiscoveryConfigModel
public string Json_attributes_template { get; set; }

/// <summary>
/// (Optional) 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.
/// (Optional) The MQTT topic subscribed to receive a JSON dictionary payload and then set as command attributes. Implies force_update of the current command state when a message is received on this topic.
/// </summary>
/// <value></value>
public string Json_attributes_topic { get; set; }
@@ -187,18 +211,42 @@ public class CommandDiscoveryConfigModel : DiscoveryConfigModel
public int? Qos { get; set; }

/// <summary>
/// (Optional) An ID that uniquely identifies this sensor. If two sensors have the same unique ID, Home Assistant will raise an exception.
/// (Optional) An ID that uniquely identifies this command. If two sensors have the same unique ID, Home Assistant will raise an exception.
/// </summary>
/// <value></value>
public string Unique_id { get; set; }

private string _objectId = string.Empty;
/// <summary>
/// (Optional) An ID that will be used by Home Assistant to generate the entity ID.
/// If not provided, will be generated based on the sensor name and the device name.
/// If not provided and sensor name already includes the device name, will return the sensor name.
/// </summary>
/// <value></value>
public string Object_id
{
get
{
if (!string.IsNullOrEmpty(_objectId))
return _objectId;

// backward compatibility with HASS.Agent and HA versions below 2023.8 where device name was part of the entity ID
// will not mess with the "Home Assistant entity ID" if user already has their own naming convention with device ID included
if (Name.Contains(Device.Name))
return Name;

return $"{Device.Name}_{Name}";
}
set { _objectId = value; }
}

/// <summary>
/// (Optional) Defines a template to extract the value.
/// </summary>
/// <value></value>
public string Value_template { get; set; }
}

/// <summary>
/// This information will be used when announcing this device on the mqtt topic
/// </summary>