Skip to content

Instantly share code, notes, and snippets.

@timolins
Last active August 17, 2022 12:03
Show Gist options
  • Save timolins/0efccf254517773e7763398736dd7661 to your computer and use it in GitHub Desktop.
Save timolins/0efccf254517773e7763398736dd7661 to your computer and use it in GitHub Desktop.
React Homey – A declarative way to home automation
/*
Disclaimer: This is just a drafted api and not real (yet).
*/
import { useState } from "react"
import ReactHomey, {
// Components
Light,
Fan,
Room,
Group,
// Hooks
useDevices,
useSensor,
useTrigger
} from "react-homey"
const Home = () => {
const [moodlight, setMoodlight] = useState(false)
const devices = useDevices()
const thermostatData = useSensor(devices.findById("Thermostat"))
const isHighTemperature = thermostatData.temperature > 25
// Listen for button presses
useTrigger(() => {
setMoodlight(!moodlight)
}, getIdByName("My Button"))
return (
<Room>
<Fan level={isHighTemperature ? 1 : 0}/>
<Light id={getIdByName("Ceiling Lamp")} power={true} brightness={0.8} />
<Group power={moodlight}>
<Light id={devices.findById("Ambient Lamp 1")} />
<Light id={devices.findById("Ambient Lamp 2")} />
<Light id={devices.findById("Ambient Lamp 3")} />
<Group/>
</Room>
)
}
ReactHomey.render(<Home/>)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment