This commit is contained in:
ludeeus
2019-03-08 19:17:48 +01:00
commit f686660196
12 changed files with 329 additions and 0 deletions

View File

@ -0,0 +1,66 @@
"""
Component to integrate with https://blueprint.com/api
For more details about this component, please refer to
https://github.com/custom-components/blueprint
"""
import os
import logging
import requests
from homeassistant.helpers import discovery
from .const import * # pylint: disable=wildcard-import
VERSION = '0.0.1'
_LOGGER = logging.getLogger(__name__)
# pylint: disable=unused-argument
async def async_setup(hass, config):
"""Set up this component."""
# Print startup message
startup = STARTUP.format(name=DOMAIN, version=VERSION, issueurl=ISSUE_URL)
_LOGGER.info(startup)
# Check that all required files are present
file_check = await check_files(hass)
if not file_check:
return False
# Create DATA dict
hass.data[DOMAIN_DATA] = {}
# Load platforms
for platform in PLATFORMS:
hass.async_create_task(
discovery.async_load_platform(hass, platform, DOMAIN, {}, config))
return True
async def update_data(hass):
"""Update data."""
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)
async def check_files(hass):
"""Retrun bool that idicate that all files are present."""
base = "{}/custom_components/{}/".format(hass.config.path(), DOMAIN)
missing = []
for file in REQUIRED_FILES:
fullpath = "{}{}".format(base, file)
if not os.path.exists(fullpath):
missing.append(file)
if missing:
_LOGGER.critical("The following files are missing: %s", str(missing))
returnvalue = False
else:
returnvalue = True
return returnvalue

View File

@ -0,0 +1,18 @@
"""Consts"""
DOMAIN = 'blueprint'
DOMAIN_DATA = '{}_data'.format(DOMAIN)
VERSION = '0.0.1'
URL = 'https://blueprint.com/api'
REQUIRED_FILES = ['sensor.py', 'const.py']
ISSUE_URL = 'https://github.com/custom-components/blueprint/issues'
PLATFORMS = ['sensor']
STARTUP = """
----------------------------------------------
{name}
Version: {version}
This is a custom component
If you have any issues with this you need to open an issue here:
{issueurl}
----------------------------------------------
"""

View File

@ -0,0 +1,42 @@
"""Sensor platform for blueprint"""
from homeassistant.helpers.entity import Entity
from . import update_data
from .const import * # pylint: disable=wildcard-import, unused-wildcard-import
ICON = 'mdi:format-quote-close'
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(Entity):
"""blueprint Sensor class."""
def __init__(self, hass):
self.hass = hass
self._state = None
async def async_update(self):
"""Update the sensor."""
await update_data(self.hass)
updated = self.hass.data[DOMAIN_DATA].get('compliment')
if updated is None:
updated = self._state
self._state = updated.capitalize()
@property
def name(self):
"""Return the name of the sensor."""
return DOMAIN
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def icon(self):
"""Return the icon of the sensor."""
return ICON