Before using any of the Home APIs for Android, you must initialize the home in
your app. In this step, you'll create a [**singleton** instance of
`Home`](https://developers.home.google.com/apis/android/initialize#create_a_home_instance) for the local context.

**Only one instance of `Home` should be active at a time.**

This is the entry point to the Home APIs and also involves declaring which
traits and device types you intend to use with the Device \& Structure and
Automation APIs. If you're just starting out with the Google Home ecosystem and
are not sure what traits or device types to register, we've suggested some of
the most common here in this guide.

## Create a Home instance

To begin, import these packages into your app:  

    import android.content.Context
    import com.google.home.FactoryRegistry
    import com.google.home.HomeConfig
    import com.google.home.Home

To initialize the Home APIs:

1. Get a reference to the
   [`Application`](https://developer.android.com/reference/android/app/Application)
   context. This context doesn't depend on any activity lifecycle, and will
   live as long as your app is alive. You can obtain it by calling
   `getApplicationContext()` within an `Activity` or `Service`:

       val context = getApplicationContext()

2. Create a [`FactoryRegistry`](https://developers.home.google.com/reference/kotlin/com/google/home/FactoryRegistry)
   instance with all the traits and device types you intend to use in your app.

   For this guide, we've suggested some common ones (Light, Plug, Sensor,
   Switch, and Thermostat device types, presence and Assistant traits for
   automations), in case you're not sure what you need. To learn more, see
   [Registration of traits and device types](https://developers.home.google.com/apis/android/initialize#register).  

       val registry = FactoryRegistry(
         traits = listOf(
                   AirQuality,
                   AreaAttendanceState,
                   AreaPresenceState,
                   AssistantBroadcast,
                   AssistantFulfillment,
                   BooleanState,
                   ColorControl,
                   ExtendedColorControl,
                   FlowMeasurement,
                   IlluminanceMeasurement,
                   LevelControl,
                   Notification,
                   OccupancySensing,
                   OnOff,
                   RelativeHumidityMeasurement,
                   Switch,
                   TemperatureMeasurement,
                   Thermostat),
         types = listOf(
                   AirQualitySensorDevice,
                   ColorDimmerSwitchDevice,
                   ColorTemperatureLightDevice,
                   ContactSensorDevice,
                   DimmableLightDevice,
                   DimmablePlugInUnitDevice,
                   DimmerSwitchDevice,
                   ExtendedColorLightDevice,
                   FlowSensorDevice,
                   GenericSwitchDevice,
                   HumiditySensorDevice,
                   LightSensorDevice,
                   OccupancySensorDevice,
                   OnOffLightDevice,
                   OnOffLightSwitchDevice,
                   OnOffPluginUnitDevice,
                   OnOffSensorDevice,
                   SpeakerDevice,
                   TemperatureSensorDevice,
                   ThermostatDevice))

   Import statements for each individual trait and device type registered here
   are required (Android Studio should prompt you to add these).
3. Instantiate a [`HomeConfig`](https://developers.home.google.com/reference/kotlin/com/google/home/HomeConfig)
   using the
   [`Dispatchers.IO`](https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/#-978510185%2FProperties%2F-1316292404)
   coroutine context and your registry instance.

       val homeConfig = HomeConfig(
               coroutineContext = Dispatchers.IO,
               factoryRegistry = registry)

4. Finally, create the **singleton** instance of
   [`Home`](https://developers.home.google.com/reference/kotlin/com/google/home/Home), which is the entry point to
   the APIs, using the context and the `HomeConfig`.

       val homeManager: HomeClient = Home.getClient(context, homeConfig)

To avoid errors with invalid sessions, **it is important that only a *singleton*
instance of `Home` is created** , by wrapping it in an [object
declaration](https://kotlinlang.org/docs/object-declarations.html).

For an example, the [Sample App](https://developers.home.google.com/apis/sample-app/build) does it this way:  

    internal object HomeClientModule {
      @Provides
      @Singleton
      fun provideHomeClient(@ApplicationContext context: Context): HomeClient {
        return Home.getClient(
          context,
          HomeConfig(
            coroutineContext = IODispatcherModule.provideIoDispatcher(),
            factoryRegistry = registry,
          ),
        )
      }
    }

### Registration of traits and device types

The `FactoryRegistry` class helps developers optimize their app binary size by
letting them explicitly indicate which traits and device types are used by their
app.

Note that permissions and the factory registry are decoupled. Therefore,
unregistered traits and types that are available to your app using permissions
but not included in the factory registry are inaccessible using the
[Automation API](https://developers.home.google.com/apis/android/automation) nor are they returned in the bulk `traits()`
or `types()` method calls.