Skip to content

Commit 1153e9e

Browse files
Limited initial list of entities, added possibility to right-click a menu item
1 parent 67f5c42 commit 1153e9e

12 files changed

Lines changed: 103 additions & 20 deletions

File tree

Home Assistant Taskbar Menu/Entities/Automation.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Windows.Controls;
4+
using System.Windows.Input;
45
using System.Windows.Threading;
56
using MaterialDesignThemes.Wpf;
67

@@ -34,6 +35,15 @@ protected override MenuItem ToMenuItem(Dispatcher dispatcher, string name)
3435
root.Icon = new PackIcon {Kind = PackIconKind.Tick};
3536
}
3637

38+
root.PreviewMouseDown += (sender, args) =>
39+
{
40+
if (args.ChangedButton == MouseButton.Right)
41+
{
42+
HaClientContext.CallService(dispatcher, this, "toggle");
43+
args.Handled = true;
44+
}
45+
};
46+
3747
new List<Tuple<string, string>>
3848
{
3949
Tuple.Create("turn_on", "Turn On"),

Home Assistant Taskbar Menu/Entities/Cover.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Windows.Controls;
3+
using System.Windows.Input;
34
using System.Windows.Threading;
45
using MaterialDesignThemes.Wpf;
56

@@ -54,6 +55,23 @@ protected override MenuItem ToMenuItem(Dispatcher dispatcher, string name)
5455
}
5556
else
5657
{
58+
root.PreviewMouseDown += (sender, args) =>
59+
{
60+
if (args.ChangedButton == MouseButton.Right)
61+
{
62+
if (IsSupported(SupportedFeatures.Open, SupportedFeatures.Close))
63+
{
64+
HaClientContext.CallService(dispatcher, this, "toggle");
65+
}
66+
else if (IsSupported(SupportedFeatures.OpenTilt, SupportedFeatures.CloseTilt))
67+
{
68+
HaClientContext.CallService(dispatcher, this, "toggle");
69+
}
70+
71+
args.Handled = true;
72+
}
73+
};
74+
5775
AddMenuItemIfSupported(dispatcher, root, SupportedFeatures.Open);
5876
AddMenuItemIfSupported(dispatcher, root, SupportedFeatures.Close);
5977
AddMenuItemIfSupported(dispatcher, root, SupportedFeatures.Stop);

Home Assistant Taskbar Menu/Entities/Entity.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ protected List<int> GetSupportedFeatures()
114114

115115
protected abstract MenuItem ToMenuItem(Dispatcher dispatcher, string name);
116116

117-
protected bool IsSupported(int supportedFeature)
117+
protected bool IsSupported(params int[] supportedFeatures)
118118
{
119-
return GetSupportedFeatures().Contains(supportedFeature);
119+
return supportedFeatures.ToList()
120+
.TrueForAll(supportedFeature => GetSupportedFeatures().Contains(supportedFeature));
120121
}
121122

122123
protected void AddMenuItemIfSupported(Dispatcher dispatcher, ItemsControl root, int supportedFeature)

Home Assistant Taskbar Menu/Entities/Fan.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Windows.Controls;
4+
using System.Windows.Input;
45
using System.Windows.Threading;
56
using MaterialDesignThemes.Wpf;
67

@@ -45,6 +46,15 @@ protected override MenuItem ToMenuItem(Dispatcher dispatcher, string name)
4546
}
4647
else
4748
{
49+
root.PreviewMouseDown += (sender, args) =>
50+
{
51+
if (args.ChangedButton == MouseButton.Right)
52+
{
53+
HaClientContext.CallService(dispatcher, this, "toggle");
54+
args.Handled = true;
55+
}
56+
};
57+
4858
root.Items.Add(CreateMenuItem(dispatcher, "turn_on", "Turn On"));
4959
root.Items.Add(CreateMenuItem(dispatcher, "turn_off", "Turn Off"));
5060
if (features.Contains(SupportedFeatures.SetSpeed))

Home Assistant Taskbar Menu/Entities/Light.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using System.Windows.Controls;
3+
using System.Windows.Input;
34
using System.Windows.Threading;
45
using MaterialDesignThemes.Wpf;
56

@@ -40,14 +41,23 @@ protected override MenuItem ToMenuItem(Dispatcher dispatcher, string name)
4041
};
4142
if (IsOn())
4243
{
43-
root.Icon = new PackIcon { Kind = PackIconKind.Tick };
44+
root.Icon = new PackIcon {Kind = PackIconKind.Tick};
4445
}
46+
4547
if (GetSupportedFeatures().Count == 0)
4648
{
47-
root.Click += (sender, args) => { HaClientContext.CallService(dispatcher, this, "toggle"); };
49+
root.Click += (sender, args) => HaClientContext.CallService(dispatcher, this, "toggle");
4850
}
4951
else
5052
{
53+
root.PreviewMouseDown += (sender, args) =>
54+
{
55+
if (args.ChangedButton == MouseButton.Right)
56+
{
57+
HaClientContext.CallService(dispatcher, this, "toggle");
58+
args.Handled = true;
59+
}
60+
};
5161
root.Items.Add(CreateMenuItem(dispatcher, "turn_on", "Turn On"));
5262
root.Items.Add(CreateMenuItem(dispatcher, "turn_off", "Turn Off"));
5363
AddSliderIfSupported(dispatcher, root, SupportedFeatures.Brightness, 0, 255,

Home Assistant Taskbar Menu/Entities/MediaPlayer.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Windows.Controls;
4+
using System.Windows.Input;
45
using System.Windows.Threading;
56
using MaterialDesignThemes.Wpf;
67

@@ -43,6 +44,14 @@ protected override MenuItem ToMenuItem(Dispatcher dispatcher, string name)
4344
{
4445
root.Icon = new PackIcon { Kind = PackIconKind.Tick };
4546
}
47+
root.PreviewMouseDown += (sender, args) =>
48+
{
49+
if (args.ChangedButton == MouseButton.Right)
50+
{
51+
HaClientContext.CallService(dispatcher, this, "toggle");
52+
args.Handled = true;
53+
}
54+
};
4655
AddMenuItemIfSupported(dispatcher, root, SupportedFeatures.TurnOn);
4756
AddMenuItemIfSupported(dispatcher, root, SupportedFeatures.TurnOff);
4857
AddMenuItemIfSupported(dispatcher, root, SupportedFeatures.Play);

Home Assistant Taskbar Menu/Entities/Vacuum.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Windows.Controls;
4+
using System.Windows.Input;
45
using System.Windows.Threading;
56
using MaterialDesignThemes.Wpf;
67

@@ -46,6 +47,15 @@ protected override MenuItem ToMenuItem(Dispatcher dispatcher, string name)
4647
}
4748
GetSupportedFeatures()
4849
.ForEach(supportedFeature => AddMenuItemIfSupported(dispatcher, root, supportedFeature));
50+
root.PreviewMouseDown += (sender, args) =>
51+
{
52+
if (args.ChangedButton == MouseButton.Right && IsSupported(SupportedFeatures.TurnOn, SupportedFeatures.TurnOff))
53+
{
54+
HaClientContext.CallService(dispatcher, this, "toggle");
55+
args.Handled = true;
56+
}
57+
};
58+
4959
if (IsSupported(SupportedFeatures.FanSpeed))
5060
{
5161
var fanSpeedItem = new MenuItem {Header = "Fan Speed", StaysOpenOnClick = true};
@@ -63,8 +73,8 @@ protected override MenuItem ToMenuItem(Dispatcher dispatcher, string name)
6373

6474
private static class SupportedFeatures
6575
{
66-
private const int TurnOn = 1;
67-
private const int TurnOff = 2;
76+
public const int TurnOn = 1;
77+
public const int TurnOff = 2;
6878
private const int Pause = 4;
6979
private const int Stop = 8;
7080
private const int ReturnHome = 16;

Home Assistant Taskbar Menu/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@
5050
// You can specify all the values or you can default the Build and Revision Numbers
5151
// by using the '*' as shown below:
5252
// [assembly: AssemblyVersion("1.0.*")]
53-
[assembly: AssemblyVersion("1.0.0.0")]
54-
[assembly: AssemblyFileVersion("1.0.0.0")]
53+
[assembly: AssemblyVersion("1.0.1.0")]
54+
[assembly: AssemblyFileVersion("1.0.1.0")]
5555
[assembly: NeutralResourcesLanguage("en")]

Home Assistant Taskbar Menu/Utils/Storage.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ public static void Save(ViewConfiguration viewConfiguration)
6363
{
6464
using (var streamWriter = new StreamWriter(ViewConfigPath))
6565
{
66-
streamWriter.Write(JsonConvert.SerializeObject(viewConfiguration));
66+
streamWriter.Write(JsonConvert.SerializeObject(viewConfiguration, Formatting.Indented, new JsonSerializerSettings()
67+
{
68+
NullValueHandling = NullValueHandling.Ignore
69+
}));
6770
}
6871
}
6972

Home Assistant Taskbar Menu/Utils/ViewConfiguration.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,20 @@ public class ViewConfiguration
1717

1818
public string EntityId { get; set; }
1919

20-
public List<ViewConfiguration> Children { get; set; } = new List<ViewConfiguration>();
20+
public List<ViewConfiguration> Children { get; set; }
2121

22-
public Dictionary<string, string> Properties = new Dictionary<string, string>();
22+
public Dictionary<string, string> Properties;
2323

2424
public bool ContainsEntity(Entity stateObject)
2525
{
26-
return stateObject.EntityId == EntityId || Children.Any(c => c.ContainsEntity(stateObject));
26+
return NodeType == Type.Entity && stateObject.EntityId == EntityId
27+
|| (NodeType == Type.Folder || NodeType == Type.Root)
28+
&& Children.Any(c => c.ContainsEntity(stateObject));
2729
}
2830

2931
public string GetProperty(string key)
3032
{
31-
return Properties.ContainsKey(key) ? Properties[key] : "";
33+
return Properties != null && Properties.ContainsKey(key) ? Properties[key] : "";
3234
}
3335

3436
public static ViewConfiguration Default()
@@ -68,7 +70,8 @@ public static ViewConfiguration Folder(string name)
6870
return new ViewConfiguration
6971
{
7072
NodeType = Type.Folder,
71-
Name = name
73+
Name = name,
74+
Children = new List<ViewConfiguration>()
7275
};
7376
}
7477

0 commit comments

Comments
 (0)