This notebook shows how you can work the Home Assistant Python API inside a Jupyter notebook.
import homeassistant.remote as remote
HOST = '127.0.0.1'
PASSWORD = 'YOUR_PASSWORD'
api = remote.API(HOST, PASSWORD)
print(remote.validate_api(api))
ok
The Home Assistant Python API is not complete. Think of it as a sub-set of the RESTful API. This means that if a certain option is not available you can use Python's requests
module to retrieve the data.
import requests
url = 'http://{}:8123/api/{}'.format(HOST, 'config')
headers = {'x-ha-access': PASSWORD,
'content-type': 'application/json'}
config = requests.get(url, headers=headers).json()
(Starting with 0.25 remote.get_config(api)
will be the short cut for this.)
# This requires that geopy is installed, eg. pip3 install geopy
from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.reverse("{}, {}".format(config.get('latitude'), config.get('longitude')))
location
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))
factor = 0.001
box = ((config.get('latitude') - factor, config.get('longitude') - factor),
(config.get('latitude') + factor, config.get('longitude') + factor))
marker = (config.get('latitude'), config.get('longitude'))
url = '{0}/export/embed.html?bbox={1}%2C{2}%2C{3}%2C{4}&layer=mapnik&marker={5}%2C{6}'.format(
'https://www.openstreetmap.org', box[0][1], box[0][0], box[1][1], box[1][0], marker[0], marker[1])
iframe = '<iframe width="720" height="280" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src={} style="border: 1px solid black"></iframe>'.format(url)
from IPython.display import display, HTML
display(HTML(iframe))
There is also a so-called magics
present for HTML. This will allow us to include a IFRAME as well.
kitchen = remote.get_state(api, 'sensor.kitchen_temperature')
print('{} is {} {}.'.format(kitchen.attributes['friendly_name'],
kitchen.state,
kitchen.attributes['unit_of_measurement']
)
)
Kitchen Temperature is 21 °C.