Skip to content

Commit 35c2684

Browse files
Add display of sensor values within groups
1 parent 0951e8e commit 35c2684

File tree

10 files changed

+76
-16
lines changed

10 files changed

+76
-16
lines changed

HA Menu.xcodeproj/project.pbxproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -621,15 +621,15 @@
621621
CODE_SIGN_IDENTITY = "Apple Development";
622622
CODE_SIGN_STYLE = Automatic;
623623
COMBINE_HIDPI_IMAGES = YES;
624-
CURRENT_PROJECT_VERSION = 24;
624+
CURRENT_PROJECT_VERSION = 25;
625625
DEVELOPMENT_TEAM = VZ3Z8BPWPW;
626626
ENABLE_HARDENED_RUNTIME = YES;
627627
INFOPLIST_FILE = "HA Menu/Info.plist";
628628
LD_RUNPATH_SEARCH_PATHS = (
629629
"$(inherited)",
630630
"@executable_path/../Frameworks",
631631
);
632-
MARKETING_VERSION = 2.4.1;
632+
MARKETING_VERSION = 2.5.0;
633633
OTHER_SWIFT_FLAGS = "-D DEBUG";
634634
PRODUCT_BUNDLE_IDENTIFIER = "org.codechimp.HA-Menu";
635635
PRODUCT_NAME = "$(TARGET_NAME)";
@@ -645,15 +645,15 @@
645645
CODE_SIGN_IDENTITY = "Apple Development";
646646
CODE_SIGN_STYLE = Automatic;
647647
COMBINE_HIDPI_IMAGES = YES;
648-
CURRENT_PROJECT_VERSION = 24;
648+
CURRENT_PROJECT_VERSION = 25;
649649
DEVELOPMENT_TEAM = VZ3Z8BPWPW;
650650
ENABLE_HARDENED_RUNTIME = YES;
651651
INFOPLIST_FILE = "HA Menu/Info.plist";
652652
LD_RUNPATH_SEARCH_PATHS = (
653653
"$(inherited)",
654654
"@executable_path/../Frameworks",
655655
);
656-
MARKETING_VERSION = 2.4.1;
656+
MARKETING_VERSION = 2.5.0;
657657
PRODUCT_BUNDLE_IDENTIFIER = "org.codechimp.HA-Menu";
658658
PRODUCT_NAME = "$(TARGET_NAME)";
659659
SWIFT_VERSION = 5.0;
@@ -750,15 +750,15 @@
750750
CODE_SIGN_IDENTITY = "Apple Development";
751751
CODE_SIGN_STYLE = Automatic;
752752
COMBINE_HIDPI_IMAGES = YES;
753-
CURRENT_PROJECT_VERSION = 24;
753+
CURRENT_PROJECT_VERSION = 25;
754754
DEVELOPMENT_TEAM = VZ3Z8BPWPW;
755755
ENABLE_HARDENED_RUNTIME = YES;
756756
INFOPLIST_FILE = "HA Menu Launcher/Info.plist";
757757
LD_RUNPATH_SEARCH_PATHS = (
758758
"$(inherited)",
759759
"@executable_path/../Frameworks",
760760
);
761-
MARKETING_VERSION = 2.4.1;
761+
MARKETING_VERSION = 2.5.0;
762762
PRODUCT_BUNDLE_IDENTIFIER = "org.codechimp.HA-Menu-Launcher";
763763
PRODUCT_NAME = "$(TARGET_NAME)";
764764
SKIP_INSTALL = YES;
@@ -774,15 +774,15 @@
774774
CODE_SIGN_IDENTITY = "Apple Development";
775775
CODE_SIGN_STYLE = Automatic;
776776
COMBINE_HIDPI_IMAGES = YES;
777-
CURRENT_PROJECT_VERSION = 24;
777+
CURRENT_PROJECT_VERSION = 25;
778778
DEVELOPMENT_TEAM = VZ3Z8BPWPW;
779779
ENABLE_HARDENED_RUNTIME = YES;
780780
INFOPLIST_FILE = "HA Menu Launcher/Info.plist";
781781
LD_RUNPATH_SEARCH_PATHS = (
782782
"$(inherited)",
783783
"@executable_path/../Frameworks",
784784
);
785-
MARKETING_VERSION = 2.4.1;
785+
MARKETING_VERSION = 2.5.0;
786786
PRODUCT_BUNDLE_IDENTIFIER = "org.codechimp.HA-Menu-Launcher";
787787
PRODUCT_NAME = "$(TARGET_NAME)";
788788
SKIP_INSTALL = YES;
241 Bytes
Loading
342 Bytes
Loading
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "[email protected]",
5+
"idiom" : "universal",
6+
"scale" : "1x"
7+
},
8+
{
9+
"filename" : "[email protected]",
10+
"idiom" : "universal",
11+
"scale" : "2x"
12+
},
13+
{
14+
"idiom" : "universal",
15+
"scale" : "3x"
16+
}
17+
],
18+
"info" : {
19+
"author" : "xcode",
20+
"version" : 1
21+
}
22+
}

HA Menu/Assets.xcassets/Contents.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"info" : {
3-
"version" : 1,
4-
"author" : "xcode"
3+
"author" : "xcode",
4+
"version" : 1
55
}
6-
}
6+
}

HA Menu/MenuItemController.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ final class MenuItemController: NSObject, NSMenuDelegate {
239239
itemType = EntityTypes.sceneType
240240
case "script":
241241
itemType = EntityTypes.scriptType
242+
case "sensor":
243+
itemType = EntityTypes.sensorType
242244

243245
default:
244246
itemType = nil
@@ -250,15 +252,20 @@ final class MenuItemController: NSObject, NSMenuDelegate {
250252
switch result {
251253
case .success(let entity):
252254
var options = [String]()
255+
var unitOfMeasurement = ""
253256

254257
if itemType == EntityTypes.inputSelectType {
255258
options = entity.attributes.options
256259
}
257260

261+
if itemType == EntityTypes.sensorType {
262+
unitOfMeasurement = entity.attributes.unitOfMeasurement
263+
}
264+
258265
// Do not add unavailable state entities
259266
if (entity.state != "unavailable") {
260267

261-
let haEntity: HaEntity = HaEntity(entityId: entityId, friendlyName: (entity.attributes.friendlyName), state: (entity.state), options: options)
268+
let haEntity: HaEntity = HaEntity(entityId: entityId, friendlyName: (entity.attributes.friendlyName), state: (entity.state), unitOfMeasurement: unitOfMeasurement, options: options)
262269

263270
entities.append(haEntity)
264271
}
@@ -345,6 +352,8 @@ final class MenuItemController: NSObject, NSMenuDelegate {
345352
else {
346353
let menuItem = NSMenuItem()
347354

355+
menuItem.title = haEntity.friendlyName
356+
348357
if haEntity.domainType == EntityDomains.sceneDomain {
349358
menuItem.action = #selector(self.turnOnEntity(_:))
350359
menuItem.state = NSControl.StateValue.off
@@ -355,13 +364,18 @@ final class MenuItemController: NSObject, NSMenuDelegate {
355364
menuItem.state = NSControl.StateValue.off
356365
menuItem.offStateImage = NSImage(named: "PlayButtonImage")
357366
}
367+
else if haEntity.domainType == EntityDomains.sensorDomain {
368+
menuItem.title = haEntity.friendlyName + ": " + haEntity.state + haEntity.unitOfMeasurement
369+
menuItem.action = #selector(self.doNothing(_:))
370+
menuItem.state = NSControl.StateValue.off
371+
menuItem.offStateImage = NSImage(named: "BulletImage")
372+
}
358373
else {
359374
menuItem.action = #selector(self.toggleEntityState(_:))
360375
menuItem.state = ((haEntity.state == "on") ? NSControl.StateValue.on : NSControl.StateValue.off)
361376
}
362377

363378
menuItem.target = self
364-
menuItem.title = haEntity.friendlyName
365379
menuItem.keyEquivalent = ""
366380
menuItem.representedObject = haEntity
367381
menuItem.tag = haEntity.type.rawValue // Tag defines what type of item it is
@@ -430,6 +444,10 @@ final class MenuItemController: NSObject, NSMenuDelegate {
430444
}
431445
}
432446

447+
@objc func doNothing(_ sender: NSMenuItem) {
448+
// fake menu responder
449+
}
450+
433451
@objc func toggleEntityState(_ sender: NSMenuItem) {
434452
let haEntity: HaEntity = sender.representedObject as! HaEntity
435453
haService.toggleEntityState(haEntity: haEntity)

HA Menu/Models/HaEntity.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ enum EntityTypes: Int, CaseIterable {
1717
case groupType = 7
1818
case sceneType = 8
1919
case scriptType = 9
20+
case sensorType = 10
2021
case unknownType = 999
2122
}
2223

@@ -29,6 +30,7 @@ enum EntityDomains: String, CaseIterable {
2930
case sceneDomain = "scene"
3031
case scriptDomain = "script"
3132
case groupDomain = "group"
33+
case sensorDomain = "sensor"
3234
case unknownDomain = "unknown"
3335
}
3436

@@ -37,6 +39,7 @@ struct HaEntity {
3739
var entityId: String
3840
var friendlyName: String
3941
var state: String
42+
var unitOfMeasurement: String
4043
var options: [String]
4144

4245
var domainType: EntityDomains {
@@ -56,6 +59,8 @@ struct HaEntity {
5659
return EntityDomains.sceneDomain
5760
case EntityDomains.scriptDomain.rawValue:
5861
return EntityDomains.scriptDomain
62+
case EntityDomains.sensorDomain.rawValue:
63+
return EntityDomains.sensorDomain
5964

6065
case EntityDomains.groupDomain.rawValue:
6166
return EntityDomains.groupDomain
@@ -88,6 +93,8 @@ struct HaEntity {
8893
return EntityTypes.sceneType
8994
case EntityDomains.scriptDomain:
9095
return EntityTypes.scriptType
96+
case EntityDomains.sensorDomain:
97+
return EntityTypes.sensorType
9198

9299
case EntityDomains.groupDomain:
93100
return EntityTypes.groupType

HA Menu/Models/HaService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class HaService {
106106
// Do not add unavailable state entities
107107
if (haState.state != "unavailable") {
108108

109-
let haEntity: HaEntity = HaEntity(entityId: haState.entityId, friendlyName: (haState.attributes.friendlyName), state: (haState.state), options: haState.attributes.options)
109+
let haEntity: HaEntity = HaEntity(entityId: haState.entityId, friendlyName: (haState.attributes.friendlyName), state: (haState.state), unitOfMeasurement: haState.attributes.unitOfMeasurement, options: haState.attributes.options)
110110

111111
entities.append(haEntity)
112112
}

HA Menu/Models/HaStateAttributes.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct HaStateAttributes : Decodable {
1212

1313
let entityIds: [String]
1414
var friendlyName: String // Allow rewriting to entityId when decoded
15+
let unitOfMeasurement: String
1516
let options: [String]
1617

1718
enum CodingKeys: String, CodingKey {
@@ -21,12 +22,14 @@ struct HaStateAttributes : Decodable {
2122
enum AttributeKeys: String, CodingKey {
2223
case entityIds = "entity_id"
2324
case friendlyName = "friendly_name"
25+
case unitOfMeasurement = "unit_of_measurement"
2426
case options = "options"
2527
}
2628

2729
init() {
2830
entityIds = [String]()
2931
friendlyName = ""
32+
unitOfMeasurement = ""
3033
options = [String]()
3134
}
3235

@@ -45,6 +48,12 @@ struct HaStateAttributes : Decodable {
4548
} else {
4649
self.friendlyName = ""
4750
}
51+
52+
if let unitOfMeasurement = try attributes.decodeIfPresent(String.self, forKey: .unitOfMeasurement) {
53+
self.unitOfMeasurement = unitOfMeasurement
54+
} else {
55+
self.unitOfMeasurement = ""
56+
}
4857

4958
if let options = try attributes.decodeIfPresent([String].self, forKey: .options) {
5059
self.options = options

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ Currently HA Menu supports
66
* Turning available switches, lights, automations and input_boolean's on and off
77
* Activating scenes and scripts
88
* input_select option menus
9-
9+
* Viewing sensor values (Sensors have to be specifically added to a group)
10+
1011
HA Menu supports MacOS 10.13 (High Sierra) and later.
1112

1213
![alt text](Art/menu.png "HA Menu")
@@ -48,7 +49,9 @@ First of all create your group(s) within groups.yaml as per the example. Note t
4849
Once you have the group(s) added to HA, within HA Menu go to Preferences and tick the Groups you want to be displayed.
4950
Close preferences to save these settings.
5051

51-
Now when you click on HA Menu again the group's you have setup will be displayed. Entities within groups are displayed in the order they are added within the group (printer, lego_lights, desk_lamp, notifications, entry_alert, who_cooks in the example).
52+
Now when you click on HA Menu again the group's you have setup will be displayed. Entities within groups are displayed in the order they are added within the group (printer, lego_lights, desk_lamp, notifications, entry_alert, who_cooks, outside_temperature in the example).
53+
54+
Where sensors are included they will be displayed with a bullet in the menu, and show the friendly name, state/value and unit of measurement. Clicking on a sensor does not perform any action.
5255

5356
Example groups.yaml
5457
```yaml
@@ -61,6 +64,7 @@ ha_menu:
6164
- input_boolean.notifications
6265
- automation.entry_alert
6366
- input_select.who_cooks
67+
- sensor.outside_temperature
6468
```
6569
6670
## Say Thanks

0 commit comments

Comments
 (0)