{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Using the Home Assistant Python API\n", "This notebook shows how you can work the [Home Assistant Python API](https://home-assistant.io/developers/python_api/) inside a Jupyter notebook." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setup" ] }, { "cell_type": "code", "execution_count": 143, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import homeassistant.remote as remote" ] }, { "cell_type": "code", "execution_count": 157, "metadata": { "collapsed": false }, "outputs": [], "source": [ "HOST = '127.0.0.1'\n", "PASSWORD = 'YOUR_PASSWORD'\n", "\n", "api = remote.API(HOST, PASSWORD)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Validation of the API is ready to use" ] }, { "cell_type": "code", "execution_count": 158, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ok\n" ] } ], "source": [ "print(remote.validate_api(api))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Retrieve the configuration\n", "The Home Assistant Python API is not complete. Think of it as a sub-set of the [RESTful API](https://home-assistant.io/developers/rest_api/). This means that if a certain option is not available you can use Python's `requests` module to retrieve the data." ] }, { "cell_type": "code", "execution_count": 148, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import requests" ] }, { "cell_type": "code", "execution_count": 150, "metadata": { "collapsed": false }, "outputs": [], "source": [ "url = 'http://{}:8123/api/{}'.format(HOST, 'config')\n", "headers = {'x-ha-access': PASSWORD,\n", " 'content-type': 'application/json'}\n", "\n", "config = requests.get(url, headers=headers).json()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(Starting with 0.25 `remote.get_config(api)` will be the short cut for this.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Display the current location" ] }, { "cell_type": "code", "execution_count": 151, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "Location(Bärengraben, 4, Grosser Muristalden, Gryphenhübeli, Bern, Verwaltungskreis Bern-Mittelland, Bern - Berne, 3006, Schweiz, Suisse, Svizzera, Svizra, (46.9480272, 7.45974119954229, 0.0))" ] }, "execution_count": 151, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# This requires that geopy is installed, eg. pip3 install geopy\n", "from geopy.geocoders import Nominatim\n", "\n", "geolocator = Nominatim()\n", "location = geolocator.reverse(\"{}, {}\".format(config.get('latitude'), config.get('longitude')))\n", "location" ] }, { "cell_type": "code", "execution_count": 152, "metadata": { "collapsed": false }, "outputs": [], "source": [ "factor = 0.001\n", "box = ((config.get('latitude') - factor, config.get('longitude') - factor),\n", " (config.get('latitude') + factor, config.get('longitude') + factor))\n", "marker = (config.get('latitude'), config.get('longitude'))\n", "\n", "url = '{0}/export/embed.html?bbox={1}%2C{2}%2C{3}%2C{4}&layer=mapnik&marker={5}%2C{6}'.format(\n", " 'https://www.openstreetmap.org', box[0][1], box[0][0], box[1][1], box[1][0], marker[0], marker[1])" ] }, { "cell_type": "code", "execution_count": 153, "metadata": { "collapsed": true }, "outputs": [], "source": [ "iframe = ''.format(url)" ] }, { "cell_type": "code", "execution_count": 154, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from IPython.display import display, HTML\n", "display(HTML(iframe))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is also a so-called `magics` present for HTML. This will allow us to include a IFRAME as well. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Display the state of an entity" ] }, { "cell_type": "code", "execution_count": 159, "metadata": { "collapsed": true }, "outputs": [], "source": [ "kitchen = remote.get_state(api, 'sensor.kitchen_temperature')" ] }, { "cell_type": "code", "execution_count": 160, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Kitchen Temperature is 21 °C.\n" ] } ], "source": [ "print('{} is {} {}.'.format(kitchen.attributes['friendly_name'],\n", " kitchen.state,\n", " kitchen.attributes['unit_of_measurement']\n", " )\n", " )" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.3" } }, "nbformat": 4, "nbformat_minor": 0 }