diff --git a/source/developers/python_api.markdown b/source/developers/python_api.markdown index 727e94f071e..2e752f80b4c 100644 --- a/source/developers/python_api.markdown +++ b/source/developers/python_api.markdown @@ -40,7 +40,7 @@ print(remote.get_config(api)) ### {% linkable_title Get details about services, events, and entitites %} -The output from this is similar to the output you'd find via the frontend, using the DevTools console. +The output from this is similar to the output you'd find via the frontend, using the [Developer Tools](/docs/tools/dev-tools/). ```python import homeassistant.remote as remote @@ -71,12 +71,11 @@ To get the details of a single entity, use `get_state`: import homeassistant.remote as remote api = remote.API('127.0.0.1', 'YOUR_PASSWORD') -office_temperature = remote.get_state(api, 'sensor.office_temperature') -print('{} is {} {}.'.format(office_temperature.name, - office_temperature.state, - office_temperature.attributes['unit_of_measurement'] - ) - ) +office_temp = remote.get_state(api, 'sensor.office_temperature') +print('{} is {} {}.'.format( + office_temp.name, office_temp.state, + office_temp.attributes['unit_of_measurement']) +) ``` This outputs the details which are stored for this entity, ie: @@ -92,10 +91,9 @@ import homeassistant.remote as remote api = remote.API('127.0.0.1', 'YOUR_PASSWORD') switch_livingroom = remote.get_state(api, 'switch.livingroom_pin_2') -print('{} is {}.'.format(switch_livingroom.name, - switch_livingroom.state - ) - ) +print('{} is {}.'.format( + switch_livingroom.name, switch_livingroom.state) +) ``` ### {% linkable_title Set the state of an entity %} @@ -184,3 +182,55 @@ data = {"title":"Test", "message":"A simple test message from HA."} remote.call_service(api, domain, 'jabber', data) ``` + +## {% linkable_title Examples %} + +This section contains a couple of sample scripts. + +### {% linkable_title List all sensors and their value %} + +If you want to see, export or list all sensor states then an easy way to do it, is to get all entities and filter for the one you are looking for. + +```python +import homeassistant.remote as remote + +api = remote.API('127.0.0.1', 'YOUR_PASSWORD') +entities = remote.get_states(api) +for entity in entities: + if entity.entity_id.startswith('sensor'): + data = remote.get_state(api, entity.entity_id) + print('{}: {}'.format(data.attributes['friendly_name'], data.state)) +``` + +### {% linkable_title Show difference between `last_changed` and `last_updated` %} + +The documentation about the [State Objects](/docs/configuration/state_object/) describes the +`last_changed` and `last_updated` fields. This example shows how it works in practice. + +```python +import time + +from prettytable import PrettyTable +import homeassistant.remote as remote + +api = remote.API('127.0.0.1', 'YOUR_PASSWORD') + +ACTIONS = { + 'Create sensor': [21, 'Test'], + 'No new sensor value': [21, 'Test'], + 'New sensor value': [22, 'Test'], + 'Update attribute': [22, 'Test1'], +} + +output = PrettyTable(['Action', 'Last changed', 'Last updated']) + +for key, value in ACTIONS.items(): + remote.set_state(api, 'sensor.test', new_state=value[0], + attributes={'friendly_name': value[1]}) + data = remote.get_state(api, 'sensor.test') + output.add_row([key, data.last_changed, data.last_updated]) + time.sleep(2) + +print(output) +``` + diff --git a/source/developers/rest_api.markdown b/source/developers/rest_api.markdown index ede8f8157b2..8f620814d5a 100644 --- a/source/developers/rest_api.markdown +++ b/source/developers/rest_api.markdown @@ -39,7 +39,7 @@ print(response.text) ```

-You can append `?api_password=YOUR_PASSWORD` to any url to log in automatically. +You can append `?api_password=YOUR_PASSWORD` to any URL to log in automatically.

Successful calls will return status code 200 or 201. Other status codes that can return are: @@ -73,33 +73,43 @@ $ curl -X GET -H "x-ha-access: YOUR_PASSWORD" \ Returns the current configuration as JSON. ```json -{ - "components": [ - "recorder", - "http", - "weather.openweathermap", - "api", - "websocket_api", - "frontend", - "sensor.time_date", - "sun", - "device_tracker", - "group", - "automation" - ], - "config_dir": "/home/ha/.homeassistant", - "elevation": 590, - "latitude": 45.92, - "location_name": "Home", - "longitude": 6.52, - "time_zone": "Europe/Zurich", - "unit_system": { - "length": "km", - "mass": "g", - "temperature": "\\u00b0C", - "volume": "L" - }, - "version": "0.37.0.dev0" +{ + "components":[ + "sensor.cpuspeed", + "frontend", + "config.core", + "http", + "map", + "api", + "sun", + "config", + "discovery", + "conversation", + "recorder", + "group", + "sensor", + "websocket_api", + "automation", + "config.automation", + "config.customize" + ], + "config_dir":"/home/ha/.homeassistant", + "elevation":510, + "latitude":45.8781529, + "location_name":"Home", + "longitude":8.458853651, + "time_zone":"Europe/Zurich", + "unit_system":{ + "length":"km", + "mass":"g", + "temperature":"\u00b0C", + "volume":"L" + }, + "version":"0.56.2", + "whitelist_external_dirs":[ + "/home/ha/.homeassistant/www", + "/home/ha/.homeassistant/" + ] } ``` @@ -115,10 +125,10 @@ Returns basic information about the Home Assistant instance as JSON. ```json { - "base_url": "http://127.0.0.1:8123", + "base_url": "http://192.168.0.2:8123", "location_name": "Home", "requires_api_password": true, - "version": "0.20.0.dev0" + "version": "0.56.2" } ```