Skip to content

Commit

Permalink
Add display of sensor values within groups
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-codechimp committed May 20, 2021
1 parent 0951e8e commit 35c2684
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 16 deletions.
16 changes: 8 additions & 8 deletions HA Menu.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -621,15 +621,15 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 24;
CURRENT_PROJECT_VERSION = 25;
DEVELOPMENT_TEAM = VZ3Z8BPWPW;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "HA Menu/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 2.4.1;
MARKETING_VERSION = 2.5.0;
OTHER_SWIFT_FLAGS = "-D DEBUG";
PRODUCT_BUNDLE_IDENTIFIER = "org.codechimp.HA-Menu";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -645,15 +645,15 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 24;
CURRENT_PROJECT_VERSION = 25;
DEVELOPMENT_TEAM = VZ3Z8BPWPW;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "HA Menu/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 2.4.1;
MARKETING_VERSION = 2.5.0;
PRODUCT_BUNDLE_IDENTIFIER = "org.codechimp.HA-Menu";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand Down Expand Up @@ -750,15 +750,15 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 24;
CURRENT_PROJECT_VERSION = 25;
DEVELOPMENT_TEAM = VZ3Z8BPWPW;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "HA Menu Launcher/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 2.4.1;
MARKETING_VERSION = 2.5.0;
PRODUCT_BUNDLE_IDENTIFIER = "org.codechimp.HA-Menu-Launcher";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand All @@ -774,15 +774,15 @@
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 24;
CURRENT_PROJECT_VERSION = 25;
DEVELOPMENT_TEAM = VZ3Z8BPWPW;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "HA Menu Launcher/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 2.4.1;
MARKETING_VERSION = 2.5.0;
PRODUCT_BUNDLE_IDENTIFIER = "org.codechimp.HA-Menu-Launcher";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions HA Menu/Assets.xcassets/BulletImage.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"images" : [
{
"filename" : "Bullet@1x.png",
"idiom" : "universal",
"scale" : "1x"
},
{
"filename" : "Bullet@2x.png",
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
6 changes: 3 additions & 3 deletions HA Menu/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
"author" : "xcode",
"version" : 1
}
}
}
22 changes: 20 additions & 2 deletions HA Menu/MenuItemController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ final class MenuItemController: NSObject, NSMenuDelegate {
itemType = EntityTypes.sceneType
case "script":
itemType = EntityTypes.scriptType
case "sensor":
itemType = EntityTypes.sensorType

default:
itemType = nil
Expand All @@ -250,15 +252,20 @@ final class MenuItemController: NSObject, NSMenuDelegate {
switch result {
case .success(let entity):
var options = [String]()
var unitOfMeasurement = ""

if itemType == EntityTypes.inputSelectType {
options = entity.attributes.options
}

if itemType == EntityTypes.sensorType {
unitOfMeasurement = entity.attributes.unitOfMeasurement
}

// Do not add unavailable state entities
if (entity.state != "unavailable") {

let haEntity: HaEntity = HaEntity(entityId: entityId, friendlyName: (entity.attributes.friendlyName), state: (entity.state), options: options)
let haEntity: HaEntity = HaEntity(entityId: entityId, friendlyName: (entity.attributes.friendlyName), state: (entity.state), unitOfMeasurement: unitOfMeasurement, options: options)

entities.append(haEntity)
}
Expand Down Expand Up @@ -345,6 +352,8 @@ final class MenuItemController: NSObject, NSMenuDelegate {
else {
let menuItem = NSMenuItem()

menuItem.title = haEntity.friendlyName

if haEntity.domainType == EntityDomains.sceneDomain {
menuItem.action = #selector(self.turnOnEntity(_:))
menuItem.state = NSControl.StateValue.off
Expand All @@ -355,13 +364,18 @@ final class MenuItemController: NSObject, NSMenuDelegate {
menuItem.state = NSControl.StateValue.off
menuItem.offStateImage = NSImage(named: "PlayButtonImage")
}
else if haEntity.domainType == EntityDomains.sensorDomain {
menuItem.title = haEntity.friendlyName + ": " + haEntity.state + haEntity.unitOfMeasurement
menuItem.action = #selector(self.doNothing(_:))
menuItem.state = NSControl.StateValue.off
menuItem.offStateImage = NSImage(named: "BulletImage")
}
else {
menuItem.action = #selector(self.toggleEntityState(_:))
menuItem.state = ((haEntity.state == "on") ? NSControl.StateValue.on : NSControl.StateValue.off)
}

menuItem.target = self
menuItem.title = haEntity.friendlyName
menuItem.keyEquivalent = ""
menuItem.representedObject = haEntity
menuItem.tag = haEntity.type.rawValue // Tag defines what type of item it is
Expand Down Expand Up @@ -430,6 +444,10 @@ final class MenuItemController: NSObject, NSMenuDelegate {
}
}

@objc func doNothing(_ sender: NSMenuItem) {
// fake menu responder
}

@objc func toggleEntityState(_ sender: NSMenuItem) {
let haEntity: HaEntity = sender.representedObject as! HaEntity
haService.toggleEntityState(haEntity: haEntity)
Expand Down
7 changes: 7 additions & 0 deletions HA Menu/Models/HaEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum EntityTypes: Int, CaseIterable {
case groupType = 7
case sceneType = 8
case scriptType = 9
case sensorType = 10
case unknownType = 999
}

Expand All @@ -29,6 +30,7 @@ enum EntityDomains: String, CaseIterable {
case sceneDomain = "scene"
case scriptDomain = "script"
case groupDomain = "group"
case sensorDomain = "sensor"
case unknownDomain = "unknown"
}

Expand All @@ -37,6 +39,7 @@ struct HaEntity {
var entityId: String
var friendlyName: String
var state: String
var unitOfMeasurement: String
var options: [String]

var domainType: EntityDomains {
Expand All @@ -56,6 +59,8 @@ struct HaEntity {
return EntityDomains.sceneDomain
case EntityDomains.scriptDomain.rawValue:
return EntityDomains.scriptDomain
case EntityDomains.sensorDomain.rawValue:
return EntityDomains.sensorDomain

case EntityDomains.groupDomain.rawValue:
return EntityDomains.groupDomain
Expand Down Expand Up @@ -88,6 +93,8 @@ struct HaEntity {
return EntityTypes.sceneType
case EntityDomains.scriptDomain:
return EntityTypes.scriptType
case EntityDomains.sensorDomain:
return EntityTypes.sensorType

case EntityDomains.groupDomain:
return EntityTypes.groupType
Expand Down
2 changes: 1 addition & 1 deletion HA Menu/Models/HaService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class HaService {
// Do not add unavailable state entities
if (haState.state != "unavailable") {

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

entities.append(haEntity)
}
Expand Down
9 changes: 9 additions & 0 deletions HA Menu/Models/HaStateAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct HaStateAttributes : Decodable {

let entityIds: [String]
var friendlyName: String // Allow rewriting to entityId when decoded
let unitOfMeasurement: String
let options: [String]

enum CodingKeys: String, CodingKey {
Expand All @@ -21,12 +22,14 @@ struct HaStateAttributes : Decodable {
enum AttributeKeys: String, CodingKey {
case entityIds = "entity_id"
case friendlyName = "friendly_name"
case unitOfMeasurement = "unit_of_measurement"
case options = "options"
}

init() {
entityIds = [String]()
friendlyName = ""
unitOfMeasurement = ""
options = [String]()
}

Expand All @@ -45,6 +48,12 @@ struct HaStateAttributes : Decodable {
} else {
self.friendlyName = ""
}

if let unitOfMeasurement = try attributes.decodeIfPresent(String.self, forKey: .unitOfMeasurement) {
self.unitOfMeasurement = unitOfMeasurement
} else {
self.unitOfMeasurement = ""
}

if let options = try attributes.decodeIfPresent([String].self, forKey: .options) {
self.options = options
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ Currently HA Menu supports
* Turning available switches, lights, automations and input_boolean's on and off
* Activating scenes and scripts
* input_select option menus

* Viewing sensor values (Sensors have to be specifically added to a group)

HA Menu supports MacOS 10.13 (High Sierra) and later.

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

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).
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).

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.

Example groups.yaml
```yaml
Expand All @@ -61,6 +64,7 @@ ha_menu:
- input_boolean.notifications
- automation.entry_alert
- input_select.who_cooks
- sensor.outside_temperature
```
## Say Thanks
Expand Down

0 comments on commit 35c2684

Please sign in to comment.