Skip to content

Commit

Permalink
Add media player to default meta (#903)
Browse files Browse the repository at this point in the history
* Added media_player to default metadata and deprecated attribute base classes

* Spelling error

* Suppress obsolete warnings in our own code :)

* Fix tests to ignore obsolete warnings.
  • Loading branch information
helto4real authored Aug 5, 2023
1 parent 6bea7d4 commit 44b2f24
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Text.Json.Serialization;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using NetDaemon.HassModel.Entities.Core;

namespace NetDaemon.HassModel.CodeGenerator.Helpers;

[SuppressMessage("", "CS0618")]
internal static class NamingHelper
{
public const string EntitiesClassName = "Entities";
Expand Down Expand Up @@ -58,7 +60,7 @@ public static string SimplifyTypeName(Type type)
// Use short name if the type is in one of the using namespaces
return UsingNamespaces.Any(u => type.Namespace == u) ? type.Name : type.FullName!;
}

public static readonly string[] UsingNamespaces =
{
"System",
Expand All @@ -67,7 +69,10 @@ public static string SimplifyTypeName(Type type)
typeof(JsonPropertyNameAttribute).Namespace!,
typeof(IHaContext).Namespace!,
typeof(Entity).Namespace!,
// Have to suppress warning because of obsolete LightAttributesBase. Suppress message attribute did not work.
#pragma warning disable CS0618 // Type or member is obsolete
typeof(LightAttributesBase).Namespace!
#pragma warning restore CS0618 // Type or member is obsolete
};

private static string GetVariableName(string typeName, string variablePrefix)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,157 @@
"clrType": "System.Object"
}
]
},
{
"domain": "media_player",
"entities": [],
"attributes": [
{
"jsonName": "app_id",
"cSharpName": "AppId",
"clrType": "System.String"
},
{
"jsonName": "app_name",
"cSharpName": "AppName",
"clrType": "System.String"
},
{
"jsonName": "device_class",
"cSharpName": "DeviceClass",
"clrType": "System.String"
},
{
"jsonName": "entity_picture_local",
"cSharpName": "EntityPictureLocal",
"clrType": "System.String"
},
{
"jsonName": "entity_picture",
"cSharpName": "EntityPicture",
"clrType": "System.String"
},
{
"jsonName": "icon",
"cSharpName": "Icon",
"clrType": "System.String"
},
{
"jsonName": "friendly_name",
"cSharpName": "FriendlyName",
"clrType": "System.String"
},
{
"jsonName": "supported_features",
"cSharpName": "SupportedFeatures",
"clrType": "System.Double"
},
{
"jsonName": "volume_level",
"cSharpName": "VolumeLevel",
"clrType": "System.Double"
},
{
"jsonName": "is_volume_muted",
"cSharpName": "IsVolumeMuted",
"clrType": "System.Boolean"
},
{
"jsonName": "media_content_id",
"cSharpName": "MediaContentId",
"clrType": "System.String"
},
{
"jsonName": "media_content_type",
"cSharpName": "MediaContentType",
"clrType": "System.String"
},
{
"jsonName": "media_duration",
"cSharpName": "MediaDuration",
"clrType": "System.Double"
},
{
"jsonName": "media_position",
"cSharpName": "MediaPosition",
"clrType": "System.Double"
},
{
"jsonName": "media_position_updated_at",
"cSharpName": "MediaPositionUpdatedAt",
"clrType": "System.String"
},
{
"jsonName": "media_title",
"cSharpName": "MediaTitle",
"clrType": "System.String"
},
{
"jsonName": "media_album_name",
"cSharpName": "MediaAlbumName",
"clrType": "System.String"
},
{
"jsonName": "media_image_url",
"cSharpName": "MediaImageUrl",
"clrType": "System.String"
},
{
"jsonName": "media_artist",
"cSharpName": "MediaArtist",
"clrType": "System.String"
},
{
"jsonName": "sound_mode_list",
"cSharpName": "SoundModeList",
"clrType": "System.Collections.Generic.IReadOnlyList\u00601[System.String]"
},
{
"jsonName": "media_track",
"cSharpName": "MediaTrack",
"clrType": "System.Object"
},
{
"jsonName": "shuffle",
"cSharpName": "Shuffle",
"clrType": "System.Boolean"
},
{
"jsonName": "repeat",
"cSharpName": "Repeat",
"clrType": "System.String"
},
{
"jsonName": "source_list",
"cSharpName": "SourceList",
"clrType": "System.Collections.Generic.IReadOnlyList\u00601[System.String]"
},
{
"jsonName": "source",
"cSharpName": "Source",
"clrType": "System.String"
},
{
"jsonName": "adb_response",
"cSharpName": "AdbResponse",
"clrType": "System.Object"
},
{
"jsonName": "hdmi_input",
"cSharpName": "HdmiInput",
"clrType": "System.Object"
},
{
"jsonName": "sound_mode",
"cSharpName": "SoundMode",
"clrType": "System.String"
},
{
"jsonName": "sound_mode_raw",
"cSharpName": "SoundModeRaw",
"clrType": "System.String"
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using System.Reflection;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.Json.Serialization;
using NetDaemon.HassModel.Entities.Core;

namespace NetDaemon.HassModel.CodeGenerator;

internal static class EntityMetaDataMerger
{
// We have to disable warnings. SuppressMessage attribute did not work.
#pragma warning disable CS0618 // Type or member is obsolete
private static readonly Type[] _possibleBaseTypes = typeof(LightAttributesBase).Assembly.GetTypes();
#pragma warning restore CS0618 // Type or member is obsolete

// We need to merge the previously saved metadata with the current metadata from HA
// We do this because sometimes entities do not provide all their attributes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public static void AssertCodeCompiles(string generated, string appCode)
var warningsOrErrors = emitResult.Diagnostics
.Where(d => d.Severity is DiagnosticSeverity.Error or DiagnosticSeverity.Warning).ToList();

if (!warningsOrErrors.Any()) return;
// Ignore obsolete warnings
if (warningsOrErrors.All(n => n.Id == "CS0618")) return;

var msg = new StringBuilder("Compile of generated code failed.\r\n");
foreach (var diagnostic in warningsOrErrors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ public async Task ControllerShouldReturnDefaultValueForMetadata()
var result = await controller.LoadEntitiesMetaDataAsync();

// ASSERT
result.Domains.Count.Should().Be(1);
result.Domains.Count.Should().Be(2);
result.Domains.Single(x => x.Domain == "light").Should().NotBeNull();
result.Domains.Single(x => x.Domain == "light").Attributes.SingleOrDefault(x => x.JsonName == "brightness").Should().NotBeNull();
result.Domains.Single(x => x.Domain == "media_player").Should().NotBeNull();
result.Domains.Single(x => x.Domain == "media_player").Attributes.SingleOrDefault(x => x.JsonName == "media_artist").Should().NotBeNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/// We use doubles even if the standard is integer to make the serializing more robust
/// since we cannot trust HA integrations use integers when supposed to
/// </remarks>
[Obsolete("Usage of attribute base classes are deprecated, default meta data is now used to replace it")]
public record LightAttributesBase
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma warning disable CS1591
#pragma warning disable CA1056

[Obsolete("Usage of attribute base classes are deprecated, default meta data is now used to replace it")]
public record MediaPlayerAttributesBase
{
[JsonPropertyName("app_id")]
Expand Down

0 comments on commit 44b2f24

Please sign in to comment.