Add binary_sensor
This commit is contained in:
@ -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.
|
||||
|
64
custom_components/blueprint/binary_sensor.py
Normal file
64
custom_components/blueprint/binary_sensor.py
Normal 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
|
@ -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'
|
||||
|
@ -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')
|
||||
|
Reference in New Issue
Block a user