Skip to content

Commit 11419ae

Browse files
committed
Improve shelly
1 parent 9cd944b commit 11419ae

8 files changed

Lines changed: 86 additions & 77 deletions

File tree

src/Namotion.NuGetPlugins/NuGetPackage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Namotion.NuGetPlugins
66
{
77
public class NuGetPackage
88
{
9-
private IPackageSearchMetadata _metadata;
9+
private readonly IPackageSearchMetadata _metadata;
1010

1111
public NuGetPackage(IPackageSearchMetadata metadata)
1212
{

src/Namotion.Shelly/ServiceCollectionExtensions.cs renamed to src/Namotion.Shelly/Extensions/ServiceCollectionExtensions.cs

File renamed without changes.

src/Namotion.Shelly/ShellyCover.cs

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public partial class ShellyCover :
3636
[State(Unit = StateUnit.Percent)]
3737
public decimal? Position => (100 - CurrentPosition) / 100m;
3838

39+
[State]
3940
[Derived]
4041
public RollerShutterState State => LastState switch
4142
{
@@ -55,38 +56,47 @@ public partial class ShellyCover :
5556
public bool? IsMoving => PowerConsumption > 1;
5657

5758
[State(Unit = StateUnit.Watt)]
58-
[JsonPropertyName("power")]
59-
public partial decimal? PowerConsumption { get; set; }
59+
[JsonPropertyName("power"), JsonInclude]
60+
public partial decimal? PowerConsumption { get; internal set; }
6061

61-
[JsonPropertyName("state"), State]
62-
public partial string? LastState { get; set; }
62+
[State]
63+
[JsonPropertyName("state"), JsonInclude]
64+
public partial string? LastState { get; internal set; }
6365

64-
[JsonPropertyName("source"), State]
65-
public partial string? Source { get; set; }
66+
[State]
67+
[JsonPropertyName("source"), JsonInclude]
68+
public partial string? Source { get; internal set; }
6669

67-
[JsonPropertyName("is_valid"), State]
68-
public partial bool? IsValid { get; set; }
70+
[State]
71+
[JsonPropertyName("is_valid"), JsonInclude]
72+
public partial bool? IsValid { get; internal set; }
6973

70-
[JsonPropertyName("safety_switch"), State]
71-
public partial bool? IsSafetySwitchTriggered { get; set; }
74+
[State]
75+
[JsonPropertyName("safety_switch"), JsonInclude]
76+
public partial bool? IsSafetySwitchTriggered { get; internal set; }
7277

73-
[JsonPropertyName("overtemperature"), State]
74-
public partial bool? OvertemperatureOccurred { get; set; }
78+
[State]
79+
[JsonPropertyName("overtemperature"), JsonInclude]
80+
public partial bool? OvertemperatureOccurred { get; internal set; }
7581

76-
[JsonPropertyName("stop_reason"), State]
77-
public partial string? StopReason { get; set; }
82+
[State]
83+
[JsonPropertyName("stop_reason"), JsonInclude]
84+
public partial string? StopReason { get; internal set; }
7885

79-
[JsonPropertyName("last_direction"), State]
80-
public partial string? LastDirection { get; set; }
86+
[State]
87+
[JsonPropertyName("last_direction"), JsonInclude]
88+
public partial string? LastDirection { get; internal set; }
8189

82-
[JsonPropertyName("current_pos")]
83-
public partial int? CurrentPosition { get; set; }
90+
[JsonPropertyName("current_pos"), JsonInclude]
91+
public partial int? CurrentPosition { get; internal set; }
8492

85-
[JsonPropertyName("calibrating"), State]
86-
public partial bool? IsCalibrating { get; set; }
93+
[State]
94+
[JsonPropertyName("calibrating"), JsonInclude]
95+
public partial bool? IsCalibrating { get; internal set; }
8796

88-
[JsonPropertyName("positioning"), State]
89-
public partial bool? IsPositioning { get; set; }
97+
[State]
98+
[JsonPropertyName("positioning"), JsonInclude]
99+
public partial bool? IsPositioning { get; internal set; }
90100

91101
[Operation]
92102
public async Task OpenAsync(CancellationToken cancellationToken)

src/Namotion.Shelly/ShellyDevice.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public partial class ShellyDevice :
3333

3434
public override string Title => $"Shelly: {Information?.Name ?? Information?.Application}";
3535

36-
public partial DateTimeOffset? LastUpdated { get; protected set; }
36+
public partial DateTimeOffset? LastUpdated { get; private set; }
3737

3838
public string IconName => "fas fa-box";
3939

@@ -47,7 +47,7 @@ public partial class ShellyDevice :
4747
public partial bool IsConnected { get; internal set; }
4848

4949
[ScanForState]
50-
public partial ShellyInformation? Information { get; protected set; }
50+
public partial ShellyInformation? Information { get; private set; }
5151

5252
[State]
5353
public partial ShellyEnergyMeter? EnergyMeter { get; internal set; }
@@ -59,7 +59,7 @@ public partial class ShellyDevice :
5959
public partial ShellySwitch? Switch1 { get; internal set; }
6060

6161
[State]
62-
public partial ShellyCover? Cover { get; protected set; }
62+
public partial ShellyCover? Cover { get; internal set; }
6363

6464
protected override TimeSpan PollingInterval =>
6565
TimeSpan.FromMilliseconds(Cover?.IsMoving == true ? 1000 : RefreshInterval);
@@ -152,7 +152,7 @@ private async Task RefreshAsync(CancellationToken cancellationToken)
152152
{
153153
var emDataStatusResponse = await httpClient.GetAsync($"http://{IpAddress}/rpc/EMData.GetStatus?id=0", cancellationToken);
154154
json = await emDataStatusResponse.Content.ReadAsStringAsync(cancellationToken);
155-
EnergyMeter.EnergyData = JsonUtilities.PopulateOrDeserialize(EnergyMeter.EnergyData, json);
155+
EnergyMeter.Status = JsonUtilities.PopulateOrDeserialize(EnergyMeter.Status, json);
156156
EnergyMeter.Update();
157157
}
158158
}

src/Namotion.Shelly/ShellyEnergyMeter.cs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,53 +33,55 @@ public partial class ShellyEnergyMeter :
3333
public decimal? PowerConsumption => TotalActivePower;
3434

3535
[ScanForState]
36-
public partial ShellyEnergyData? EnergyData { get; internal set; }
36+
public partial ShellyEnergyMeterStatus? Status { get; internal set; }
3737

3838
[State]
39-
public partial ShellyEnergyMeterPhase PhaseA { get; protected set; }
39+
public partial ShellyEnergyMeterPhase PhaseA { get; private set; }
4040

4141
[State]
42-
public partial ShellyEnergyMeterPhase PhaseB { get; protected set; }
42+
public partial ShellyEnergyMeterPhase PhaseB { get; private set; }
4343

4444
[State]
45-
public partial ShellyEnergyMeterPhase PhaseC { get; protected set; }
45+
public partial ShellyEnergyMeterPhase PhaseC { get; private set; }
4646

47-
[Derived]
4847
[State(Unit = StateUnit.WattHour, IsCumulative = true)]
49-
public decimal? TotalConsumedEnergy => EnergyData?.TotalActiveEnergy;
48+
public decimal? TotalConsumedEnergy { get; private set; }
5049

5150
/// <summary>
5251
/// Gets or sets the ID of the EM1 component.
5352
/// </summary>
54-
[JsonPropertyName("id")]
55-
public partial int Id { get; set; }
53+
[JsonPropertyName("id"), JsonInclude]
54+
public partial int Id { get; internal set; }
5655

5756
/// <summary>
5857
/// Gets or sets the total current in amperes.
5958
/// </summary>
60-
[JsonPropertyName("total_current"), State(Unit = StateUnit.Ampere)]
61-
public partial double TotalCurrent { get; set; }
59+
[State(Unit = StateUnit.Ampere)]
60+
[JsonPropertyName("total_current"), JsonInclude]
61+
public partial double TotalCurrent { get; internal set; }
6262

6363
/// <summary>
6464
/// Gets or sets the total active power in watts.
6565
/// Active power (real power) is the actual power consumed by electrical equipment to perform useful work, such as running a motor or lighting a bulb.
6666
/// </summary>
67-
[JsonPropertyName("total_act_power"), State(Unit = StateUnit.Watt)]
68-
public partial decimal TotalActivePower { get; set; }
67+
[JsonPropertyName("total_act_power"), JsonInclude]
68+
public partial decimal TotalActivePower { get; internal set; }
6969

7070
/// <summary>
7171
/// Gets or sets the total apparent power in volt-amperes.
7272
/// Apparent power is the combination of active power (real power) and reactive power.
7373
/// It represents the total power used by the electrical equipment to do work and sustain the magnetic and electric fields.
7474
/// </summary>
75-
[JsonPropertyName("total_aprt_power"), State(Unit = StateUnit.Watt)]
76-
public partial double TotalApparentPower { get; set; }
75+
[State(Unit = StateUnit.WattHour)]
76+
[JsonPropertyName("total_aprt_power"), JsonInclude]
77+
public partial double TotalApparentPower { get; internal set; }
7778

7879
/// <summary>
7980
/// Gets or sets the current of the neutral line in amperes.
8081
/// </summary>
81-
[JsonPropertyName("n_current"), State(Unit = StateUnit.Ampere)]
82-
public partial double? NeutralCurrent { get; set; }
82+
[State(Unit = StateUnit.Ampere)]
83+
[JsonPropertyName("n_current"), JsonInclude]
84+
public partial double? NeutralCurrent { get; internal set; }
8385

8486
public ShellyEnergyMeter()
8587
{
@@ -88,17 +90,19 @@ public ShellyEnergyMeter()
8890
PhaseC = new ShellyEnergyMeterPhase("c");
8991
}
9092

91-
public void Update()
93+
internal void Update()
9294
{
95+
TotalConsumedEnergy = Status?.TotalActiveEnergy;
96+
9397
PhaseA.Current = PhaseACurrent;
9498
PhaseA.Voltage = PhaseAVoltage;
9599
PhaseA.ActivePower = PhaseAActivePower;
96100
PhaseA.ApparentPower = PhaseAApparentPower;
97101
PhaseA.PowerFactor = PhaseAPowerFactor;
98102
PhaseA.Frequency = PhaseAFrequency;
99103

100-
PhaseA.TotalActiveEnergy = EnergyData?.PhaseATotalActiveEnergy;
101-
PhaseA.TotalActiveReturnedEnergy = EnergyData?.PhaseATotalActiveReturnedEnergy;
104+
PhaseA.TotalActiveEnergy = Status?.PhaseATotalActiveEnergy;
105+
PhaseA.TotalActiveReturnedEnergy = Status?.PhaseATotalActiveReturnedEnergy;
102106

103107
PhaseB.Current = PhaseBCurrent;
104108
PhaseB.Voltage = PhaseBVoltage;
@@ -107,8 +111,8 @@ public void Update()
107111
PhaseB.PowerFactor = PhaseBPowerFactor;
108112
PhaseB.Frequency = PhaseBFrequency;
109113

110-
PhaseB.TotalActiveEnergy = EnergyData?.PhaseBTotalActiveEnergy;
111-
PhaseB.TotalActiveReturnedEnergy = EnergyData?.PhaseBTotalActiveReturnedEnergy;
114+
PhaseB.TotalActiveEnergy = Status?.PhaseBTotalActiveEnergy;
115+
PhaseB.TotalActiveReturnedEnergy = Status?.PhaseBTotalActiveReturnedEnergy;
112116

113117
PhaseC.Current = PhaseCCurrent;
114118
PhaseC.Voltage = PhaseCVoltage;
@@ -117,8 +121,8 @@ public void Update()
117121
PhaseC.PowerFactor = PhaseCPowerFactor;
118122
PhaseC.Frequency = PhaseCFrequency;
119123

120-
PhaseC.TotalActiveEnergy = EnergyData?.PhaseCTotalActiveEnergy;
121-
PhaseC.TotalActiveReturnedEnergy = EnergyData?.PhaseCTotalActiveReturnedEnergy;
124+
PhaseC.TotalActiveEnergy = Status?.PhaseCTotalActiveEnergy;
125+
PhaseC.TotalActiveReturnedEnergy = Status?.PhaseCTotalActiveReturnedEnergy;
122126
}
123127

124128
/// <summary>
@@ -237,10 +241,4 @@ public void Update()
237241
/// </summary>
238242
[JsonPropertyName("c_freq"), JsonInclude]
239243
public double PhaseCFrequency { get; internal set; }
240-
241-
[JsonExtensionData]
242-
public Dictionary<string, object>? ExtensionData { get; set; }
243-
244-
//[JsonPropertyName("user_calibrated_phase")]
245-
//public List<object> UserCalibratedPhase { get; set; }
246244
}

src/Namotion.Shelly/ShellyEnergyDataBase.cs renamed to src/Namotion.Shelly/ShellyEnergyMeterStatus.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
using System.Text.Json.Serialization;
2-
32
using HomeBlaze.Abstractions.Attributes;
4-
53
using Namotion.Interceptor.Attributes;
64

75
namespace Namotion.Shelly;
86

97
[InterceptorSubject]
10-
public partial class ShellyEnergyData
8+
public partial class ShellyEnergyMeterStatus
119
{
1210
/// <summary>
1311
/// Gets the identifier.

src/Namotion.Shelly/ShellyInformation.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,85 +13,85 @@ public partial class ShellyInformation
1313
/// <summary>
1414
/// Gets the ID of the device.
1515
/// </summary>
16-
[JsonPropertyName("id")]
1716
[State]
17+
[JsonPropertyName("id")]
1818
public partial string? Id { get; init; }
1919

2020
/// <summary>
2121
/// Gets the name of the device.
2222
/// </summary>
23-
[JsonPropertyName("name")]
2423
[State]
24+
[JsonPropertyName("name")]
2525
public partial string? Name { get; init; }
2626

2727
/// <summary>
2828
/// Gets the MAC address of the device.
2929
/// </summary>
30-
[JsonPropertyName("mac")]
3130
[State]
31+
[JsonPropertyName("mac")]
3232
public partial string? MacAddress { get; init; }
3333

3434
/// <summary>
3535
/// Gets the slot of the device.
3636
/// </summary>
37-
[JsonPropertyName("slot")]
3837
[State]
38+
[JsonPropertyName("slot")]
3939
public partial int? Slot { get; init; }
4040

4141
/// <summary>
4242
/// Gets the model of the device.
4343
/// </summary>
44-
[JsonPropertyName("model")]
4544
[State]
45+
[JsonPropertyName("model")]
4646
public partial string? Model { get; init; }
4747

4848
/// <summary>
4949
/// Gets the generation of the device.
5050
/// </summary>
51-
[JsonPropertyName("gen")]
5251
[State]
52+
[JsonPropertyName("gen")]
5353
public partial int? Generation { get; init; }
5454

5555
/// <summary>
5656
/// Gets the firmware identifier of the device.
5757
/// </summary>
58-
[JsonPropertyName("fw_id")]
5958
[State]
59+
[JsonPropertyName("fw_id")]
6060
public partial string? FirmwareIdentifier { get; init; }
6161

6262
/// <summary>
6363
/// Gets the version of the firmware.
6464
/// </summary>
65-
[JsonPropertyName("ver")]
6665
[State]
66+
[JsonPropertyName("ver")]
6767
public partial string? Version { get; init; }
6868

6969
/// <summary>
7070
/// Gets the application name.
7171
/// </summary>
72-
[JsonPropertyName("app")]
7372
[State]
73+
[JsonPropertyName("app")]
7474
public partial string? Application { get; init; }
7575

7676
/// <summary>
7777
/// Gets a value indicating whether authentication is enabled.
7878
/// </summary>
79-
[JsonPropertyName("auth_en")]
8079
[State]
80+
[JsonPropertyName("auth_en")]
8181
public partial bool? IsAuthenticationEnabled { get; init; }
8282

8383
/// <summary>
8484
/// Gets the authentication domain.
8585
/// </summary>
86-
[JsonPropertyName("auth_domain")]
8786
[State]
87+
[JsonPropertyName("auth_domain")]
8888
public partial string? AuthenticationDomain { get; init; }
8989

9090
/// <summary>
9191
/// Gets the profile of the device.
9292
/// </summary>
93-
[JsonPropertyName("profile")]
9493
[State]
94+
[JsonPropertyName("profile")]
9595
public partial string? Profile { get; init; }
9696

9797
/// <summary>

src/Namotion.Shelly/ShellySwitch.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,17 @@ public partial class ShellySwitch :
3636

3737
public DateTimeOffset? LastUpdated => Parent?.LastUpdated;
3838

39-
[JsonPropertyName("id"), State]
40-
public partial int? Index { get; set; }
39+
[State]
40+
[JsonPropertyName("id"), JsonInclude]
41+
public partial int? Index { get; internal set; }
4142

42-
[JsonPropertyName("output"), State]
43-
public partial bool? IsOn { get; set; }
43+
[State]
44+
[JsonPropertyName("output"), JsonInclude]
45+
public partial bool? IsOn { get; internal set; }
4446

45-
[JsonPropertyName("source"), State]
46-
public partial string? Source { get; set; }
47+
[State]
48+
[JsonPropertyName("source"), JsonInclude]
49+
public partial string? Source { get; internal set; }
4750

4851
public async Task TurnOnAsync(CancellationToken cancellationToken = default)
4952
{

0 commit comments

Comments
 (0)