---
title: "#298: Changing Light Colors with HomeKit 💡🌈"
url: https://littlebitesofcocoa.com/298-changing-light-colors-with-homekit
author: Jake Marsh
published: 2017-02-17
topics: ["HomeKit"]
description: "In Bite #297, we learned about about how to simulate a light bulb accessory using Apple's HomeKit Accessory Simulator."
---

# #298: Changing Light Colors with HomeKit 💡🌈

By Jake Marsh, February 17, 2017. HTML: https://littlebitesofcocoa.com/298-changing-light-colors-with-homekit

In [Bite #297](https://littlebitesofcocoa.com/297-simulating-homekit-accessories), we learned about about how to simulate a light bulb accessory using [Apple's HomeKit Accessory Simulator](https://littlebitesofcocoa.com/297-simulating-homekit-accessories). Today we'll continue where we left off, and learn how to change the <span style="color: blue; font-weight: bold;">color</span> of our light bulb.

Let's dive in.

Everything in _HomeKit_ is built around the `HMHomeManager` type. We'll use this **manager** to interact with homes, accessories, and more:

``` swift
let manager = HMHomeManager()
manager.delegate = self
```

We've set ourselves as the **delegate** here so we can implement one **function**:

``` swift
extension ViewController : HMHomeManagerDelegate {
  func homeManagerDidUpdateHomes(_ manager: HMHomeManager) {
    // TODO
  }
}
```

Like all user data, we'll need to ask for permission before we can access the user's _HomeKit_ data. Once the user has allowed us, the `homeManagerDidUpdateHomes` **function** will be called. This **function** will also be called anytime signficant changes happen in a **home**.

We can use this as a cue to update our app's UI to match. Neat.

Ok, let's have some fun and see what it would look like (in code) to change <a href="https://www.youtube.com/watch?v=HAfFfqiYLp0" style="text-decoration: none;">all of the lights</a> in our house to a different color.

First let's get all the **lights** in the **home**:

``` swift
func homeManagerDidUpdateHomes(_ manager: HMHomeManager) {
  guard let home = manager.homes.first else { return }

  let lights = home.accessories.filter { $0.category.categoryType == HMAccessoryCategoryTypeLightbulb }
}
```

Nice. We now have an *array* of all **lights** in our **home** as `HMAccessory` objects. To make changes, we'll need to get to the **services** and **characteristics** <i>inside</i> of each **accessory**.

Let's `flatMap` our way to success and get an **array** of just the <i>"Hue"</i> characteristics.

**Note:** We're just going to try this out by tinting our light <span style="color: blue; font-weight: bold;">blue</span>. In a complete app, we'd need to make sure to update all 3 <i>Hue</i>, <i>Brightness</i>, and <i>Saturation</i> characteristics of the light.

``` swift
let hueCharacteristics = lights
  .flatMap { $0.services }
  .flatMap { $0.characteristics }
  .filter { $0.characteristicType == HMCharacteristicTypeHue }
```

Nice, now we need a way to grab a hue value from a `UIColor`:

``` swift
func hsba(from color: UIColor) -> [CGFloat] {
  var HSBA = [CGFloat](repeating: 0.0, count: 4)
  color.getHue(&HSBA[0], saturation: &HSBA[1], brightness: &HSBA[2], alpha: &HSBA[3])
  return HSBA
}
```

Not the prettiest code, but it'll do. Now let's use it:

``` swift
let color = hsba(from: UIColor.blue)
let newHue = Float(color[0])

for hc in hueCharacteristics {
  let max = hc.metadata?.maximumValue?.floatValue ?? 1.0

  hc.writeValue(NSNumber(value: newHue * max), completionHandler: { if let error = $0 { print("Failed: \(error)") } })
}
```

Here we grab our `newHue` value for our `UIColor.blue` color, then iterate through all the `hueCharacteristics` from before, ensuring we multiply our `newHue` value by each **characteristic's** `maximumValue` property. This makes sure we're speaking the language of the accessory, and setting a value in a scale it understands. 

Finally, we wrap up our value in an `NSNumber` and write it to the `HMCharacteristic`. 

When we build and run, we're prompted for permission. After allowing, we can flip back over to the **HomeKit Accessory Simulator** app and see the hue of our **light** has changed. Success!

Try adding more simulated **lights**, setting them to random colors, then running the app again to see a greater effect.

That's all for now. We've got plenty more to explore in HomeKit, look out for more Bites soon.

<p></p>
