From 6bd7005957a650d10e06e802409574c5e634baa6 Mon Sep 17 00:00:00 2001 From: ludeeus Date: Fri, 8 Mar 2019 22:24:47 +0100 Subject: [PATCH] Add binary_sensor --- README.md | 2 + custom_components/blueprint/__init__.py | 8 ++- custom_components/blueprint/binary_sensor.py | 64 ++++++++++++++++++++ custom_components/blueprint/const.py | 13 ++-- custom_components/blueprint/sensor.py | 11 ++-- 5 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 custom_components/blueprint/binary_sensor.py diff --git a/README.md b/README.md index f8fc5b5..4073edf 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ _Component to integrate with [blueprint][blueprint]._ Platform | Description -- | -- +`binary_sensor` | Show something `True` or `False` `sensor` | Show info from blueprint API. ![example][exampleimg] @@ -26,6 +27,7 @@ Using your HA configuration directory (folder) as a starting point you should no ```text custom_components/blueprint/__init__.py +custom_components/blueprint/binary_sensor.py custom_components/blueprint/const.py custom_components/blueprint/sensor.py ``` diff --git a/custom_components/blueprint/__init__.py b/custom_components/blueprint/__init__.py index da29127..44304b9 100644 --- a/custom_components/blueprint/__init__.py +++ b/custom_components/blueprint/__init__.py @@ -1,17 +1,21 @@ """ -Component to integrate with blueprint +Component to integrate with blueprint. For more details about this component, please refer to https://github.com/custom-components/blueprint """ import os +from datetime import timedelta import logging import requests from homeassistant.helpers import discovery +from homeassistant.util import Throttle from .const import ( DOMAIN_DATA, DOMAIN, ISSUE_URL, PLATFORMS, REQUIRED_FILES, STARTUP, URL, VERSION) +MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30) + _LOGGER = logging.getLogger(__name__) # pylint: disable=unused-argument @@ -39,7 +43,7 @@ async def async_setup(hass, config): ) return True - +@Throttle(MIN_TIME_BETWEEN_UPDATES) async def update_data(hass): """Update data.""" # This is where the main logic to update platform data goes. diff --git a/custom_components/blueprint/binary_sensor.py b/custom_components/blueprint/binary_sensor.py new file mode 100644 index 0000000..cbf8e2d --- /dev/null +++ b/custom_components/blueprint/binary_sensor.py @@ -0,0 +1,64 @@ +"""Binary ensor platform for blueprint.""" +from homeassistant.components.binary_sensor import BinarySensorDevice +from . import update_data +from .const import ( + BINARY_SENSOR_DEVICE_CLASS, DOMAIN as NAME, DOMAIN_DATA, SENSOR_ICON) + + +async def async_setup_platform( + hass, config, async_add_entities, discovery_info=None +): # pylint: disable=unused-argument + """Setup sensor platform.""" + async_add_entities([BlueprintSensor(hass)], True) + + +class BlueprintSensor(BinarySensorDevice): + """blueprint Sensor class.""" + + def __init__(self, hass): + self.hass = hass + self.attr = {} + self._status = False + + async def async_update(self): + """Update the sensor.""" + # Send update "signal" to the component + await update_data(self.hass) + + # Get new data (if any) + updated = self.hass.data[DOMAIN_DATA] + + # Check the data and update the value. + if updated.get("completed") is None: + self._status = self._status + else: + self._status = updated.get("completed") + + # Set/update attributes + self.attr['user_id'] = updated.get('userId') + self.attr['title'] = updated.get('title') + + @property + def name(self): + """Return the name of the sensor.""" + return NAME + + @property + def device_class(self): + """Return the class of this sensor.""" + return BINARY_SENSOR_DEVICE_CLASS + + @property + def is_on(self): + """Return true if the binary sensor is on.""" + return self._state + + @property + def icon(self): + """Return the icon of the sensor.""" + return SENSOR_ICON + + @property + def device_state_attributes(self): + """Return the state attributes.""" + return self.attr diff --git a/custom_components/blueprint/const.py b/custom_components/blueprint/const.py index fe7fb0d..2c6fe75 100644 --- a/custom_components/blueprint/const.py +++ b/custom_components/blueprint/const.py @@ -1,20 +1,20 @@ -"""Conststants.""" +"""Conststants for blueprint.""" # Base component constants DOMAIN = "blueprint" DOMAIN_DATA = "{}_data".format(DOMAIN) VERSION = "0.0.1" -PLATFORMS = ["sensor"] -REQUIRED_FILES = ["sensor.py", "const.py"] +PLATFORMS = ["binary_sensor", "sensor"] +REQUIRED_FILES = ["binary_sensor.py", "sensor.py", "const.py"] ISSUE_URL = "https://github.com/custom-components/blueprint/issues" STARTUP = """ ----------------------------------------------- +------------------------------------------------------------------- {name} Version: {version} This is a custom component If you have any issues with this you need to open an issue here: {issueurl} ----------------------------------------------- +------------------------------------------------------------------- """ # Operational @@ -22,3 +22,6 @@ URL = 'https://jsonplaceholder.typicode.com/todos/1' # Icons SENSOR_ICON = "mdi:format-quote-close" + +# Device classes +BINARY_SENSOR_DEVICE_CLASS = 'connectivity' diff --git a/custom_components/blueprint/sensor.py b/custom_components/blueprint/sensor.py index f27dc56..d4e5a01 100644 --- a/custom_components/blueprint/sensor.py +++ b/custom_components/blueprint/sensor.py @@ -1,4 +1,4 @@ -"""Sensor platform for blueprint""" +"""Sensor platform for blueprint.""" from homeassistant.helpers.entity import Entity from . import update_data from .const import DOMAIN as NAME, DOMAIN_DATA, SENSOR_ICON @@ -27,12 +27,11 @@ class BlueprintSensor(Entity): # Get new data (if any) updated = self.hass.data[DOMAIN_DATA] - # Check if there is data + # Check the data and update the value. if updated.get("title") is None: - updated = self._state - - # Set/update the state, and make sure it looks good by capitalizing it - self._state = updated.capitalize() + self._state = self._status + else: + self._state = updated.get("title") # Set/update attributes self.attr['user_id'] = updated.get('userId')