Skip to content

Commit

Permalink
Add scripts menu items
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-codechimp committed Jul 19, 2020
1 parent f48c817 commit ca3e891
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions HA Menu/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ extension AppDelegate {
"domain_inputbooleans": true,
"domain_inputselects": true,
"domain_scenes": true,
"domain_scripts": true,
"betaNotifications": false
])

Expand Down
8 changes: 8 additions & 0 deletions HA Menu/MenuItemController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ final class MenuItemController: NSObject, NSMenuDelegate {
itemType = EntityTypes.automationType
case "scene":
itemType = EntityTypes.sceneType
case "script":
itemType = EntityTypes.scriptType

default:
itemType = nil
}
Expand Down Expand Up @@ -305,6 +308,11 @@ final class MenuItemController: NSObject, NSMenuDelegate {
menuItem.state = NSControl.StateValue.off
menuItem.offStateImage = NSImage(named: "PlayButtonImage")
}
else if haEntity.domainType == EntityDomains.scriptDomain {
menuItem.action = #selector(self.turnOnEntity(_:))
menuItem.state = NSControl.StateValue.off
menuItem.offStateImage = NSImage(named: "PlayButtonImage")
}
else {
menuItem.action = #selector(self.toggleEntityState(_:))
menuItem.state = ((haEntity.state == "on") ? NSControl.StateValue.on : NSControl.StateValue.off)
Expand Down
8 changes: 8 additions & 0 deletions HA Menu/Models/HaEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum EntityTypes: Int, CaseIterable {
case inputSelectType = 6
case groupType = 7
case sceneType = 8
case scriptType = 9
case unknownType = 999
}

Expand All @@ -26,6 +27,7 @@ enum EntityDomains: String, CaseIterable {
case automationDomain = "automation"
case inputSelectDomain = "input_select"
case sceneDomain = "scene"
case scriptDomain = "script"
case groupDomain = "group"
case unknownDomain = "unknown"
}
Expand All @@ -52,6 +54,9 @@ struct HaEntity {
return EntityDomains.inputSelectDomain
case EntityDomains.sceneDomain.rawValue:
return EntityDomains.sceneDomain
case EntityDomains.scriptDomain.rawValue:
return EntityDomains.scriptDomain

case EntityDomains.groupDomain.rawValue:
return EntityDomains.groupDomain
default:
Expand Down Expand Up @@ -81,6 +86,9 @@ struct HaEntity {
return EntityTypes.inputSelectType
case EntityDomains.sceneDomain:
return EntityTypes.sceneType
case EntityDomains.scriptDomain:
return EntityTypes.scriptType

case EntityDomains.groupDomain:
return EntityTypes.groupType
default:
Expand Down
15 changes: 15 additions & 0 deletions HA Menu/Models/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ struct Preferences {
}
}

var domainScripts: Bool {
get {
return UserDefaults.standard.bool(forKey: "domain_scripts")
}
set {
UserDefaults.standard.set(newValue, forKey: "domain_scripts")
UserDefaults.standard.synchronize()
}
}

var menuItems: [PrefMenuItem] {
get {
var decodedResponse = [PrefMenuItem]()
Expand All @@ -183,6 +193,10 @@ struct Preferences {
decodedResponse.append(PrefMenuItem(entityId: "scene", itemType: itemTypes.Domain, subMenu: true, enabled: domainScenes, friendlyName: "Scenes"))
}

if !domainExists(domain: "script", prefs: decodedResponse) {
decodedResponse.append(PrefMenuItem(entityId: "script", itemType: itemTypes.Domain, subMenu: true, enabled: domainScenes, friendlyName: "Scripts"))
}

return decodedResponse
}
catch {
Expand All @@ -205,6 +219,7 @@ struct Preferences {

decodedResponse.append(PrefMenuItem(entityId: "scene", itemType: itemTypes.Domain, subMenu: true, enabled: domainScenes, friendlyName: "Scenes"))

decodedResponse.append(PrefMenuItem(entityId: "script", itemType: itemTypes.Domain, subMenu: true, enabled: domainScripts, friendlyName: "Scripts"))

// Init Groups from old setting
for group in groups {
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
A Mac OS Menu Bar app to perform common Home Assistant functions
Currently HA Menu supports
* Turning available switches, lights, automations and input_boolean's on and off
* Activating scenes
* Activating scenes and scripts
* input_select option menus

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

0 comments on commit ca3e891

Please sign in to comment.