Skip to content

Instantly share code, notes, and snippets.

@rfletcher
Last active June 28, 2022 13:55
Show Gist options
  • Save rfletcher/2816fd69963760b21670ae0cc7e35d62 to your computer and use it in GitHub Desktop.
Save rfletcher/2816fd69963760b21670ae0cc7e35d62 to your computer and use it in GitHub Desktop.
A simple bridge between iMessage and Home Assistant's conversation component
const HomeAssistant = require( 'homeassistant' );
const Pino = require( 'pino' );
const config = require( 'config' );
const hass = new HomeAssistant( config.get( 'home_assistant' ) );
const imessage = require( 'osa-imessage' );
const logger = Pino();
// TODO package this better
const bridge = {
handleMessage: function( sender, text ) {
logger.info( `from ${sender}: ${text}` );
hass.services._post( '/conversation/process', null, {
text: text
} )
.then( res => {
imessage.send( sender, res.speech.plain.speech );
} )
.catch( err => {
logger.error( err );
imessage.send( sender, "Sorry, something broke" );
} );
}
}
imessage.listen().on( 'message', ( msg ) => {
if ( msg.fromMe ) {
logger.warn( `ignoring message from myself: ${msg.text}` );
return;
}
if ( ! config.get( 'allowed_senders' ).includes( msg.handle ) ) {
logger.warn( `ignoring message from unknown sender: ${msg.handle}: ${msg.text}` );
return;
}
bridge.handleMessage( msg.handle, msg.text );
} );
---
allowed_senders:
- "[email protected]"
home_assistant:
host: "http://<your home assistant host or ip>"
port: 8123
token: "<your home assistant token>"
{
"name": "imessage-home-assistant",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"dependencies": {
"config": "3.0.1",
"homeassistant": "0.2.0",
"js-yaml": "3.12.1",
"osa-imessage": "2.4.2",
"pino": "5.11.1"
}
}
@rfletcher
Copy link
Author

rfletcher commented Feb 19, 2019

                  chat

This represents an hour of two of tinkering, so use at your own risk, but here's a simple proof of concept bridge between Home Assistant and iMessage.

Requirements

  1. Home Assistant
  2. Node and NPM
  3. An always-on Mac (or Hackintosh?)
  4. A second device that can use iMessage (an iPhone, iPad, or another Mac).

Setup

  1. Configure Home Assistant's conversation component to do something useful for some plain english input.
  2. Create a new Apple ID and sign into it with iMessage on the Mac.
  3. npm install; node app.js on that Mac.
  4. Text that new Apple ID using iMessage, and now you’re having a two-way conversation with your house, with tight Siri integration, from anywhere that has cell service, and without having to poke a hole in your firewall.

PS: The config file goes in config/default.yaml, but GitHub wouldn't let me include a slash in the name.

@adrum
Copy link

adrum commented Feb 21, 2019

Thank you for posting this! I've extended this to include a REST notify service that I recently integrated in my setup. Let me know if you want me to post it.

Edit: Forked here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment