2019-06-18 03:23:59 +00:00
|
|
|
"""Support for Plaato Airlock."""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from aiohttp import web
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import DOMAIN as SENSOR
|
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_WEBHOOK_ID,
|
|
|
|
HTTP_OK,
|
|
|
|
TEMP_CELSIUS,
|
|
|
|
TEMP_FAHRENHEIT,
|
|
|
|
VOLUME_GALLONS,
|
|
|
|
VOLUME_LITERS,
|
|
|
|
)
|
2019-06-18 03:23:59 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEPENDENCIES = ["webhook"]
|
|
|
|
|
|
|
|
PLAATO_DEVICE_SENSORS = "sensors"
|
|
|
|
PLAATO_DEVICE_ATTRS = "attrs"
|
|
|
|
|
|
|
|
ATTR_DEVICE_ID = "device_id"
|
|
|
|
ATTR_DEVICE_NAME = "device_name"
|
|
|
|
ATTR_TEMP_UNIT = "temp_unit"
|
|
|
|
ATTR_VOLUME_UNIT = "volume_unit"
|
|
|
|
ATTR_BPM = "bpm"
|
|
|
|
ATTR_TEMP = "temp"
|
|
|
|
ATTR_SG = "sg"
|
|
|
|
ATTR_OG = "og"
|
|
|
|
ATTR_BUBBLES = "bubbles"
|
|
|
|
ATTR_ABV = "abv"
|
|
|
|
ATTR_CO2_VOLUME = "co2_volume"
|
|
|
|
ATTR_BATCH_VOLUME = "batch_volume"
|
|
|
|
|
|
|
|
SENSOR_UPDATE = "{}_sensor_update".format(DOMAIN)
|
|
|
|
SENSOR_DATA_KEY = "{}.{}".format(DOMAIN, SENSOR)
|
|
|
|
|
|
|
|
WEBHOOK_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(ATTR_DEVICE_NAME): cv.string,
|
|
|
|
vol.Required(ATTR_DEVICE_ID): cv.positive_int,
|
|
|
|
vol.Required(ATTR_TEMP_UNIT): vol.Any(TEMP_CELSIUS, TEMP_FAHRENHEIT),
|
|
|
|
vol.Required(ATTR_VOLUME_UNIT): vol.Any(VOLUME_LITERS, VOLUME_GALLONS),
|
|
|
|
vol.Required(ATTR_BPM): cv.positive_int,
|
|
|
|
vol.Required(ATTR_TEMP): vol.Coerce(float),
|
|
|
|
vol.Required(ATTR_SG): vol.Coerce(float),
|
|
|
|
vol.Required(ATTR_OG): vol.Coerce(float),
|
|
|
|
vol.Required(ATTR_ABV): vol.Coerce(float),
|
|
|
|
vol.Required(ATTR_CO2_VOLUME): vol.Coerce(float),
|
|
|
|
vol.Required(ATTR_BATCH_VOLUME): vol.Coerce(float),
|
|
|
|
vol.Required(ATTR_BUBBLES): cv.positive_int,
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2019-06-18 03:23:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass, hass_config):
|
|
|
|
"""Set up the Plaato component."""
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, entry):
|
|
|
|
"""Configure based on config entry."""
|
|
|
|
if DOMAIN not in hass.data:
|
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
|
|
|
|
webhook_id = entry.data[CONF_WEBHOOK_ID]
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.components.webhook.async_register(DOMAIN, "Plaato", webhook_id, handle_webhook)
|
2019-06-18 03:23:59 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.async_create_task(hass.config_entries.async_forward_entry_setup(entry, SENSOR))
|
2019-06-18 03:23:59 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass, entry):
|
|
|
|
"""Unload a config entry."""
|
|
|
|
hass.components.webhook.async_unregister(entry.data[CONF_WEBHOOK_ID])
|
|
|
|
hass.data[SENSOR_DATA_KEY]()
|
|
|
|
|
|
|
|
await hass.config_entries.async_forward_entry_unload(entry, SENSOR)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def handle_webhook(hass, webhook_id, request):
|
|
|
|
"""Handle incoming webhook from Plaato."""
|
|
|
|
try:
|
|
|
|
data = WEBHOOK_SCHEMA(await request.json())
|
|
|
|
except vol.MultipleInvalid as error:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning("An error occurred when parsing webhook data <%s>", error)
|
2019-06-18 03:23:59 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
device_id = _device_id(data)
|
|
|
|
|
|
|
|
attrs = {
|
|
|
|
ATTR_DEVICE_NAME: data.get(ATTR_DEVICE_NAME),
|
|
|
|
ATTR_DEVICE_ID: data.get(ATTR_DEVICE_ID),
|
|
|
|
ATTR_TEMP_UNIT: data.get(ATTR_TEMP_UNIT),
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_VOLUME_UNIT: data.get(ATTR_VOLUME_UNIT),
|
2019-06-18 03:23:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sensors = {
|
|
|
|
ATTR_TEMP: data.get(ATTR_TEMP),
|
|
|
|
ATTR_BPM: data.get(ATTR_BPM),
|
|
|
|
ATTR_SG: data.get(ATTR_SG),
|
|
|
|
ATTR_OG: data.get(ATTR_OG),
|
|
|
|
ATTR_ABV: data.get(ATTR_ABV),
|
|
|
|
ATTR_CO2_VOLUME: data.get(ATTR_CO2_VOLUME),
|
|
|
|
ATTR_BATCH_VOLUME: data.get(ATTR_BATCH_VOLUME),
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BUBBLES: data.get(ATTR_BUBBLES),
|
2019-06-18 03:23:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hass.data[DOMAIN][device_id] = {
|
|
|
|
PLAATO_DEVICE_ATTRS: attrs,
|
2019-07-31 19:25:30 +00:00
|
|
|
PLAATO_DEVICE_SENSORS: sensors,
|
2019-06-18 03:23:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async_dispatcher_send(hass, SENSOR_UPDATE, device_id)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
return web.Response(text="Saving status for {}".format(device_id), status=HTTP_OK)
|
2019-06-18 03:23:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _device_id(data):
|
|
|
|
"""Return name of device sensor."""
|
|
|
|
return "{}_{}".format(data.get(ATTR_DEVICE_NAME), data.get(ATTR_DEVICE_ID))
|