Add binary_sensor

This commit is contained in:
ludeeus
2019-03-08 22:24:47 +01:00
parent ce2d663821
commit 6bd7005957
5 changed files with 85 additions and 13 deletions

View File

@ -9,6 +9,7 @@ _Component to integrate with [blueprint][blueprint]._
Platform | Description Platform | Description
-- | -- -- | --
`binary_sensor` | Show something `True` or `False`
`sensor` | Show info from blueprint API. `sensor` | Show info from blueprint API.
![example][exampleimg] ![example][exampleimg]
@ -26,6 +27,7 @@ Using your HA configuration directory (folder) as a starting point you should no
```text ```text
custom_components/blueprint/__init__.py custom_components/blueprint/__init__.py
custom_components/blueprint/binary_sensor.py
custom_components/blueprint/const.py custom_components/blueprint/const.py
custom_components/blueprint/sensor.py custom_components/blueprint/sensor.py
``` ```

View File

@ -1,17 +1,21 @@
""" """
Component to integrate with blueprint Component to integrate with blueprint.
For more details about this component, please refer to For more details about this component, please refer to
https://github.com/custom-components/blueprint https://github.com/custom-components/blueprint
""" """
import os import os
from datetime import timedelta
import logging import logging
import requests import requests
from homeassistant.helpers import discovery from homeassistant.helpers import discovery
from homeassistant.util import Throttle
from .const import ( from .const import (
DOMAIN_DATA, DOMAIN, ISSUE_URL, PLATFORMS, REQUIRED_FILES, STARTUP, URL, DOMAIN_DATA, DOMAIN, ISSUE_URL, PLATFORMS, REQUIRED_FILES, STARTUP, URL,
VERSION) VERSION)
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
# pylint: disable=unused-argument # pylint: disable=unused-argument
@ -39,7 +43,7 @@ async def async_setup(hass, config):
) )
return True return True
@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def update_data(hass): async def update_data(hass):
"""Update data.""" """Update data."""
# This is where the main logic to update platform data goes. # This is where the main logic to update platform data goes.

View File

@ -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

View File

@ -1,20 +1,20 @@
"""Conststants.""" """Conststants for blueprint."""
# Base component constants # Base component constants
DOMAIN = "blueprint" DOMAIN = "blueprint"
DOMAIN_DATA = "{}_data".format(DOMAIN) DOMAIN_DATA = "{}_data".format(DOMAIN)
VERSION = "0.0.1" VERSION = "0.0.1"
PLATFORMS = ["sensor"] PLATFORMS = ["binary_sensor", "sensor"]
REQUIRED_FILES = ["sensor.py", "const.py"] REQUIRED_FILES = ["binary_sensor.py", "sensor.py", "const.py"]
ISSUE_URL = "https://github.com/custom-components/blueprint/issues" ISSUE_URL = "https://github.com/custom-components/blueprint/issues"
STARTUP = """ STARTUP = """
---------------------------------------------- -------------------------------------------------------------------
{name} {name}
Version: {version} Version: {version}
This is a custom component This is a custom component
If you have any issues with this you need to open an issue here: If you have any issues with this you need to open an issue here:
{issueurl} {issueurl}
---------------------------------------------- -------------------------------------------------------------------
""" """
# Operational # Operational
@ -22,3 +22,6 @@ URL = 'https://jsonplaceholder.typicode.com/todos/1'
# Icons # Icons
SENSOR_ICON = "mdi:format-quote-close" SENSOR_ICON = "mdi:format-quote-close"
# Device classes
BINARY_SENSOR_DEVICE_CLASS = 'connectivity'

View File

@ -1,4 +1,4 @@
"""Sensor platform for blueprint""" """Sensor platform for blueprint."""
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from . import update_data from . import update_data
from .const import DOMAIN as NAME, DOMAIN_DATA, SENSOR_ICON from .const import DOMAIN as NAME, DOMAIN_DATA, SENSOR_ICON
@ -27,12 +27,11 @@ class BlueprintSensor(Entity):
# Get new data (if any) # Get new data (if any)
updated = self.hass.data[DOMAIN_DATA] updated = self.hass.data[DOMAIN_DATA]
# Check if there is data # Check the data and update the value.
if updated.get("title") is None: if updated.get("title") is None:
updated = self._state self._state = self._status
else:
# Set/update the state, and make sure it looks good by capitalizing it self._state = updated.get("title")
self._state = updated.capitalize()
# Set/update attributes # Set/update attributes
self.attr['user_id'] = updated.get('userId') self.attr['user_id'] = updated.get('userId')