Do you ever wanted to include some data coming from an external rest-api into your home automation? It's not at all hard to create a simple sensor that will run an http-call on each sensor update.
Start by creating a file inside your homeassistant root folder. On most linux-systems this will be /home/user/.homeassistant in this folder create a file custom_components/sensor/http.py
This file will contain our logic for our sensor.
In order to register our service, we need to define a method setup_platform():
def setup_platform(hass, config, add_devices, discovery_info=None): add_devices([HttpSensor()])
Next we need to create a custom class which will contain our sensor logic:
import urllib.request from homeassistant.helpers.entity import Entity class HttpSensor(Entity): def __init__(self): self._state = None @property def name(self): return 'Name of sensor' @property def state(self): return self._state @property def unit_of_measurement(self): return "unit name" def update(self): contents = urllib.request.urlopen("https://url-of-service").read() contents = str(contents) self._state = contents
In order to register this new sensor we need to reference it in our configuration.xml file:
sensor: - platform: http