Add configuration
This commit is contained in:
@ -8,17 +8,42 @@ 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 (
|
||||
DOMAIN_DATA, DOMAIN, ISSUE_URL, PLATFORMS, REQUIRED_FILES, STARTUP, URL,
|
||||
VERSION)
|
||||
VERSION, CONF_BINARY_SENSOR, CONF_SENSOR, CONF_SWITCH, CONF_ENABLED,
|
||||
CONF_NAME, DEAFULT_NAME)
|
||||
|
||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
BINARY_SENSOR_SCHEMA = vol.Schema({
|
||||
vol.Optional(CONF_ENABLED, default=False): cv.boolean,
|
||||
vol.Optional(CONF_NAME, default=DEAFULT_NAME): cv.string,
|
||||
})
|
||||
|
||||
SENSOR_SCHEMA = vol.Schema({
|
||||
vol.Optional(CONF_ENABLED, default=False): cv.boolean,
|
||||
vol.Optional(CONF_NAME, default=DEAFULT_NAME): cv.string,
|
||||
})
|
||||
|
||||
SWITCH_SCHEMA = vol.Schema({
|
||||
vol.Optional(CONF_ENABLED, default=False): cv.boolean,
|
||||
vol.Optional(CONF_NAME, default=DEAFULT_NAME): cv.string,
|
||||
})
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.Schema({
|
||||
vol.Optional(CONF_BINARY_SENSOR): vol.All(
|
||||
cv.ensure_list, [BINARY_SENSOR_SCHEMA]),
|
||||
vol.Optional(CONF_SENSOR): vol.All(cv.ensure_list, [SENSOR_SCHEMA]),
|
||||
vol.Optional(CONF_SWITCH): vol.All(cv.ensure_list, [SWITCH_SCHEMA]),
|
||||
}),
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
@ -38,9 +63,24 @@ async def async_setup(hass, config):
|
||||
|
||||
# Load platforms
|
||||
for platform in PLATFORMS:
|
||||
hass.async_create_task(
|
||||
discovery.async_load_platform(hass, platform, DOMAIN, {}, config)
|
||||
)
|
||||
# Get platform spesific configuration
|
||||
platform_config = config[DOMAIN].get(platform, {})
|
||||
|
||||
# If platform is not enabled, skip.
|
||||
if not platform_config:
|
||||
continue
|
||||
|
||||
for entry in platform_config:
|
||||
entry_config = platform_config[entry]
|
||||
|
||||
# If entry is not enabled, skip.
|
||||
if not platform_config.get(CONF_ENABLED):
|
||||
continue
|
||||
|
||||
hass.async_create_task(
|
||||
discovery.async_load_platform(
|
||||
hass, platform, DOMAIN, entry_config, config)
|
||||
)
|
||||
return True
|
||||
|
||||
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
||||
|
@ -2,23 +2,24 @@
|
||||
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)
|
||||
BINARY_SENSOR_DEVICE_CLASS, 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([BlueprintBinarySensor(hass)], True)
|
||||
async_add_entities([BlueprintBinarySensor(hass, discovery_info)], True)
|
||||
|
||||
|
||||
class BlueprintBinarySensor(BinarySensorDevice):
|
||||
"""blueprint Sensor class."""
|
||||
|
||||
def __init__(self, hass):
|
||||
def __init__(self, hass, config):
|
||||
self.hass = hass
|
||||
self.attr = {}
|
||||
self._status = False
|
||||
self._name = config['name']
|
||||
|
||||
async def async_update(self):
|
||||
"""Update the sensor."""
|
||||
@ -41,7 +42,7 @@ class BlueprintBinarySensor(BinarySensorDevice):
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return NAME
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
|
@ -25,3 +25,14 @@ SENSOR_ICON = "mdi:format-quote-close"
|
||||
|
||||
# Device classes
|
||||
BINARY_SENSOR_DEVICE_CLASS = 'connectivity'
|
||||
|
||||
# Configuration
|
||||
CONF_BINARY_SENSOR = 'binary_sensor'
|
||||
CONF_SENSOR = 'sensor'
|
||||
CONF_SWITCH = 'switch'
|
||||
CONF_ENABLED = 'enabled'
|
||||
CONF_NAME = 'name'
|
||||
|
||||
|
||||
# Defaults
|
||||
DEAFULT_NAME = DOMAIN
|
||||
|
@ -1,23 +1,24 @@
|
||||
"""Sensor platform for blueprint."""
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from . import update_data
|
||||
from .const import DOMAIN as NAME, DOMAIN_DATA, SENSOR_ICON
|
||||
from .const import 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)
|
||||
async_add_entities([BlueprintSensor(hass, discovery_info)], True)
|
||||
|
||||
|
||||
class BlueprintSensor(Entity):
|
||||
"""blueprint Sensor class."""
|
||||
|
||||
def __init__(self, hass):
|
||||
def __init__(self, hass, config):
|
||||
self.hass = hass
|
||||
self.attr = {}
|
||||
self._state = None
|
||||
self._name = config['name']
|
||||
|
||||
async def async_update(self):
|
||||
"""Update the sensor."""
|
||||
@ -40,7 +41,7 @@ class BlueprintSensor(Entity):
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return NAME
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
|
Reference in New Issue
Block a user