Updates, updates, more updates, worst commit messge ever!
This commit is contained in:
@ -7,26 +7,26 @@ https://github.com/custom-components/blueprint
|
||||
import os
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
import requests
|
||||
import voluptuous as vol
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers import discovery
|
||||
from homeassistant.util import Throttle
|
||||
from .const import (
|
||||
CONF_BINARY_SENSOR,
|
||||
CONF_ENABLED,
|
||||
CONF_NAME,
|
||||
CONF_PASSWORD,
|
||||
CONF_SENSOR,
|
||||
CONF_SWITCH,
|
||||
CONF_USERNAME,
|
||||
DEFAULT_NAME,
|
||||
DOMAIN_DATA,
|
||||
DOMAIN,
|
||||
ISSUE_URL,
|
||||
PLATFORMS,
|
||||
REQUIRED_FILES,
|
||||
STARTUP,
|
||||
URL,
|
||||
VERSION,
|
||||
CONF_BINARY_SENSOR,
|
||||
CONF_SENSOR,
|
||||
CONF_SWITCH,
|
||||
CONF_ENABLED,
|
||||
CONF_NAME,
|
||||
DEFAULT_NAME,
|
||||
)
|
||||
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
|
||||
@ -58,6 +58,8 @@ CONFIG_SCHEMA = vol.Schema(
|
||||
{
|
||||
DOMAIN: vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_USERNAME): cv.string,
|
||||
vol.Optional(CONF_PASSWORD): cv.string,
|
||||
vol.Optional(CONF_BINARY_SENSOR): vol.All(
|
||||
cv.ensure_list, [BINARY_SENSOR_SCHEMA]
|
||||
),
|
||||
@ -72,6 +74,8 @@ CONFIG_SCHEMA = vol.Schema(
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Set up this component."""
|
||||
# Import client from a external python package hosted on PyPi
|
||||
from sampleclient.client import Client
|
||||
|
||||
# Print startup message
|
||||
startup = STARTUP.format(name=DOMAIN, version=VERSION, issueurl=ISSUE_URL)
|
||||
@ -85,6 +89,14 @@ async def async_setup(hass, config):
|
||||
# Create DATA dict
|
||||
hass.data[DOMAIN_DATA] = {}
|
||||
|
||||
# Get "global" configuration.
|
||||
username = config[DOMAIN].get(CONF_USERNAME)
|
||||
password = config[DOMAIN].get(CONF_PASSWORD)
|
||||
|
||||
# Configure the client.
|
||||
client = Client(username, password)
|
||||
hass.data[DOMAIN_DATA]["client"] = BlueprintData(hass, client)
|
||||
|
||||
# Load platforms
|
||||
for platform in PLATFORMS:
|
||||
# Get platform specific configuration
|
||||
@ -110,16 +122,23 @@ 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.
|
||||
try:
|
||||
request = requests.get(URL)
|
||||
jsondata = request.json()
|
||||
hass.data[DOMAIN_DATA] = jsondata
|
||||
except Exception as error: # pylint: disable=broad-except
|
||||
_LOGGER.error("Could not update data - %s", error)
|
||||
class BlueprintData:
|
||||
"""This class handle communication and stores the data."""
|
||||
|
||||
def __init__(self, hass, client):
|
||||
"""Initialize the class."""
|
||||
self.hass = hass
|
||||
self.client = client
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
async def update_data(self):
|
||||
"""Update data."""
|
||||
# This is where the main logic to update platform data goes.
|
||||
try:
|
||||
data = self.client.get_data()
|
||||
self.hass.data[DOMAIN_DATA]["data"] = data
|
||||
except Exception as error: # pylint: disable=broad-except
|
||||
_LOGGER.error("Could not update data - %s", error)
|
||||
|
||||
|
||||
async def check_files(hass):
|
||||
|
@ -1,7 +1,6 @@
|
||||
"""Binary sensor platform for blueprint."""
|
||||
from homeassistant.components.binary_sensor import BinarySensorDevice
|
||||
from . import update_data
|
||||
from .const import BINARY_SENSOR_DEVICE_CLASS, DOMAIN_DATA
|
||||
from .const import ATTRIBUTION, BINARY_SENSOR_DEVICE_CLASS, DEFAULT_NAME, DOMAIN_DATA
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
@ -18,25 +17,26 @@ class BlueprintBinarySensor(BinarySensorDevice):
|
||||
self.hass = hass
|
||||
self.attr = {}
|
||||
self._status = False
|
||||
self._name = config["name"]
|
||||
self._name = config.get("name", DEFAULT_NAME)
|
||||
|
||||
async def async_update(self):
|
||||
"""Update the binary_sensor."""
|
||||
# Send update "signal" to the component
|
||||
await update_data(self.hass)
|
||||
await self.hass.data[DOMAIN_DATA]["client"].update_data()
|
||||
|
||||
# Get new data (if any)
|
||||
updated = self.hass.data[DOMAIN_DATA]
|
||||
updated = self.hass.data[DOMAIN_DATA]["data"].get("data", {})
|
||||
|
||||
# Check the data and update the value.
|
||||
if updated.get("completed") is None:
|
||||
if updated.get("bool_on") is None:
|
||||
self._status = self._status
|
||||
else:
|
||||
self._status = updated.get("completed")
|
||||
self._status = updated.get("bool_on")
|
||||
|
||||
# Set/update attributes
|
||||
self.attr["user_id"] = updated.get("userId")
|
||||
self.attr["title"] = updated.get("title")
|
||||
self.attr["attribution"] = ATTRIBUTION
|
||||
self.attr["time"] = str(updated.get("time"))
|
||||
self.attr["static"] = updated.get("static")
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
|
@ -4,9 +4,15 @@ DOMAIN = "blueprint"
|
||||
DOMAIN_DATA = "{}_data".format(DOMAIN)
|
||||
VERSION = "0.0.1"
|
||||
PLATFORMS = ["binary_sensor", "sensor", "switch"]
|
||||
REQUIRED_FILES = ["binary_sensor.py", "const.py", "sensor.py", "switch.py"]
|
||||
REQUIRED_FILES = [
|
||||
"binary_sensor.py",
|
||||
"const.py",
|
||||
"manifest.json",
|
||||
"sensor.py",
|
||||
"switch.py",
|
||||
]
|
||||
ISSUE_URL = "https://github.com/custom-components/blueprint/issues"
|
||||
|
||||
ATTRIBUTION = "Data from this is provided by blueprint."
|
||||
STARTUP = """
|
||||
-------------------------------------------------------------------
|
||||
{name}
|
||||
@ -17,9 +23,6 @@ If you have any issues with this you need to open an issue here:
|
||||
-------------------------------------------------------------------
|
||||
"""
|
||||
|
||||
# Operational
|
||||
URL = "https://jsonplaceholder.typicode.com/todos/1"
|
||||
|
||||
# Icons
|
||||
ICON = "mdi:format-quote-close"
|
||||
|
||||
@ -32,6 +35,8 @@ CONF_SENSOR = "sensor"
|
||||
CONF_SWITCH = "switch"
|
||||
CONF_ENABLED = "enabled"
|
||||
CONF_NAME = "name"
|
||||
CONF_USERNAME = "username"
|
||||
CONF_PASSWORD = "password"
|
||||
|
||||
# Defaults
|
||||
DEFAULT_NAME = DOMAIN
|
||||
|
@ -1,8 +1,8 @@
|
||||
{
|
||||
"domain": "blueprint",
|
||||
"name": "Blueprint",
|
||||
"documentation": "https://github.com/custom-components/blueprint/blob/master/README.md",
|
||||
"documentation": "https://github.com/custom-components/blueprint",
|
||||
"dependencies": [],
|
||||
"codeowners": ["@ludeeus"],
|
||||
"requirements": []
|
||||
"requirements": ["sampleclient==0.0.1"]
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
"""Sensor platform for blueprint."""
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from . import update_data
|
||||
from .const import DOMAIN_DATA, ICON
|
||||
from .const import ATTRIBUTION, DEFAULT_NAME, DOMAIN_DATA, ICON
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
@ -18,25 +17,26 @@ class BlueprintSensor(Entity):
|
||||
self.hass = hass
|
||||
self.attr = {}
|
||||
self._state = None
|
||||
self._name = config["name"]
|
||||
self._name = config.get("name", DEFAULT_NAME)
|
||||
|
||||
async def async_update(self):
|
||||
"""Update the sensor."""
|
||||
# Send update "signal" to the component
|
||||
await update_data(self.hass)
|
||||
await self.hass.data[DOMAIN_DATA]["client"].update_data()
|
||||
|
||||
# Get new data (if any)
|
||||
updated = self.hass.data[DOMAIN_DATA]
|
||||
updated = self.hass.data[DOMAIN_DATA]["data"].get("data", {})
|
||||
|
||||
# Check the data and update the value.
|
||||
if updated.get("title") is None:
|
||||
if updated.get("static") is None:
|
||||
self._state = self._status
|
||||
else:
|
||||
self._state = updated.get("title")
|
||||
self._state = updated.get("static")
|
||||
|
||||
# Set/update attributes
|
||||
self.attr["user_id"] = updated.get("userId")
|
||||
self.attr["completed"] = updated.get("completed")
|
||||
self.attr["attribution"] = ATTRIBUTION
|
||||
self.attr["time"] = str(updated.get("time"))
|
||||
self.attr["none"] = updated.get("none")
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
|
@ -1,7 +1,6 @@
|
||||
"""Switch platform for blueprint."""
|
||||
from homeassistant.components.switch import SwitchDevice
|
||||
from . import update_data
|
||||
from .const import ICON, DOMAIN_DATA
|
||||
from .const import ATTRIBUTION, DEFAULT_NAME, DOMAIN_DATA, ICON
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
@ -18,33 +17,31 @@ class BlueprintBinarySwitch(SwitchDevice):
|
||||
self.hass = hass
|
||||
self.attr = {}
|
||||
self._status = False
|
||||
self._name = config["name"]
|
||||
self._name = config.get("name", DEFAULT_NAME)
|
||||
|
||||
async def async_update(self):
|
||||
"""Update the switch."""
|
||||
# Send update "signal" to the component
|
||||
await update_data(self.hass)
|
||||
await self.hass.data[DOMAIN_DATA]["client"].update_data()
|
||||
|
||||
# Get new data (if any)
|
||||
updated = self.hass.data[DOMAIN_DATA]
|
||||
updated = self.hass.data[DOMAIN_DATA]["data"].get("data", {})
|
||||
|
||||
# Check the data and update the value.
|
||||
if updated.get("completed") is None:
|
||||
self._status = self._status
|
||||
else:
|
||||
self._status = updated.get("completed")
|
||||
self._status = self.hass.data[DOMAIN_DATA]["client"].client.something
|
||||
|
||||
# Set/update attributes
|
||||
self.attr["user_id"] = updated.get("userId")
|
||||
self.attr["title"] = updated.get("title")
|
||||
self.attr["attribution"] = ATTRIBUTION
|
||||
self.attr["time"] = str(updated.get("time"))
|
||||
self.attr["static"] = updated.get("static")
|
||||
|
||||
async def async_turn_on(self, **kwargs): # pylint: disable=unused-argument
|
||||
"""Turn on the switch."""
|
||||
self._status = True
|
||||
await self.hass.data[DOMAIN_DATA]["client"].client.change_something(True)
|
||||
|
||||
async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument
|
||||
"""Turn off the switch."""
|
||||
self._status = False
|
||||
await self.hass.data[DOMAIN_DATA]["client"].client.change_something(False)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
|
Reference in New Issue
Block a user