Skip to content

Instantly share code, notes, and snippets.

@mugifly
Last active March 27, 2019 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mugifly/22af0d55ccf4099becd44165f6500196 to your computer and use it in GitHub Desktop.
Save mugifly/22af0d55ccf4099becd44165f6500196 to your computer and use it in GitHub Desktop.
Simplest API Server for Google Home Notifier
/**
* Simplest API Server for Google Home Notifier
* https://gist.github.com/mugifly/22af0d55ccf4099becd44165f6500196/
* Thanks for https://github.com/noelportugal/google-home-notifier
**/
'use strict';
// Language to speak
const SPEECH_LANGUAGE = 'ja';
const express = require('express');
const bodyParser = require('body-parser');
const googlehome = require('google-home-notifier');
const app = express();
app.use(bodyParser.text());
/**
* GET - /
*/
app.get('/', (req, res) => {
res.send('Simplest API Server for Google Home Notifier');
});
/**
* POST - /speak/:deviceIdentifier
* (Content-Type: 'text/plain')
*/
app.post('/speak/:deviceIdentifier', (req, res) => {
const device_identifier = req.params.deviceIdentifier;
let device_name = null;
let device_ip = null;
const speech_text = req.body;
res.header('Content-Type', 'text/plain;charset=utf-8');
if (device_identifier == null || speech_text == null) {
return res.status(400).send('Invalid arguments');
} else if (1000 <= speech_text.length) {
return res.status(400).send('Too long text');
}
if (device_identifier.match(/^\d+\.\d+\.\d+\.\d+$/)) {
device_ip = device_identifier;
} else if (device_identifier.match(/^Google-Home(-Mini|)(-[a-zA-Z0-9]*|)$/)) {
device_name = device_identifier;
} else {
return res.status(400).send('Invalid device identifier');
}
// Speak
try {
// Specify the device
if (device_ip) {
googlehome.ip(device_ip, SPEECH_LANGUAGE);
} else {
googlehome.device(device_name, SPEECH_LANGUAGE);
}
// Send to device
googlehome.notify(speech_text, (n_result) => {
res.send(n_result);
});
} catch (e) {
res.status(500).send(e.toString());
}
});
const listener = app.listen(process.env.port || 3002, () => {
console.log('Simple API Server for Google Home Notifier listening on localhost:' + listener.address().port);
});

ghome-notifier-server

Simplest API Server for Google Home Notifier

Installation

It tested on Raspberry Pi Zero W with Raspbian Stretch.

Firstly, Install the dependencies and this script.

$ sudo apt-get install git-core libnss-mdns libavahi-compat-libdnssd-dev

$ wget https://gist.github.com/mugifly/22af0d55ccf4099becd44165f6500196/archive/master.zip -O ghome-notifier-server.zip
$ unzip -j -d ghome-notifier-server ghome-notifier-server.zip
$ cd ghome-notifier-server/

$ npm install

Then, please modify a module according to https://github.com/noelportugal/google-home-notifier#after-npm-install.

Usage

1. Start server

$ npm start &

2. Call the WebAPI

To specify Google Home by IP address:

$ echo "Hello" | curl -H "Content-Type: text/plain" --data-binary @- http://localhost:3002/speak/192.168.x.x

To specify Google Home by a device name:

$ echo "Hello" | curl -H "Content-Type: text/plain" --data-binary @- http://localhost:3002/speak/Google-Home-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
{
"name": "ghome-notifier-server",
"version": "1.1.0",
"description": "Simplest API Server for Google Home Notifier",
"homepage": "https://gist.github.com/mugifly/22af0d55ccf4099becd44165f6500196#file-ghome-notifier-server-md",
"main": "ghome-notifier-server.js",
"scripts": {
"start": "node ghome-notifier-server.js",
"test": "echo \"Error: no test specified\" && exit 0"
},
"author": "Masanori Ohgita",
"license": "MIT",
"dependencies": {
"body-parser": "^1.18.3",
"express": "^4.16.2",
"google-home-notifier": "^1.2.0"
}
}
@mugifly
Copy link
Author

mugifly commented Aug 19, 2018

For example, you can make a voice notification with Google Home from Home Assistant.
The following is an example configuration using "Command line Notify" component and Automation feature on Home Assistant.

/config/configuration.yaml

notify:
  - name: ghome_notifier
    platform: command_line
    command: "curl -H \"Content-Type: text/plain\" --data-binary @- http://192.168.10.100:3002/speak/Google-Home-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

/config/automations.yaml

- alias: window_opened_notification
  trigger:
    - platform: time
      # It checks state of window every minute.
      minutes: '/1'
      seconds: 00
  action:
    - condition: or
      conditions:
       - condition: state
         entity_id: 'cover.dining_window_1'
         # trigger only if this window was 'open' for last 30 seconds.
         state: 'open'
         for:
           seconds: 30
       - condition: state
         entity_id: 'cover.dining_window_2'
         state: 'open'
    - service: notify.ghome_notifier
      data:
        message: 'An window is still open. please close.'

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