Last active
July 21, 2019 15:22
-
-
Save nitobuendia/82992cb0bdc8e076ce588f9c35e94ff8 to your computer and use it in GitHub Desktop.
API Rest Server for Enviro+ (and sensor config for Home Assistant)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flask | |
import reader | |
app = flask.Flask(__name__) | |
enviro_reader = reader.EnviroReader() | |
@app.route("/api/all") | |
def get_all_readings(): | |
reading = enviro_reader.get_all() | |
return flask.jsonify(reading) | |
@app.route("/api/gas/all") | |
def get_gas(): | |
reading = enviro_reader.get_gas() | |
return flask.jsonify(reading) | |
@app.route("/api/gas/adc") | |
def get_adc(): | |
reading = enviro_reader.get_adc() | |
return flask.jsonify(reading) | |
@app.route("/api/gas/nh3") | |
def get_nh3(): | |
reading = enviro_reader.get_nh3() | |
return flask.jsonify(reading) | |
@app.route("/api/gas/no2") | |
def get_no2(): | |
reading = enviro_reader.get_no2() | |
return flask.jsonify(reading) | |
@app.route("/api/gas/co") | |
def get_co(): | |
reading = enviro_reader.get_co() | |
return flask.jsonify(reading) | |
@app.route("/api/light/all") | |
def get_light(): | |
reading = enviro_reader.get_light() | |
return flask.jsonify(reading) | |
@app.route("/api/light/lux") | |
def get_lux(): | |
reading = enviro_reader.get_lux() | |
return flask.jsonify(reading) | |
@app.route("/api/light/proximity") | |
def get_proximity(): | |
reading = enviro_reader.get_proximity() | |
return flask.jsonify(reading) | |
@app.route("/api/particulates/all") | |
def get_particles(): | |
reading = enviro_reader.get_particulates() | |
return flask.jsonify(reading) | |
@app.route("/api/particulates/pm1.0") | |
def get_pm_1_0(): | |
reading = enviro_reader.get_pm(1.0) | |
return flask.jsonify(reading) | |
@app.route("/api/particulates/pm2.5") | |
def get_pm_2_5(): | |
reading = enviro_reader.get_pm(2.5) | |
return flask.jsonify(reading) | |
@app.route("/api/particulates/pm10") | |
def get_pm_10(): | |
reading = enviro_reader.get_pm(10) | |
return flask.jsonify(reading) | |
@app.route("/api/particulates/air/pm0.3") | |
def get_air_pm_0_3(): | |
reading = enviro_reader.get_air_pm(0.3) | |
return flask.jsonify(reading) | |
@app.route("/api/particulates/air/pm0.5") | |
def get_air_pm_0_5(): | |
reading = enviro_reader.get_air_pm(0.5) | |
return flask.jsonify(reading) | |
@app.route("/api/particulates/air/pm1.0") | |
def get_air_pm_1_0(): | |
reading = enviro_reader.get_air_pm(1.0) | |
return flask.jsonify(reading) | |
@app.route("/api/particulates/air/pm2.5") | |
def get_air_pm_2_5(): | |
reading = enviro_reader.get_air_pm(2.5) | |
return flask.jsonify(reading) | |
@app.route("/api/particulates/air/pm5") | |
def get_air_pm_5(): | |
reading = enviro_reader.get_air_pm(5) | |
return flask.jsonify(reading) | |
@app.route("/api/particulates/air/pm10") | |
def get_air_pm_10(): | |
reading = enviro_reader.get_air_pm(10) | |
return flask.jsonify(reading) | |
@app.route("/api/weather/all") | |
def get_weather(): | |
reading = enviro_reader.get_weather() | |
return flask.jsonify(reading) | |
@app.route("/api/weather/humidity") | |
def get_humidity(): | |
reading = enviro_reader.get_humidity() | |
return flask.jsonify(reading) | |
@app.route("/api/weather/pressure") | |
def get_pressure(): | |
reading = enviro_reader.get_pressure() | |
return flask.jsonify(reading) | |
@app.route("/api/weather/temperature") | |
def get_temperature(): | |
reading = enviro_reader.get_temperature() | |
return flask.jsonify(reading) | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0', port=99, debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Creates a unified reader to get data for all Enviro+ metrics.""" | |
from bme280 import BME280 | |
from enviroplus import gas | |
import ltr559 as light | |
from pms5003 import PMS5003 | |
from smbus2 import SMBus | |
class EnviroReader(object): | |
"""Reads all data from Enviro+ component. | |
Methods: | |
get_all(): Reads all data. | |
get_gas(): Gets all gas data. | |
get_adc(): Gets adc data. | |
get_nh3(): Gets nh3 data. | |
get_no2(): Gets no2 data. | |
get_co(): Gets co data. | |
get_light(): Gets all light data. | |
get_lux(): Gets lux data. | |
get_proximity(): Get proximity data. | |
get_particulates(): Gets particle data. | |
get_pm(pm_size): # Supported: 1.0, 2.5, 10. | |
get_air_pm(pm_size): # Supported: 0.3, 0.5, 1.0, 2.5, 5, 10. | |
get_weather(): Gets weather data. | |
get_humidity(): Gets humidity data. | |
get_pressure(): Gets pressure data. | |
get_temperature(): Gets temperature data. | |
""" | |
def __init__(self): | |
"""Initializes reader and all required sensors.""" | |
self._bus = SMBus(1) | |
self._gas = gas | |
self._gas.enable_adc() | |
self._light = light | |
self._light.setup() | |
self._particulates = PMS5003() | |
self._particulates.setup() | |
self._weather = BME280(i2c_dev=self._bus) | |
self._weather.setup() | |
def get_all(self): | |
return { | |
'gas': self.get_gas(), | |
'light': self.get_light(), | |
'particulates': self.get_particulates(), | |
'weather': self.get_weather(), | |
} | |
def get_gas(self): | |
return { | |
'adc': self._gas.read_adc(), # Analog to digital converter. | |
'nh3': self._gas.read_nh3(), # Ammonia. | |
'no2': self._gas.read_oxidising(), # Nitrogen dioxide. | |
'co': self._gas.read_reducing(), # Carbon monoxide. | |
} | |
def get_adc(self): # Analog to digital converter. | |
return {'adc': self._gas.read_adc()} | |
def get_nh3(self): # Ammonia. | |
return {'nh3': self._gas.read_nh3()} | |
def get_no2(self): # Nitrogen dioxide. | |
return {'no2': self._gas.read_oxidising()} | |
def get_co(self): # Carbon monoxide. | |
return {'co': self._gas.read_reducing()} | |
def get_light(self): | |
self._light.update_sensor() | |
return { | |
'lux': self._light.get_lux(), | |
'proximity': self._light.get_proximity(), | |
} | |
def get_lux(self): | |
self._light.update_sensor() | |
return {'lux': self._light.get_lux()} | |
def get_proximity(self): | |
self._light.update_sensor() | |
return {'proximity': self._light.get_proximity()} | |
def get_particulates(self): | |
reading = self._particulates.read() | |
return { | |
'pm1.0': reading.pm_ug_per_m3(1.0), | |
'pm2.5': reading.pm_ug_per_m3(2.5), | |
'pm10': reading.pm_ug_per_m3(10), | |
'air_pm0.3': reading.pm_per_1l_air(0.3), | |
'air_pm0.5': reading.pm_per_1l_air(0.5), | |
'air_pm1.0': reading.pm_per_1l_air(1.0), | |
'air_pm2.5': reading.pm_per_1l_air(2.5), | |
'air_pm5': reading.pm_per_1l_air(5), | |
'air_pm10': reading.pm_per_1l_air(10), | |
} | |
def get_pm(self, pm_size): # Supported: 1.0, 2.5, 10. | |
reading = self._particulates.read() | |
dict_key = 'pm{pm_size}'.format(pm_size=pm_size) | |
return {dict_key: reading.pm_ug_per_m3(pm_size)} | |
def get_air_pm(self, pm_size): # Supported: 0.3, 0.5, 1.0, 2.5, 5, 10. | |
reading = self._particulates.read() | |
dict_key = 'air_pm{pm_size}'.format(pm_size=pm_size) | |
return {dict_key: reading.pm_per_1l_air(pm_size)} | |
def get_weather(self): | |
self._weather.update_sensor() | |
return { # TODO: compensate measurements. | |
'humidity': self._weather.get_humidity(), | |
'pressure': self._weather.get_pressure(), | |
'temperature': self._weather.get_temperature(), | |
} | |
def get_humidity(self): | |
return {'humidity': self._weather.get_humidity()} | |
def get_pressure(self): | |
return {'pressure': self._weather.get_pressure()} | |
def get_temperature(self): | |
return {'temperature': self._weather.get_temperature()} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################################ | |
# This is not required for the REST API to run. | |
# This is a .yaml configuration file for Home-Assistant (Hassio). | |
################################################################################ | |
- platform: rest | |
name: enviropi | |
resource: # Enter web-server URL. | |
scan_interval: 300 | |
json_attributes: | |
- gas | |
- light | |
- particulates | |
- weather | |
value_template: 'OK' | |
- platform: template | |
sensors: | |
living_room_oxidising: | |
friendly_name: 'Living Room Oxidising Gas' | |
value_template: '{{ states.sensor.enviropi.attributes["gas"]["no2"] }}' | |
unit_of_measurement: 'Ohms' | |
living_room_reducing: | |
friendly_name: 'Living Room Carbon Monoxide Gas' | |
value_template: '{{ states.sensor.enviropi.attributes["gas"]["co"] }}' | |
unit_of_measurement: 'Ohms' | |
living_room_ammonia: | |
friendly_name: 'Living Room Ammonia Gas' | |
value_template: '{{ states.sensor.enviropi.attributes["gas"]["nh3"] }}' | |
unit_of_measurement: 'Ohms' | |
living_room_illuminance: | |
friendly_name: 'Living Room Illuminance' | |
value_template: '{{ states.sensor.enviropi.attributes["light"]["lux"] }}' | |
device_class: illuminance | |
unit_of_measurement: 'lx' | |
living_room_pm_1p0: | |
friendly_name: 'Living Room PM1.0' | |
value_template: '{{ states.sensor.enviropi.attributes["particulates"]["pm1.0"] }}' | |
unit_of_measurement: 'ug/m3' | |
living_room_pm_2p5: | |
friendly_name: 'Living Room PM2.5' | |
value_template: '{{ states.sensor.enviropi.attributes["particulates"]["pm2.5"] }}' | |
unit_of_measurement: 'ug/m3' | |
living_room_pm_10: | |
friendly_name: 'Living Room PM10' | |
value_template: '{{ states.sensor.enviropi.attributes["particulates"]["pm10"] }}' | |
unit_of_measurement: 'ug/m3' | |
living_room_temperature: | |
friendly_name: 'Living Room Temperature' | |
value_template: '{{ states.sensor.enviropi.attributes["weather"]["temperature"] }}' | |
device_class: temperature | |
unit_of_measurement: '°C' | |
living_room_humidity: | |
friendly_name: 'Living Room Humidity' | |
value_template: '{{ states.sensor.enviropi.attributes["weather"]["humidity"] }}' | |
device_class: humidity | |
unit_of_measurement: '%' | |
living_room_pressure: | |
friendly_name: 'Living Room Pressure' | |
value_template: '{{ states.sensor.enviropi.attributes["weather"]["pressure"] }}' | |
device_class: pressure | |
unit_of_measurement: 'hPa' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment