Skip to content

Commit

Permalink
Added button and input_button
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-codechimp committed May 30, 2022
1 parent 957a512 commit 45d24bf
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 10 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 = 29;
CURRENT_PROJECT_VERSION = 30;
DEVELOPMENT_TEAM = VZ3Z8BPWPW;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "HA Menu/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 2.6.2;
MARKETING_VERSION = 2.7.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 = 29;
CURRENT_PROJECT_VERSION = 30;
DEVELOPMENT_TEAM = VZ3Z8BPWPW;
ENABLE_HARDENED_RUNTIME = YES;
INFOPLIST_FILE = "HA Menu/Info.plist";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 2.6.2;
MARKETING_VERSION = 2.7.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 = 29;
CURRENT_PROJECT_VERSION = 30;
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.6.2;
MARKETING_VERSION = 2.7.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 = 29;
CURRENT_PROJECT_VERSION = 30;
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.6.2;
MARKETING_VERSION = 2.7.0;
PRODUCT_BUNDLE_IDENTIFIER = "org.codechimp.HA-Menu-Launcher";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
Expand Down
2 changes: 2 additions & 0 deletions HA Menu/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ extension AppDelegate {
"domain_scenes": true,
"domain_scripts": true,
"domain_covers": true,
"domain_buttons": true,
"domain_inputbuttons": true,
"betaNotifications": false
])

Expand Down
21 changes: 20 additions & 1 deletion HA Menu/MenuItemController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ final class MenuItemController: NSObject, NSMenuDelegate {
itemType = EntityTypes.sensorType
case "cover":
itemType = EntityTypes.coverType
case "button":
itemType = EntityTypes.buttonType
case "input_button":
itemType = EntityTypes.inputButtonType

default:
itemType = nil
Expand Down Expand Up @@ -366,6 +370,16 @@ final class MenuItemController: NSObject, NSMenuDelegate {
menuItem.state = NSControl.StateValue.off
menuItem.offStateImage = NSImage(named: "PlayButtonImage")
}
else if haEntity.domainType == EntityDomains.buttonDomain {
menuItem.action = #selector(self.pressEntity(_:))
menuItem.state = NSControl.StateValue.off
menuItem.offStateImage = NSImage(named: "PlayButtonImage")
}
else if haEntity.domainType == EntityDomains.inputButtonDomain {
menuItem.action = #selector(self.pressEntity(_:))
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(_:))
Expand Down Expand Up @@ -468,7 +482,12 @@ final class MenuItemController: NSObject, NSMenuDelegate {
let haEntity: HaEntity = sender.representedObject as! HaEntity
haService.turnOnEntity(haEntity: haEntity)
}


@objc func pressEntity(_ sender: NSMenuItem) {
let haEntity: HaEntity = sender.representedObject as! HaEntity
haService.pressEntity(haEntity: haEntity)
}

func checkForUpdate() {
let parser = FeedParser(URL: releasesFeedURL)

Expand Down
12 changes: 12 additions & 0 deletions HA Menu/Models/HaEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ enum EntityTypes: Int, CaseIterable {
case scriptType = 9
case sensorType = 10
case coverType = 11
case buttonType = 12
case inputButtonType = 13
case unknownType = 999
}

Expand All @@ -33,6 +35,8 @@ enum EntityDomains: String, CaseIterable {
case groupDomain = "group"
case sensorDomain = "sensor"
case coverDomain = "cover"
case buttonDomain = "button"
case inputButtonDomain = "input_button"
case unknownDomain = "unknown"
}

Expand Down Expand Up @@ -65,6 +69,10 @@ struct HaEntity {
return EntityDomains.sensorDomain
case EntityDomains.coverDomain.rawValue:
return EntityDomains.coverDomain
case EntityDomains.buttonDomain.rawValue:
return EntityDomains.buttonDomain
case EntityDomains.inputButtonDomain.rawValue:
return EntityDomains.inputButtonDomain

case EntityDomains.groupDomain.rawValue:
return EntityDomains.groupDomain
Expand Down Expand Up @@ -101,6 +109,10 @@ struct HaEntity {
return EntityTypes.sensorType
case EntityDomains.coverDomain:
return EntityTypes.coverType
case EntityDomains.buttonDomain:
return EntityTypes.buttonType
case EntityDomains.inputButtonDomain:
return EntityTypes.inputButtonType

case EntityDomains.groupDomain:
return EntityTypes.groupType
Expand Down
17 changes: 17 additions & 0 deletions HA Menu/Models/HaService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,23 @@ class HaService {

task.resume()
}

func pressEntity(haEntity: HaEntity) {
let params = ["entity_id": haEntity.entityId]
let urlString = "\(prefs.server)/api/services/\(haEntity.domain)/press"

var request = createAuthURLRequest(url: URL(string: urlString)!)

request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject: params, options: [])

let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { data, response, error -> Void in
print(String(data: data!, encoding: String.Encoding.utf8)!)
})

task.resume()
}


}
32 changes: 32 additions & 0 deletions HA Menu/Models/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,26 @@ struct Preferences {
UserDefaults.standard.synchronize()
}
}

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

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

var menuItems: [PrefMenuItem] {
get {
Expand Down Expand Up @@ -211,6 +231,14 @@ struct Preferences {
decodedResponse.append(PrefMenuItem(entityId: "cover", itemType: itemTypes.Domain, subMenu: true, enabled: domainCovers, friendlyName: "Covers"))
}

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

if !domainExists(domain: "input_button", prefs: decodedResponse) {
decodedResponse.append(PrefMenuItem(entityId: "input_button", itemType: itemTypes.Domain, subMenu: true, enabled: domainButtons, friendlyName: "Input Buttons"))
}

return decodedResponse
}
catch {
Expand All @@ -237,6 +265,10 @@ struct Preferences {

decodedResponse.append(PrefMenuItem(entityId: "cover", itemType: itemTypes.Domain, subMenu: true, enabled: domainCovers, friendlyName: "Covers"))

decodedResponse.append(PrefMenuItem(entityId: "button", itemType: itemTypes.Domain, subMenu: true, enabled: domainButtons, friendlyName: "Buttons"))

decodedResponse.append(PrefMenuItem(entityId: "input_button", itemType: itemTypes.Domain, subMenu: true, enabled: domainInputButtons, friendlyName: "Input Buttons"))

// Init Groups from old setting
for group in groups {
decodedResponse.append(PrefMenuItem(entityId: group, itemType: itemTypes.Group, subMenu: false, enabled: true, friendlyName: ""))
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 and scripts
* Activating scenes, scripts, buttons and input_buttons
* input_select option menus
* Opening and Closing Covers
* Viewing sensor values (Sensors have to be specifically added to a group)
Expand Down

0 comments on commit 45d24bf

Please sign in to comment.