Meet the Smart-Home Hub: Plan It, Build It
The third pass starts with a smart-home hub that works with one brand of light, behind one front door the phone app and the wall panel call; then two more brands arrive whose code is shaped nothing like the first, and the plan has to keep other companies' code at the edge.
Journeyman · OOP · Updated
After this lesson you can
- Plan an app that talks to other companies' code: decide the interfaces it expects, and keep the outside code at the edge
- Give an app one front door, so the screens that call it never depend on which brand of device is behind it
Key takeaways
- An app that talks to other companies' code should decide what it expects first, as its own interfaces, and keep the outside code at the edge.
- The hub is a front door, the Facade from Pass 1 grown up: the phone app and the wall panel call the hub, and never touch a device.
- Version 1 works with one brand, and that's fine. The trouble starts when the hub's own code is written in that brand's words.
- Three brands, three shapes: setPower, engage and startStream do similar jobs with different names, and none of them is the hub's to change.
Before this: Facade: One Button, Many Parts (one simple front on several parts), and interfaces (a promise any class can keep).
Most of the code you'll write at work talks to code you didn't write: a payment company's library, a shipping company's web service, a device maker's kit. You can call that code, but you can't change it, and every company shapes its code its own way. This pass follows one app that lives in that world: a smart-home hub. This lesson plans it, builds version 1, and meets the problem the next lessons solve.
What the app does
HomeBase is a small box that runs a home. Maya's house has three rooms (the living room, the kitchen and the bedroom), and the hub controls the devices in them. Maya and Sam use the phone app; anyone can press the panel on the kitchen wall. Every morning a routine called "good morning" gets the house going.
Version 1 controls one kind of device from one brand: Lumo lights, one in each room. Lumo sells its lights with a small library of code, and a Lumo light turns on with setPower(true) and off with setPower(false). That library is Lumo's: the hub can use it, but nobody at HomeBase can change it. When Lumo ships an update, their code replaces whatever was there.
Plan it: a front door, and the code at the edge
Start with the nouns, as in Pass 1: a hub, rooms, lights. Then ask who calls whom. The phone app and the wall panel shouldn't talk to the lights themselves. They should ask the hub ("living room light on", "good morning"), and the hub works out which device does what.
That's the Facade from Pass 1, grown up. In the bakery, checkout.placeOrder put one simple front on four parts that had to run in order. The hub's front door does the same for a whole house, with one more job: the parts behind it come from other companies, and they'll change. If the phone app never touches a light, then a new brand of light is the hub's business, not the phone app's.
Build version 1
Here is version 1. The hub keeps a Lumo light for each room, and its front door has three things to ask for: a light on, a light off, and good morning (the kitchen light, for now):
Hub hub = new Hub();
Console.WriteLine("Phone app: living room light on");
hub.LightOn("living room");
Console.WriteLine("Wall panel: good morning");
hub.GoodMorning();
// The hub's front door: the phone app and the wall panel ask it, and never touch a device.
class Hub
{
private readonly Dictionary<string, LumoLight> lights = new Dictionary<string, LumoLight>();
public Hub()
{
foreach (string room in new[] { "living room", "kitchen", "bedroom" })
{
lights[room] = new LumoLight(room);
}
}
public void LightOn(string room) => lights[room].SetPower(true);
public void LightOff(string room) => lights[room].SetPower(false);
public void GoodMorning() => lights["kitchen"].SetPower(true);
}
// Lumo's own code, from its library: the hub can use it, but can't change it.
class LumoLight
{
private readonly string room;
public LumoLight(string room) { this.room = room; }
public void SetPower(bool on) => Console.WriteLine( quot;Lumo [{room}]: power {(on ? "on" : "off")}");
}
Phone app: living room light on
Lumo [living room]: power on
Wall panel: good morning
Lumo [kitchen]: power on
// Lumo's own code, from its library: the hub can use it, but can't change it.
class LumoLight {
private final String room;
LumoLight(String room) { this.room = room; }
void setPower(boolean on) { IO.println("Lumo [" + room + "]: power " + (on ? "on" : "off")); }
}
// The hub's front door: the phone app and the wall panel ask it, and never touch a device.
class Hub {
private final Map<String, LumoLight> lights = new HashMap<>();
Hub() {
for (String room : List.of("living room", "kitchen", "bedroom")) {
lights.put(room, new LumoLight(room));
}
}
void lightOn(String room) { lights.get(room).setPower(true); }
void lightOff(String room) { lights.get(room).setPower(false); }
void goodMorning() { lights.get("kitchen").setPower(true); }
}
void main() {
Hub hub = new Hub();
IO.println("Phone app: living room light on");
hub.lightOn("living room");
IO.println("Wall panel: good morning");
hub.goodMorning();
}
Phone app: living room light on
Lumo [living room]: power on
Wall panel: good morning
Lumo [kitchen]: power on
class LumoLight:
"""Lumo's own code, from its library: the hub can use it, but can't change it."""
def __init__(self, room):
self.room = room
def set_power(self, on):
print(f"Lumo [{self.room}]: power {'on' if on else 'off'}")
class Hub:
"""The hub's front door: the phone app and the wall panel ask it, and never touch a device."""
def __init__(self):
self.lights = {}
for room in ["living room", "kitchen", "bedroom"]:
self.lights[room] = LumoLight(room)
def light_on(self, room):
self.lights[room].set_power(True)
def light_off(self, room):
self.lights[room].set_power(False)
def good_morning(self):
self.lights["kitchen"].set_power(True)
hub = Hub()
print("Phone app: living room light on")
hub.light_on("living room")
print("Wall panel: good morning")
hub.good_morning()
Phone app: living room light on
Lumo [living room]: power on
Wall panel: good morning
Lumo [kitchen]: power on
// Lumo's own code, from its library: the hub can use it, but can't change it.
class LumoLight {
constructor(room) {
this.room = room;
}
setPower(on) {
console.log(`Lumo [${this.room}]: power ${on ? "on" : "off"}`);
}
}
// The hub's front door: the phone app and the wall panel ask it, and never touch a device.
class Hub {
lights = new Map();
constructor() {
for (const room of ["living room", "kitchen", "bedroom"]) {
this.lights.set(room, new LumoLight(room));
}
}
lightOn(room) { this.lights.get(room).setPower(true); }
lightOff(room) { this.lights.get(room).setPower(false); }
goodMorning() { this.lights.get("kitchen").setPower(true); }
}
const hub = new Hub();
console.log("Phone app: living room light on");
hub.lightOn("living room");
console.log("Wall panel: good morning");
hub.goodMorning();
Phone app: living room light on
Lumo [living room]: power on
Wall panel: good morning
Lumo [kitchen]: power on
// Lumo's own code, from its library: the hub can use it, but can't change it.
class LumoLight {
private readonly room: string;
constructor(room: string) {
this.room = room;
}
setPower(on: boolean): void {
console.log(`Lumo [${this.room}]: power ${on ? "on" : "off"}`);
}
}
// The hub's front door: the phone app and the wall panel ask it, and never touch a device.
class Hub {
private readonly lights = new Map<string, LumoLight>();
constructor() {
for (const room of ["living room", "kitchen", "bedroom"]) {
this.lights.set(room, new LumoLight(room));
}
}
lightOn(room: string): void { this.lights.get(room)!.setPower(true); }
lightOff(room: string): void { this.lights.get(room)!.setPower(false); }
goodMorning(): void { this.lights.get("kitchen")!.setPower(true); }
}
const hub = new Hub();
console.log("Phone app: living room light on");
hub.lightOn("living room");
console.log("Wall panel: good morning");
hub.goodMorning();
Phone app: living room light on
Lumo [living room]: power on
Wall panel: good morning
Lumo [kitchen]: power on
The names follow each language's style (SetPower in C#, set_power in Python, setPower in the others). In TypeScript, the ! after get(room) tells the compiler "this room is there", since a map lookup can come back empty.
It works, and for version 1 that's the point. The phone app and the wall panel don't know Lumo exists: they only know the hub.
Try it. Add a fourth room, a hallway, and turn its light on from the phone app. Only the hub changes.
What's likely to change?
The question every pass starts with. Maya and Sam already have plans for the house, and HomeBase's own team has a list:
| Likely to change | Why | Where it lives in version 1 |
|---|---|---|
| The brands of device | Maya ordered a Bolt lock for the front door and a Tuneo speaker for the kitchen; more brands will follow | the word LumoLight, and Lumo's setPower, inside the hub |
| The routines | "Good morning" should also unlock the front door and start the news | goodMorning |
| The weather | The wall panel will show it, from a weather service called SkyCast, which is far away and slow | nowhere yet |
| Who may do what | A guest can turn on a light, but can't unlock the front door | nowhere yet |
And some things won't change: the hub has rooms, rooms have devices, and the screens ask the hub.
Look at the first row's last column. The hub's own code says setPower, which is Lumo's word. That's coupling (how much one piece of code depends on another): the hub is tied to one brand, and the brands are what's about to change.
Three brands, three shapes
Maya's two new devices arrive, each with its brand's own library. Here are all three brands, used the way their makers wrote them:
const string News = "https://tuneo.example/news";
new LumoLight("kitchen").SetPower(true);
BoltLock frontDoor = new BoltLock("front door");
frontDoor.Release();
Console.WriteLine( quot;The front door says: {frontDoor.Status()}");
new TuneoSpeaker("kitchen").StartStream(News);
class LumoLight
{
private readonly string room;
public LumoLight(string room) { this.room = room; }
public void SetPower(bool on) => Console.WriteLine( quot;Lumo [{room}]: power {(on ? "on" : "off")}");
}
class BoltLock
{
private readonly string door;
private string status = "ENGAGED";
public BoltLock(string door) { this.door = door; }
public void Engage() { status = "ENGAGED"; Console.WriteLine( quot;Bolt [{door}]: ENGAGED"); }
public void Release() { status = "RELEASED"; Console.WriteLine( quot;Bolt [{door}]: RELEASED"); }
public string Status() => status;
}
class TuneoSpeaker
{
private readonly string room;
public TuneoSpeaker(string room) { this.room = room; }
public void StartStream(string url) => Console.WriteLine( quot;Tuneo [{room}]: streaming {url}");
public void Halt() => Console.WriteLine( quot;Tuneo [{room}]: halted");
}
Lumo [kitchen]: power on
Bolt [front door]: RELEASED
The front door says: RELEASED
Tuneo [kitchen]: streaming https://tuneo.example/news
class LumoLight {
private final String room;
LumoLight(String room) { this.room = room; }
void setPower(boolean on) { IO.println("Lumo [" + room + "]: power " + (on ? "on" : "off")); }
}
class BoltLock {
private final String door;
private String status = "ENGAGED";
BoltLock(String door) { this.door = door; }
void engage() { status = "ENGAGED"; IO.println("Bolt [" + door + "]: ENGAGED"); }
void release() { status = "RELEASED"; IO.println("Bolt [" + door + "]: RELEASED"); }
String status() { return status; }
}
class TuneoSpeaker {
private final String room;
TuneoSpeaker(String room) { this.room = room; }
void startStream(String url) { IO.println("Tuneo [" + room + "]: streaming " + url); }
void halt() { IO.println("Tuneo [" + room + "]: halted"); }
}
final String NEWS = "https://tuneo.example/news";
void main() {
new LumoLight("kitchen").setPower(true);
BoltLock frontDoor = new BoltLock("front door");
frontDoor.release();
IO.println("The front door says: " + frontDoor.status());
new TuneoSpeaker("kitchen").startStream(NEWS);
}
Lumo [kitchen]: power on
Bolt [front door]: RELEASED
The front door says: RELEASED
Tuneo [kitchen]: streaming https://tuneo.example/news
NEWS = "https://tuneo.example/news"
class LumoLight:
def __init__(self, room):
self.room = room
def set_power(self, on):
print(f"Lumo [{self.room}]: power {'on' if on else 'off'}")
class BoltLock:
def __init__(self, door):
self.door = door
self._status = "ENGAGED"
def engage(self):
self._status = "ENGAGED"
print(f"Bolt [{self.door}]: ENGAGED")
def release(self):
self._status = "RELEASED"
print(f"Bolt [{self.door}]: RELEASED")
def status(self):
return self._status
class TuneoSpeaker:
def __init__(self, room):
self.room = room
def start_stream(self, url):
print(f"Tuneo [{self.room}]: streaming {url}")
def halt(self):
print(f"Tuneo [{self.room}]: halted")
LumoLight("kitchen").set_power(True)
front_door = BoltLock("front door")
front_door.release()
print(f"The front door says: {front_door.status()}")
TuneoSpeaker("kitchen").start_stream(NEWS)
Lumo [kitchen]: power on
Bolt [front door]: RELEASED
The front door says: RELEASED
Tuneo [kitchen]: streaming https://tuneo.example/news
const NEWS = "https://tuneo.example/news";
class LumoLight {
constructor(room) { this.room = room; }
setPower(on) { console.log(`Lumo [${this.room}]: power ${on ? "on" : "off"}`); }
}
class BoltLock {
#status = "ENGAGED";
constructor(door) { this.door = door; }
engage() { this.#status = "ENGAGED"; console.log(`Bolt [${this.door}]: ENGAGED`); }
release() { this.#status = "RELEASED"; console.log(`Bolt [${this.door}]: RELEASED`); }
status() { return this.#status; }
}
class TuneoSpeaker {
constructor(room) { this.room = room; }
startStream(url) { console.log(`Tuneo [${this.room}]: streaming ${url}`); }
halt() { console.log(`Tuneo [${this.room}]: halted`); }
}
new LumoLight("kitchen").setPower(true);
const frontDoor = new BoltLock("front door");
frontDoor.release();
console.log(`The front door says: ${frontDoor.status()}`);
new TuneoSpeaker("kitchen").startStream(NEWS);
Lumo [kitchen]: power on
Bolt [front door]: RELEASED
The front door says: RELEASED
Tuneo [kitchen]: streaming https://tuneo.example/news
const NEWS = "https://tuneo.example/news";
class LumoLight {
private readonly room: string;
constructor(room: string) { this.room = room; }
setPower(on: boolean): void { console.log(`Lumo [${this.room}]: power ${on ? "on" : "off"}`); }
}
class BoltLock {
private readonly door: string;
private current = "ENGAGED";
constructor(door: string) { this.door = door; }
engage(): void { this.current = "ENGAGED"; console.log(`Bolt [${this.door}]: ENGAGED`); }
release(): void { this.current = "RELEASED"; console.log(`Bolt [${this.door}]: RELEASED`); }
status(): string { return this.current; }
}
class TuneoSpeaker {
private readonly room: string;
constructor(room: string) { this.room = room; }
startStream(url: string): void { console.log(`Tuneo [${this.room}]: streaming ${url}`); }
halt(): void { console.log(`Tuneo [${this.room}]: halted`); }
}
new LumoLight("kitchen").setPower(true);
const frontDoor = new BoltLock("front door");
frontDoor.release();
console.log(`The front door says: ${frontDoor.status()}`);
new TuneoSpeaker("kitchen").startStream(NEWS);
Lumo [kitchen]: power on
Bolt [front door]: RELEASED
The front door says: RELEASED
Tuneo [kitchen]: streaming https://tuneo.example/news
Three brands, three shapes. A Lumo light turns on with setPower(true). A Bolt lock opens with release(), and tells you how it is in capital letters, "RELEASED" or "ENGAGED". A Tuneo speaker plays with startStream(url), where a station is a web address, and stops with halt(). They do the same kinds of jobs, in three different words, and none of it is HomeBase's to change.
If the hub copies each brand's words into its own code, every routine fills up with setPower, release and startStream, and the next brand means editing all of them again.
The plan: the hub says what it expects
So before writing any of that, decide the hub's own words. Whatever the brand, the hub wants three simple promises, as interfaces:
- a
Lightwithon()andoff(); - a
Lockwithlock()andunlock(); - a
Speakerwithplay(station)andstop().
The hub's own code (its front door and its routines) will only ever use these three. The brands' code stays at the edge, behind a fence, because the brands are what changes:

The diagram has a gap in the middle, and it's the real question: a LumoLight doesn't have an on() method, and we can't add one. How does a Lumo light keep the hub's Light promise when we can't change a line of Lumo's code? That's the next lesson.
Check yourself
2 questions to try on the lesson page.
So what
When an app talks to code it doesn't own, decide first what it expects, behind one front door, and keep the outside code at the edge. Then a new brand is a problem at the edge, not in every routine.