From 45d24bffa35ad4c262cc81c29b204ed696916bcd Mon Sep 17 00:00:00 2001 From: Andrew Jackson Date: Mon, 30 May 2022 14:26:07 +0100 Subject: [PATCH] Added button and input_button --- HA Menu.xcodeproj/project.pbxproj | 16 ++++++++-------- HA Menu/AppDelegate.swift | 2 ++ HA Menu/MenuItemController.swift | 21 +++++++++++++++++++- HA Menu/Models/HaEntity.swift | 12 ++++++++++++ HA Menu/Models/HaService.swift | 17 ++++++++++++++++ HA Menu/Models/Preferences.swift | 32 +++++++++++++++++++++++++++++++ README.md | 2 +- 7 files changed, 92 insertions(+), 10 deletions(-) diff --git a/HA Menu.xcodeproj/project.pbxproj b/HA Menu.xcodeproj/project.pbxproj index a2e804b..1254c06 100644 --- a/HA Menu.xcodeproj/project.pbxproj +++ b/HA Menu.xcodeproj/project.pbxproj @@ -621,7 +621,7 @@ 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"; @@ -629,7 +629,7 @@ "$(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)"; @@ -645,7 +645,7 @@ 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"; @@ -653,7 +653,7 @@ "$(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; @@ -750,7 +750,7 @@ 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"; @@ -758,7 +758,7 @@ "$(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; @@ -774,7 +774,7 @@ 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"; @@ -782,7 +782,7 @@ "$(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; diff --git a/HA Menu/AppDelegate.swift b/HA Menu/AppDelegate.swift index 819a1d7..17a4f9f 100644 --- a/HA Menu/AppDelegate.swift +++ b/HA Menu/AppDelegate.swift @@ -56,6 +56,8 @@ extension AppDelegate { "domain_scenes": true, "domain_scripts": true, "domain_covers": true, + "domain_buttons": true, + "domain_inputbuttons": true, "betaNotifications": false ]) diff --git a/HA Menu/MenuItemController.swift b/HA Menu/MenuItemController.swift index d0b37d8..61d9177 100644 --- a/HA Menu/MenuItemController.swift +++ b/HA Menu/MenuItemController.swift @@ -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 @@ -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(_:)) @@ -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) diff --git a/HA Menu/Models/HaEntity.swift b/HA Menu/Models/HaEntity.swift index 170a60a..b53491a 100644 --- a/HA Menu/Models/HaEntity.swift +++ b/HA Menu/Models/HaEntity.swift @@ -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 } @@ -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" } @@ -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 @@ -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 diff --git a/HA Menu/Models/HaService.swift b/HA Menu/Models/HaService.swift index 6e79dfb..402fdfc 100644 --- a/HA Menu/Models/HaService.swift +++ b/HA Menu/Models/HaService.swift @@ -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() + } } diff --git a/HA Menu/Models/Preferences.swift b/HA Menu/Models/Preferences.swift index b9f9252..611f309 100644 --- a/HA Menu/Models/Preferences.swift +++ b/HA Menu/Models/Preferences.swift @@ -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 { @@ -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 { @@ -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: "")) diff --git a/README.md b/README.md index 87d0420..3ae0f8b 100644 --- a/README.md +++ b/README.md @@ -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)