Skip to content

Commit 91b7295

Browse files
Improved types for generated service arguments (#1149)
* Improved types for generated service arguments * fix test
1 parent b1f7fea commit 91b7295

File tree

6 files changed

+247
-125
lines changed

6 files changed

+247
-125
lines changed

src/HassModel/NetDaemon.HassModel.CodeGenerator/MetaData/ServicesMetaData/HassServiceArgumentMapper.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,23 @@ private static Type GetClrTypeFromSelector(Selector? selectorObject)
1717
var clrType = selectorObject switch
1818
{
1919
null => typeof(object),
20-
NumberSelector s when (s.Step ?? 1) % 1 != 0 => typeof(double),
21-
NumberSelector => typeof(long),
20+
NumberSelector { Step: not null } s when s.Step % 1 == 0 => typeof(long),
21+
NumberSelector => typeof(double),
2222
EntitySelector => typeof(string),
2323
DeviceSelector => typeof(string),
2424
AreaSelector => typeof(string),
2525
_ => selectorObject.Type switch
2626
{
27+
"color_rgb" => typeof(IReadOnlyCollection<int>),
2728
"boolean" => typeof(bool),
28-
"object" => typeof(object),
29-
"time" => typeof(DateTime), // Maybe TimeOnly??,
29+
"date" => typeof(DateOnly),
30+
"time" => typeof(TimeOnly),
31+
"datetime" => typeof(DateTime),
3032
"text" => typeof(string),
3133
_ => typeof(object)
3234
},
3335
};
3436

3537
return selectorObject?.Multiple ?? false ? typeof(IEnumerable<>).MakeGenericType(clrType) : clrType;
3638
}
37-
}
39+
}

src/HassModel/NetDaemon.HassModel.Tests/CodeGenerator/ServiceMetaDataParserTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void TestSomeBasicServicesCanBeParsed()
4646
[Fact]
4747
public void TestServicesWithAdvancedFieldsCanBeParsed()
4848
{
49-
var sample = File.ReadAllText(@"CodeGenerator/ServiceMetaDataSamples/Lights.json");
49+
var sample = File.ReadAllText(@"CodeGenerator/ServiceMetaDataSamples/light.json");
5050

5151
var res = Parse(sample);
5252
res.Should().HaveCount(1);
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
{
2+
"calendar": {
3+
"create_event": {
4+
"name": "Create event",
5+
"description": "Adds a new calendar event.",
6+
"fields": {
7+
"summary": {
8+
"required": true,
9+
"example": "Department Party",
10+
"selector": {
11+
"text": null
12+
},
13+
"name": "Summary",
14+
"description": "Defines the short summary or subject for the event."
15+
},
16+
"description": {
17+
"example": "Meeting to provide technical review for \u0027Phoenix\u0027 design.",
18+
"selector": {
19+
"text": null
20+
},
21+
"name": "Description",
22+
"description": "A more complete description of the event than the one provided by the summary."
23+
},
24+
"start_date_time": {
25+
"example": "2022-03-22 20:00:00",
26+
"selector": {
27+
"datetime": null
28+
},
29+
"name": "Start time",
30+
"description": "The date and time the event should start."
31+
},
32+
"end_date_time": {
33+
"example": "2022-03-22 22:00:00",
34+
"selector": {
35+
"datetime": null
36+
},
37+
"name": "End time",
38+
"description": "The date and time the event should end."
39+
},
40+
"start_date": {
41+
"example": "2022-03-22",
42+
"selector": {
43+
"date": null
44+
},
45+
"name": "Start date",
46+
"description": "The date the all-day event should start."
47+
},
48+
"end_date": {
49+
"example": "2022-03-23",
50+
"selector": {
51+
"date": null
52+
},
53+
"name": "End date",
54+
"description": "The date the all-day event should end (exclusive)."
55+
},
56+
"in": {
57+
"example": "{\u0022days\u0022: 2} or {\u0022weeks\u0022: 2}",
58+
"name": "In",
59+
"description": "Days or weeks that you want to create the event in."
60+
},
61+
"location": {
62+
"example": "Conference Room - F123, Bldg. 002",
63+
"selector": {
64+
"text": null
65+
},
66+
"name": "Location",
67+
"description": "The location of the event."
68+
}
69+
},
70+
"target": {
71+
"entity": [
72+
{
73+
"domain": [
74+
"calendar"
75+
],
76+
"supported_features": [
77+
1
78+
]
79+
}
80+
]
81+
}
82+
},
83+
"get_events": {
84+
"name": "Get events",
85+
"description": "Get events on a calendar within a time range.",
86+
"fields": {
87+
"start_date_time": {
88+
"example": "2022-03-22 20:00:00",
89+
"selector": {
90+
"datetime": null
91+
},
92+
"name": "Start time",
93+
"description": "Returns active events after this time (exclusive). When not set, defaults to now."
94+
},
95+
"end_date_time": {
96+
"example": "2022-03-22 22:00:00",
97+
"selector": {
98+
"datetime": null
99+
},
100+
"name": "End time",
101+
"description": "Returns active events before this time (exclusive). Cannot be used with \u0027duration\u0027."
102+
},
103+
"duration": {
104+
"selector": {
105+
"duration": null
106+
},
107+
"name": "Duration",
108+
"description": "Returns active events from start_date_time until the specified duration."
109+
}
110+
},
111+
"target": {
112+
"entity": [
113+
{
114+
"domain": [
115+
"calendar"
116+
]
117+
}
118+
]
119+
},
120+
"response": {
121+
"optional": false
122+
}
123+
}
124+
}
125+
}

src/HassModel/NetDaemon.HassModel.Tests/CodeGenerator/ServiceMetaDataSamples/Lights.json renamed to src/HassModel/NetDaemon.HassModel.Tests/CodeGenerator/ServiceMetaDataSamples/light.json

File renamed without changes.

0 commit comments

Comments
 (0)