From 1a99eccf5c2df4996e3ffa47631348221cb48045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Nenz=C3=A9n?= Date: Tue, 7 May 2019 00:57:47 +0200 Subject: [PATCH 01/16] Adds integration for Plaato Airlock --- .../components/plaato/.translations/sv.json | 18 +++ homeassistant/components/plaato/__init__.py | 135 +++++++++++++++++ homeassistant/components/plaato/manifest.json | 8 + homeassistant/components/plaato/sensor.py | 142 ++++++++++++++++++ homeassistant/components/plaato/strings.json | 18 +++ 5 files changed, 321 insertions(+) create mode 100644 homeassistant/components/plaato/.translations/sv.json create mode 100644 homeassistant/components/plaato/__init__.py create mode 100644 homeassistant/components/plaato/manifest.json create mode 100644 homeassistant/components/plaato/sensor.py create mode 100644 homeassistant/components/plaato/strings.json diff --git a/homeassistant/components/plaato/.translations/sv.json b/homeassistant/components/plaato/.translations/sv.json new file mode 100644 index 00000000000000..a4f18c637a9697 --- /dev/null +++ b/homeassistant/components/plaato/.translations/sv.json @@ -0,0 +1,18 @@ +{ + "config": { + "abort": { + "not_internet_accessible": "Din Home Assistant instans m\u00e5ste vara \u00e5tkomlig ifr\u00e5n internet f\u00f6r att ta emot meddelanden ifr\u00e5n Plaato Airlock.", + "one_instance_allowed": "Endast en enda instans \u00e4r n\u00f6dv\u00e4ndig." + }, + "create_entry": { + "default": "F\u00f6r att skicka h\u00e4ndelser till Home Assistant m\u00e5ste du konfigurera webhook funktionen i Plaato.\n\n Fyll i f\u00f6ljande information:\n \n- URL: `{webhook_url}`\n- Method: POST\n\nSe [dokumentation]({docs_url}) om hur du konfigurerar detta f\u00f6r mer information." + }, + "step": { + "user": { + "description": "\u00c4r du s\u00e4ker p\u00e5 att du vill konfigurera Plaato Airlock?", + "title": "Konfigurera Plaato Airlock" + } + }, + "title": "Plaato Airlock" + } +} \ No newline at end of file diff --git a/homeassistant/components/plaato/__init__.py b/homeassistant/components/plaato/__init__.py new file mode 100644 index 00000000000000..5528e6461bf37d --- /dev/null +++ b/homeassistant/components/plaato/__init__.py @@ -0,0 +1,135 @@ +"""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 ( + CONF_WEBHOOK_ID, HTTP_OK, HTTP_UNPROCESSABLE_ENTITY, + TEMP_CELSIUS, TEMP_FAHRENHEIT, VOLUME_GALLONS, VOLUME_LITERS) +from homeassistant.helpers import config_entry_flow +import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.dispatcher import async_dispatcher_send + +_LOGGER = logging.getLogger(__name__) + +DOMAIN = 'plaato' +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) + +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) + + +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] + hass.components.webhook.async_register( + DOMAIN, 'Plaato', webhook_id, handle_webhook) + + hass.async_create_task( + hass.config_entries.async_forward_entry_setup(entry, SENSOR) + ) + + return True + + +async def async_unload_entry(hass, entry): + """Unload a config entry.""" + hass.components.webhook.async_unregister(entry.data[CONF_WEBHOOK_ID]) + + 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: + return web.Response( + body=error.error_message, + status=HTTP_UNPROCESSABLE_ENTITY + ) + + 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), + ATTR_VOLUME_UNIT: data.get(ATTR_VOLUME_UNIT) + } + + 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), + ATTR_BUBBLES: data.get(ATTR_BUBBLES) + } + + hass.data[DOMAIN][device_id] = { + PLAATO_DEVICE_ATTRS: attrs, + PLAATO_DEVICE_SENSORS: sensors + } + + async_dispatcher_send(hass, SENSOR_UPDATE, device_id) + + return web.Response( + text="Saving status for {}".format(device_id), status=HTTP_OK) + + +def _device_id(data): + """Return name of device sensor.""" + return "{}_{}".format(data.get(ATTR_DEVICE_NAME), data.get(ATTR_DEVICE_ID)) + + +config_entry_flow.register_webhook_flow( + DOMAIN, + 'Webhook', + { + 'docs_url': 'https://www.home-assistant.io/components/plaato/' + } +) diff --git a/homeassistant/components/plaato/manifest.json b/homeassistant/components/plaato/manifest.json new file mode 100644 index 00000000000000..322205fa800571 --- /dev/null +++ b/homeassistant/components/plaato/manifest.json @@ -0,0 +1,8 @@ +{ + "domain": "plaato", + "name": "Plaato Airlock", + "documentation": "https://www.home-assistant.io/components/plaato", + "dependencies": ["webhook"], + "codeowners": ["@JohNan"], + "requirements": [] +} \ No newline at end of file diff --git a/homeassistant/components/plaato/sensor.py b/homeassistant/components/plaato/sensor.py new file mode 100644 index 00000000000000..0f36821f90ed86 --- /dev/null +++ b/homeassistant/components/plaato/sensor.py @@ -0,0 +1,142 @@ +"""Support for Plaato Airlock sensors.""" + +import logging + +from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers.entity import Entity + +from . import ( + ATTR_ABV, ATTR_BATCH_VOLUME, ATTR_BPM, ATTR_CO2_VOLUME, ATTR_TEMP, + ATTR_TEMP_UNIT, ATTR_VOLUME_UNIT, DOMAIN as PLAATO_DOMAIN, + PLAATO_DEVICE_ATTRS, PLAATO_DEVICE_SENSORS, SENSOR as SENSOR_DOMAIN, + SENSOR_UPDATE) + +_LOGGER = logging.getLogger(__name__) + +DATA_KEY = '{}.{}'.format(PLAATO_DOMAIN, SENSOR_DOMAIN) + + +async def async_setup_platform(hass, config, async_add_entities, + discovery_info=None): + """Set up the Plaato sensor.""" + + +async def async_setup_entry(hass, config_entry, async_add_entities): + """Set up Plaato from a config entry.""" + devices = {} + + def get_device(device_id): + """Get a device.""" + return hass.data[PLAATO_DOMAIN].get(device_id, False) + + def get_device_sensors(device_id): + """Get device sensors.""" + return hass.data[PLAATO_DOMAIN].get(device_id)\ + .get(PLAATO_DEVICE_SENSORS) + + async def _update_sensor(device_id): + """Update/Create the sensors.""" + if device_id not in devices and get_device(device_id): + entities = [] + sensors = get_device_sensors(device_id) + + for sensor_type, value in sensors.items(): + entities.append(PlaatoSensor(device_id, sensor_type)) + + devices[device_id] = entities + + async_add_entities(entities, True) + else: + for entity in devices[device_id]: + entity.async_schedule_update_ha_state() + + hass.data[DATA_KEY] = async_dispatcher_connect( + hass, SENSOR_UPDATE, _update_sensor + ) + + return True + + +class PlaatoSensor(Entity): + """Representation of a Sensor.""" + + def __init__(self, device_id, sensor_type): + """Initialize the sensor.""" + self._device_id = device_id + self._type = sensor_type + self._state = 0 + self._name = "{} {}".format(device_id, sensor_type) + self._attributes = None + + @property + def name(self): + """Return the name of the sensor.""" + return "{} {}".format(PLAATO_DOMAIN, self._name) + + @property + def unique_id(self): + """Return the unique ID of this sensor.""" + return "{}_{}".format(self._device_id, self._type) + + @property + def device_info(self): + """Get device info.""" + return { + 'identifiers': { + (PLAATO_DOMAIN, self._device_id) + }, + 'name': self._device_id, + 'manufacturer': 'Plaato', + 'model': 'Airlock' + } + + def get_sensors(self): + """Get device sensors.""" + return self.hass.data[PLAATO_DOMAIN].get(self._device_id)\ + .get(PLAATO_DEVICE_SENSORS, False) + + def get_sensors_unit_of_measurement(self, sensor_type): + """Get unit of measurement for sensor of type.""" + return self.hass.data[PLAATO_DOMAIN].get(self._device_id)\ + .get(PLAATO_DEVICE_ATTRS, []).get(sensor_type, '') + + @property + def state(self): + """Return the state of the sensor.""" + return self._state + + @property + def device_state_attributes(self): + """Return the state attributes of the monitored installation.""" + if self._attributes is not None: + return self._attributes + + @property + def unit_of_measurement(self): + """Return the unit of measurement.""" + if self._type == ATTR_TEMP: + return self.get_sensors_unit_of_measurement(ATTR_TEMP_UNIT) + elif self._type == ATTR_BATCH_VOLUME or self._type == ATTR_CO2_VOLUME: + return self.get_sensors_unit_of_measurement(ATTR_VOLUME_UNIT) + elif self._type == ATTR_BPM: + return 'bpm' + elif self._type == ATTR_ABV: + return '%' + + return '' + + async def async_update(self): + """Fetch new state data for the sensor.""" + sensors = self.get_sensors() + if sensors is False: + _LOGGER.debug("Device with name %s has no sensors.", self.name) + return + + if self._type == ATTR_ABV: + self._state = round(sensors.get(self._type), 2) + elif self._type == ATTR_TEMP: + self._state = round(sensors.get(self._type), 1) + elif self._type == ATTR_CO2_VOLUME: + self._state = round(sensors.get(self._type), 2) + else: + self._state = sensors.get(self._type) diff --git a/homeassistant/components/plaato/strings.json b/homeassistant/components/plaato/strings.json new file mode 100644 index 00000000000000..ee99da0c8b1cd7 --- /dev/null +++ b/homeassistant/components/plaato/strings.json @@ -0,0 +1,18 @@ +{ + "config": { + "title": "Plaato Airlock", + "step": { + "user": { + "title": "Set up the Plaato Webhook", + "description": "Are you sure you want to set up the Plaato Airlock?" + } + }, + "abort": { + "one_instance_allowed": "Only a single instance is necessary.", + "not_internet_accessible": "Your Home Assistant instance needs to be accessible from the internet to receive messages from Plaato Airlock." + }, + "create_entry": { + "default": "To send events to Home Assistant, you will need to setup the webhook feature in Plaato Airlock.\n\nFill in the following info:\n\n- URL: `{webhook_url}`\n- Method: POST\n\nSee [the documentation]({docs_url}) for further details." + } + } +} \ No newline at end of file From e28502e7396576b98816cfc58d94845527b186ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Nenz=C3=A9n?= Date: Tue, 7 May 2019 01:08:58 +0200 Subject: [PATCH 02/16] Updates codeowners and coveragerc --- .coveragerc | 1 + CODEOWNERS | 1 + 2 files changed, 2 insertions(+) diff --git a/.coveragerc b/.coveragerc index 7e618b9d4b36e3..c8213378e91889 100644 --- a/.coveragerc +++ b/.coveragerc @@ -451,6 +451,7 @@ omit = homeassistant/components/ping/device_tracker.py homeassistant/components/pioneer/media_player.py homeassistant/components/pjlink/media_player.py + homeassistant/components/plaato/* homeassistant/components/plex/media_player.py homeassistant/components/plex/sensor.py homeassistant/components/plum_lightpad/* diff --git a/CODEOWNERS b/CODEOWNERS index a6b1b44e34cf51..fb6b204253b8e5 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -191,6 +191,7 @@ homeassistant/components/panel_iframe/* @home-assistant/frontend homeassistant/components/persistent_notification/* @home-assistant/core homeassistant/components/philips_js/* @elupus homeassistant/components/pi_hole/* @fabaff +homeassistant/components/plaato/* @JohNan homeassistant/components/plant/* @ChristianKuehnel homeassistant/components/point/* @fredrike homeassistant/components/ps4/* @ktnrg45 From e4e5be0baa8b669f6c647349ebd1c8414dce14d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Nenz=C3=A9n?= Date: Tue, 7 May 2019 02:02:11 +0200 Subject: [PATCH 03/16] Fixes lint errors --- homeassistant/components/plaato/sensor.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/plaato/sensor.py b/homeassistant/components/plaato/sensor.py index 0f36821f90ed86..2a739dbba21550 100644 --- a/homeassistant/components/plaato/sensor.py +++ b/homeassistant/components/plaato/sensor.py @@ -40,7 +40,7 @@ async def _update_sensor(device_id): entities = [] sensors = get_device_sensors(device_id) - for sensor_type, value in sensors.items(): + for sensor_type in sensors.items(): entities.append(PlaatoSensor(device_id, sensor_type)) devices[device_id] = entities @@ -122,8 +122,8 @@ def unit_of_measurement(self): return 'bpm' elif self._type == ATTR_ABV: return '%' - - return '' + else: + return '' async def async_update(self): """Fetch new state data for the sensor.""" From 573276a0d0f84175283ce906de8e856cb9e4b88a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Nenz=C3=A9n?= Date: Tue, 7 May 2019 06:37:03 +0200 Subject: [PATCH 04/16] Fixers lint check error --- homeassistant/components/plaato/sensor.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/plaato/sensor.py b/homeassistant/components/plaato/sensor.py index 2a739dbba21550..9c674ef86acc0e 100644 --- a/homeassistant/components/plaato/sensor.py +++ b/homeassistant/components/plaato/sensor.py @@ -116,14 +116,14 @@ def unit_of_measurement(self): """Return the unit of measurement.""" if self._type == ATTR_TEMP: return self.get_sensors_unit_of_measurement(ATTR_TEMP_UNIT) - elif self._type == ATTR_BATCH_VOLUME or self._type == ATTR_CO2_VOLUME: + if self._type == ATTR_BATCH_VOLUME or self._type == ATTR_CO2_VOLUME: return self.get_sensors_unit_of_measurement(ATTR_VOLUME_UNIT) - elif self._type == ATTR_BPM: + if self._type == ATTR_BPM: return 'bpm' - elif self._type == ATTR_ABV: + if self._type == ATTR_ABV: return '%' - else: - return '' + + return '' async def async_update(self): """Fetch new state data for the sensor.""" From 14f7ed2c233dd9ed31c638c462e5ef1ddd2710d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Nenz=C3=A9n?= Date: Wed, 8 May 2019 19:13:51 +0200 Subject: [PATCH 05/16] Removed sv translation file --- .../components/plaato/.translations/sv.json | 18 ------------------ 1 file changed, 18 deletions(-) delete mode 100644 homeassistant/components/plaato/.translations/sv.json diff --git a/homeassistant/components/plaato/.translations/sv.json b/homeassistant/components/plaato/.translations/sv.json deleted file mode 100644 index a4f18c637a9697..00000000000000 --- a/homeassistant/components/plaato/.translations/sv.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "config": { - "abort": { - "not_internet_accessible": "Din Home Assistant instans m\u00e5ste vara \u00e5tkomlig ifr\u00e5n internet f\u00f6r att ta emot meddelanden ifr\u00e5n Plaato Airlock.", - "one_instance_allowed": "Endast en enda instans \u00e4r n\u00f6dv\u00e4ndig." - }, - "create_entry": { - "default": "F\u00f6r att skicka h\u00e4ndelser till Home Assistant m\u00e5ste du konfigurera webhook funktionen i Plaato.\n\n Fyll i f\u00f6ljande information:\n \n- URL: `{webhook_url}`\n- Method: POST\n\nSe [dokumentation]({docs_url}) om hur du konfigurerar detta f\u00f6r mer information." - }, - "step": { - "user": { - "description": "\u00c4r du s\u00e4ker p\u00e5 att du vill konfigurera Plaato Airlock?", - "title": "Konfigurera Plaato Airlock" - } - }, - "title": "Plaato Airlock" - } -} \ No newline at end of file From c7ca6962a532791cb4c70f5955dd6614a4f70692 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Nenz=C3=A9n?= Date: Thu, 9 May 2019 08:32:15 +0200 Subject: [PATCH 06/16] Adds en translation file --- .../components/plaato/.translations/en.json | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 homeassistant/components/plaato/.translations/en.json diff --git a/homeassistant/components/plaato/.translations/en.json b/homeassistant/components/plaato/.translations/en.json new file mode 100644 index 00000000000000..6d3aa2c59c43eb --- /dev/null +++ b/homeassistant/components/plaato/.translations/en.json @@ -0,0 +1,18 @@ +{ + "config": { + "abort": { + "not_internet_accessible": "Your Home Assistant instance needs to be accessible from the internet to receive messages from Plaato Airlock.", + "one_instance_allowed": "Only a single instance is necessary." + }, + "create_entry": { + "default": "To send events to Home Assistant, you will need to setup the webhook feature in Plaato Airlock.\n\nFill in the following info:\n\n- URL: `{webhook_url}`\n- Method: POST\n\nSee [the documentation]({docs_url}) for further details." + }, + "step": { + "user": { + "description": "Are you sure you want to set up the Plaato Airlock?", + "title": "Set up the Plaato Webhook" + } + }, + "title": "Plaato Airlock" + } +} \ No newline at end of file From 39e2553d7bf16ca091b4d6bd5ef253a9ce1a3f97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Nenz=C3=A9n?= Date: Tue, 14 May 2019 16:45:23 +0200 Subject: [PATCH 07/16] Sets config flow to true in manifest --- homeassistant/components/plaato/manifest.json | 1 + 1 file changed, 1 insertion(+) diff --git a/homeassistant/components/plaato/manifest.json b/homeassistant/components/plaato/manifest.json index 322205fa800571..cd6111ba9da789 100644 --- a/homeassistant/components/plaato/manifest.json +++ b/homeassistant/components/plaato/manifest.json @@ -1,6 +1,7 @@ { "domain": "plaato", "name": "Plaato Airlock", + "config_flow": true, "documentation": "https://www.home-assistant.io/components/plaato", "dependencies": ["webhook"], "codeowners": ["@JohNan"], From 512e444b9b78367eb6ab1a9ab324b893e4e2ab1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Nenz=C3=A9n?= Date: Tue, 14 May 2019 16:59:24 +0200 Subject: [PATCH 08/16] Moves config flow and domain to seperate files --- homeassistant/components/plaato/__init__.py | 11 +---------- homeassistant/components/plaato/config_flow.py | 11 +++++++++++ homeassistant/components/plaato/const.py | 3 +++ 3 files changed, 15 insertions(+), 10 deletions(-) create mode 100644 homeassistant/components/plaato/config_flow.py create mode 100644 homeassistant/components/plaato/const.py diff --git a/homeassistant/components/plaato/__init__.py b/homeassistant/components/plaato/__init__.py index 5528e6461bf37d..9c43518e792182 100644 --- a/homeassistant/components/plaato/__init__.py +++ b/homeassistant/components/plaato/__init__.py @@ -8,13 +8,12 @@ from homeassistant.const import ( CONF_WEBHOOK_ID, HTTP_OK, HTTP_UNPROCESSABLE_ENTITY, TEMP_CELSIUS, TEMP_FAHRENHEIT, VOLUME_GALLONS, VOLUME_LITERS) -from homeassistant.helpers import config_entry_flow import homeassistant.helpers.config_validation as cv from homeassistant.helpers.dispatcher import async_dispatcher_send +from .const import DOMAIN _LOGGER = logging.getLogger(__name__) -DOMAIN = 'plaato' DEPENDENCIES = ['webhook'] PLAATO_DEVICE_SENSORS = 'sensors' @@ -125,11 +124,3 @@ def _device_id(data): """Return name of device sensor.""" return "{}_{}".format(data.get(ATTR_DEVICE_NAME), data.get(ATTR_DEVICE_ID)) - -config_entry_flow.register_webhook_flow( - DOMAIN, - 'Webhook', - { - 'docs_url': 'https://www.home-assistant.io/components/plaato/' - } -) diff --git a/homeassistant/components/plaato/config_flow.py b/homeassistant/components/plaato/config_flow.py new file mode 100644 index 00000000000000..e150ad2aa05f5a --- /dev/null +++ b/homeassistant/components/plaato/config_flow.py @@ -0,0 +1,11 @@ +"""Config flow for GPSLogger.""" +from homeassistant.helpers import config_entry_flow +from .const import DOMAIN + +config_entry_flow.register_webhook_flow( + DOMAIN, + 'Webhook', + { + 'docs_url': 'https://www.home-assistant.io/components/plaato/' + } +) \ No newline at end of file diff --git a/homeassistant/components/plaato/const.py b/homeassistant/components/plaato/const.py new file mode 100644 index 00000000000000..f683ddb664ce8d --- /dev/null +++ b/homeassistant/components/plaato/const.py @@ -0,0 +1,3 @@ +"""Const for GPSLogger.""" + +DOMAIN = 'plaato' From 7d3585f17b0cc4b51bb24b1cd6a349c474837f29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Nenz=C3=A9n?= Date: Tue, 14 May 2019 17:04:49 +0200 Subject: [PATCH 09/16] Fixes lint errors --- homeassistant/components/plaato/__init__.py | 1 - homeassistant/components/plaato/config_flow.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/homeassistant/components/plaato/__init__.py b/homeassistant/components/plaato/__init__.py index 9c43518e792182..33787203769720 100644 --- a/homeassistant/components/plaato/__init__.py +++ b/homeassistant/components/plaato/__init__.py @@ -123,4 +123,3 @@ async def handle_webhook(hass, webhook_id, request): def _device_id(data): """Return name of device sensor.""" return "{}_{}".format(data.get(ATTR_DEVICE_NAME), data.get(ATTR_DEVICE_ID)) - diff --git a/homeassistant/components/plaato/config_flow.py b/homeassistant/components/plaato/config_flow.py index e150ad2aa05f5a..c3f9279df05ffc 100644 --- a/homeassistant/components/plaato/config_flow.py +++ b/homeassistant/components/plaato/config_flow.py @@ -8,4 +8,4 @@ { 'docs_url': 'https://www.home-assistant.io/components/plaato/' } -) \ No newline at end of file +) From 9579a2a9a3bead1d8abb1eece4ef1156c169011a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Nenz=C3=A9n?= Date: Wed, 15 May 2019 08:59:58 +0200 Subject: [PATCH 10/16] Runs hassfest to regenerate config_flows.py --- homeassistant/generated/config_flows.py | 1 + 1 file changed, 1 insertion(+) diff --git a/homeassistant/generated/config_flows.py b/homeassistant/generated/config_flows.py index 296c620cd7de31..4c7d77e0dabbf5 100644 --- a/homeassistant/generated/config_flows.py +++ b/homeassistant/generated/config_flows.py @@ -37,6 +37,7 @@ "nest", "openuv", "owntracks", + "plaato", "point", "ps4", "rainmachine", From 97a817e0dee72ff9296fbc54b5d78e9f3477cf44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Nenz=C3=A9n?= Date: Thu, 13 Jun 2019 21:37:24 +0200 Subject: [PATCH 11/16] Adds should poll property and fixes for loop --- homeassistant/components/plaato/sensor.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/plaato/sensor.py b/homeassistant/components/plaato/sensor.py index 9c674ef86acc0e..e93e376dc7ade0 100644 --- a/homeassistant/components/plaato/sensor.py +++ b/homeassistant/components/plaato/sensor.py @@ -40,7 +40,7 @@ async def _update_sensor(device_id): entities = [] sensors = get_device_sensors(device_id) - for sensor_type in sensors.items(): + for sensor_type in sensors: entities.append(PlaatoSensor(device_id, sensor_type)) devices[device_id] = entities @@ -125,6 +125,11 @@ def unit_of_measurement(self): return '' + @property + def should_poll(self): + """Return the polling state.""" + return False + async def async_update(self): """Fetch new state data for the sensor.""" sensors = self.get_sensors() From bfde338afdf278da792a573fede55955af057269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Nenz=C3=A9n?= Date: Thu, 13 Jun 2019 21:50:12 +0200 Subject: [PATCH 12/16] Only log a warning when webhook data was broken --- homeassistant/components/plaato/__init__.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/plaato/__init__.py b/homeassistant/components/plaato/__init__.py index 33787203769720..5621b3eadf598d 100644 --- a/homeassistant/components/plaato/__init__.py +++ b/homeassistant/components/plaato/__init__.py @@ -84,10 +84,8 @@ async def handle_webhook(hass, webhook_id, request): try: data = WEBHOOK_SCHEMA(await request.json()) except vol.MultipleInvalid as error: - return web.Response( - body=error.error_message, - status=HTTP_UNPROCESSABLE_ENTITY - ) + _LOGGER.warning("An error occurred when parsing webhook data <%s>", error) + return device_id = _device_id(data) From d5c1740abe6111743fbc1985f9011a08b677cb55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Nenz=C3=A9n?= Date: Fri, 14 Jun 2019 08:09:21 +0200 Subject: [PATCH 13/16] Fixes static test failure --- homeassistant/components/plaato/__init__.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/plaato/__init__.py b/homeassistant/components/plaato/__init__.py index 5621b3eadf598d..af93cfd51722f4 100644 --- a/homeassistant/components/plaato/__init__.py +++ b/homeassistant/components/plaato/__init__.py @@ -6,7 +6,7 @@ from homeassistant.components.sensor import DOMAIN as SENSOR from homeassistant.const import ( - CONF_WEBHOOK_ID, HTTP_OK, HTTP_UNPROCESSABLE_ENTITY, + CONF_WEBHOOK_ID, HTTP_OK, TEMP_CELSIUS, TEMP_FAHRENHEIT, VOLUME_GALLONS, VOLUME_LITERS) import homeassistant.helpers.config_validation as cv from homeassistant.helpers.dispatcher import async_dispatcher_send @@ -84,7 +84,8 @@ async def handle_webhook(hass, webhook_id, request): try: data = WEBHOOK_SCHEMA(await request.json()) except vol.MultipleInvalid as error: - _LOGGER.warning("An error occurred when parsing webhook data <%s>", error) + _LOGGER.warning("An error occurred when parsing webhook data <%s>", + error) return device_id = _device_id(data) From 5b4ce0ffe49d08c595ffc1bb99e9d709f23a5546 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Nenz=C3=A9n?= Date: Tue, 18 Jun 2019 00:10:18 +0200 Subject: [PATCH 14/16] Moves state update from async_update to state prop --- homeassistant/components/plaato/sensor.py | 30 ++++++++++------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/plaato/sensor.py b/homeassistant/components/plaato/sensor.py index e93e376dc7ade0..307d6a28793cc9 100644 --- a/homeassistant/components/plaato/sensor.py +++ b/homeassistant/components/plaato/sensor.py @@ -103,7 +103,19 @@ def get_sensors_unit_of_measurement(self, sensor_type): @property def state(self): """Return the state of the sensor.""" - return self._state + sensors = self.get_sensors() + if sensors is False: + _LOGGER.debug("Device with name %s has no sensors.", self.name) + return 0 + + if self._type == ATTR_ABV: + return round(sensors.get(self._type), 2) + elif self._type == ATTR_TEMP: + return round(sensors.get(self._type), 1) + elif self._type == ATTR_CO2_VOLUME: + return round(sensors.get(self._type), 2) + else: + return sensors.get(self._type) @property def device_state_attributes(self): @@ -129,19 +141,3 @@ def unit_of_measurement(self): def should_poll(self): """Return the polling state.""" return False - - async def async_update(self): - """Fetch new state data for the sensor.""" - sensors = self.get_sensors() - if sensors is False: - _LOGGER.debug("Device with name %s has no sensors.", self.name) - return - - if self._type == ATTR_ABV: - self._state = round(sensors.get(self._type), 2) - elif self._type == ATTR_TEMP: - self._state = round(sensors.get(self._type), 1) - elif self._type == ATTR_CO2_VOLUME: - self._state = round(sensors.get(self._type), 2) - else: - self._state = sensors.get(self._type) From 2a3d94042da6133cf86bc7f619d779e0bc364b3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johan=20Nenz=C3=A9n?= Date: Tue, 18 Jun 2019 00:53:47 +0200 Subject: [PATCH 15/16] Unsubscribes the dispatch signal listener --- homeassistant/components/plaato/__init__.py | 2 ++ homeassistant/components/plaato/sensor.py | 7 ++----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/plaato/__init__.py b/homeassistant/components/plaato/__init__.py index af93cfd51722f4..9857ef47b1ce8c 100644 --- a/homeassistant/components/plaato/__init__.py +++ b/homeassistant/components/plaato/__init__.py @@ -33,6 +33,7 @@ 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, @@ -74,6 +75,7 @@ async def async_setup_entry(hass, entry): 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 diff --git a/homeassistant/components/plaato/sensor.py b/homeassistant/components/plaato/sensor.py index 307d6a28793cc9..4a55e10444d760 100644 --- a/homeassistant/components/plaato/sensor.py +++ b/homeassistant/components/plaato/sensor.py @@ -8,13 +8,10 @@ from . import ( ATTR_ABV, ATTR_BATCH_VOLUME, ATTR_BPM, ATTR_CO2_VOLUME, ATTR_TEMP, ATTR_TEMP_UNIT, ATTR_VOLUME_UNIT, DOMAIN as PLAATO_DOMAIN, - PLAATO_DEVICE_ATTRS, PLAATO_DEVICE_SENSORS, SENSOR as SENSOR_DOMAIN, - SENSOR_UPDATE) + PLAATO_DEVICE_ATTRS, PLAATO_DEVICE_SENSORS, SENSOR_DATA_KEY, SENSOR_UPDATE) _LOGGER = logging.getLogger(__name__) -DATA_KEY = '{}.{}'.format(PLAATO_DOMAIN, SENSOR_DOMAIN) - async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): @@ -50,7 +47,7 @@ async def _update_sensor(device_id): for entity in devices[device_id]: entity.async_schedule_update_ha_state() - hass.data[DATA_KEY] = async_dispatcher_connect( + hass.data[SENSOR_DATA_KEY] = async_dispatcher_connect( hass, SENSOR_UPDATE, _update_sensor ) From 44810ea1d433ca845efb28d4ee099ee9e2827d54 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 17 Jun 2019 20:07:28 -0700 Subject: [PATCH 16/16] Update sensor.py --- homeassistant/components/plaato/sensor.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/plaato/sensor.py b/homeassistant/components/plaato/sensor.py index 4a55e10444d760..6352c83712181d 100644 --- a/homeassistant/components/plaato/sensor.py +++ b/homeassistant/components/plaato/sensor.py @@ -107,12 +107,11 @@ def state(self): if self._type == ATTR_ABV: return round(sensors.get(self._type), 2) - elif self._type == ATTR_TEMP: + if self._type == ATTR_TEMP: return round(sensors.get(self._type), 1) - elif self._type == ATTR_CO2_VOLUME: + if self._type == ATTR_CO2_VOLUME: return round(sensors.get(self._type), 2) - else: - return sensors.get(self._type) + return sensors.get(self._type) @property def device_state_attributes(self):