Before using any of the Home APIs for iOS, you must initialize the home in your app.
Home
is the top-level entry to the SDK and provides access to all entities in
the user's structure.
When requesting all entities of a particular type, the API returns a Query
object that lets you choose how to receive the results.
Create a Home object
Use connect()
to create
a Home
object. This is a basic step to access a user's home:
do {
self.home = try await Home.connect()
} catch {
Logger.error("Auth error: \(error).")
}
This call prompts the SDK to check authorization state. If unauthenticated, the SDK presents the OAuth flow for a user to grant permission to their home to the app.
Once you have access to a home, you can use the following APIs:
- Device & Structure APIs
- Commissioning API
Home configuration
When initialized, the Home
client reads from an app provided
HomeClientConfiguration
object to prepare its environment. This object should
be passed to the client prior to any Home API calls so that the permissions,
automations, and commissioning features are able to interact with one's smart
home project.
teamID
: The Apple Team ID of the app using SDK.clientID
: The OAuth Client ID of the app using SDK. See Set up OAuth for your iOS app for more information.sharedAppGroup
: The App Group used for shared storage with the Google Home Matter Commissioning SDK.strictOperationValidation
: Validates if a trait or command is supported.- If
true
, you will receive an exception message if your trait or command is unsupported. - If
false
, a command or attribute write will still be sent, but you will receive an error for the unsupported trait.
- If
referencedAutomationTypes
: The list of traits and device types you intend to use with the Automation API.
These options are set using the configure()
method of the Home
object as shown:
Home.configure {
// Authentication and Home API usage configurations
$0.teamID = "ABC123"
$0.clientID = "123-abc.apps.googleusercontent.com"
$0.strictOperationValidation = true
// Commissioning configurations
$0.sharedAppGroup = "group.com.company.commissioning"
// Automation configurations
$0.referencedAutomationTypes = ReferencedAutomationTypes(
deviceTypes: [
OnOffLightDeviceType.self,
TemperatureSensorDeviceType.self,
WindowCoveringDeviceType.self,
],
traits: [
Google.TimeTrait.self,
Matter.OnOffTrait.self,
Matter.TemperatureMeasurementTrait.self,
Matter.WindowCoveringTrait.self,
]
)
}
Permissions
Before using any of the Home APIs for iOS, the app must have permission to
access devices or structures. The connect()
process will prompt the SDK to
check authorization state, and if unauthorized, will present the OAuth flow for
granting permission.
Update permissions
To update permissions granted to the Home APIs, use
presentPermissionsUpdate()
:
self.home?.presentPermissionsUpdate()
If the active user is signed into Safari, the permissions view will be presented for that user. Otherwise, the user will be prompted to select an account or sign in.
OkGoogle permissions
The
okGoogle
command is a device-level command and can be used to automate any device in
the structure. However, a Home APIs app may not have access to every device.
The following table describes how permissions are enforced in such cases.
Automation | Trait | Permissions enforcement |
---|---|---|
At 10:00pm, broadcast "Bedtime" on bedroom speaker. |
AssistantBroadcastTrait
on device. |
Automation creation:
|
At 10:00pm, broadcast "Bedtime" on all devices |
AssistantBroadcastTrait
on structure. |
Automation creation:
|
At 10:00pm, "play some music" |
AssistantFulfillmentTrait.OkGoogleCommand
|
Automation creation:
|
Whenever someone says "play some music" |
VoiceStarterTrait.OkGoogleEvent
|
Automation creation:
|
Revoke access
To clear client side access, use
disconnect()
to disconnect the Home
object from the account:
await self.home?.disconnect()
self.home = nil
Users can revoke previously-granted access:
Through the Google MyAccounts page > Data & privacy > Third-party apps & services. This will revoke the OAuth token that was issued when initial consent was granted, and will revoke access to any instance of the app that the user was using across all surfaces (phones) and structures.
The user may be directed with a deep link to the Third-party apps & services sub-page using the following URL scheme:
https://myaccount.google.com/connections/link?project_number=Cloud project_number
Through the Google Home app (GHA) > Settings > Linked Apps page. Clicking on in the GHA will take you to the Settings page. From there, click the Linked Apps tile which takes you to a page that looks similar to the consent screen. From this page the user can remove access to the app. The user can use this same page to change which device types or specific sensitive devices are accessible to the app.