diff --git a/README.md b/README.md index ca1d724..231fc09 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ developers will have use for, please open a PR to add it :) *** README content if this was a published component: *** + # blueprint [![BuyMeCoffee][buymecoffeebedge]][buymecoffee] @@ -28,6 +29,7 @@ Platform | Description -- | -- `binary_sensor` | Show something `True` or `False` `sensor` | Show info from blueprint API. +`switch`| Switchable device. ![example][exampleimg] @@ -47,14 +49,55 @@ custom_components/blueprint/__init__.py custom_components/blueprint/binary_sensor.py custom_components/blueprint/const.py custom_components/blueprint/sensor.py +custom_components/blueprint/sensor.py ``` ## Example configuration.yaml ```yaml blueprint: + binary_sensor: + - enabled: true + name: My custom name + sensor: + - enabled: true + name: My custom name + switch: + - enabled: true + name: My custom name ``` +## Configuration options + +Key | Type | Required | Description +-- | -- | -- | -- +`binary_sensor` | `list` | `False` | Configuration for the `binary_sensor` platform. +`sensor` | `list` | `False` | Configuration for the `sensor` platform. +`switch` | `list` | `False` | Configuration for the `switch` platform. + + +### Configuration options for `binary_sensor` list + +Key | Type | Required | Default | Description +-- | -- | -- | -- | -- +`enabled` | `boolean` | `False` | `False` | Boolean to enable/disable the platform. +`name` | `string` | `False` | `blueprint` | Custom name for the entity. + +### Configuration options for `sensor` list + +Key | Type | Required | Default | Description +-- | -- | -- | -- | -- +`enabled` | `boolean` | `False` | `False` | Boolean to enable/disable the platform. +`name` | `string` | `False` | `blueprint` | Custom name for the entity. + +### Configuration options for `switch` list + +Key | Type | Required | Default | Description +-- | -- | -- | -- | -- +`enabled` | `boolean` | `False` | `False` | Boolean to enable/disable the platform. +`name` | `string` | `False` | `blueprint` | Custom name for the entity. + + *** [exampleimg]: example.png diff --git a/custom_components/blueprint/__init__.py b/custom_components/blueprint/__init__.py index 44304b9..0077598 100644 --- a/custom_components/blueprint/__init__.py +++ b/custom_components/blueprint/__init__.py @@ -8,17 +8,42 @@ 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) diff --git a/custom_components/blueprint/binary_sensor.py b/custom_components/blueprint/binary_sensor.py index 9c4f6bf..4a2f834 100644 --- a/custom_components/blueprint/binary_sensor.py +++ b/custom_components/blueprint/binary_sensor.py @@ -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 @@ async def async_update(self): @property def name(self): """Return the name of the sensor.""" - return NAME + return self._name @property def device_class(self): diff --git a/custom_components/blueprint/const.py b/custom_components/blueprint/const.py index 2c6fe75..149a3be 100644 --- a/custom_components/blueprint/const.py +++ b/custom_components/blueprint/const.py @@ -25,3 +25,14 @@ # 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 diff --git a/custom_components/blueprint/sensor.py b/custom_components/blueprint/sensor.py index d4e5a01..fa4001a 100644 --- a/custom_components/blueprint/sensor.py +++ b/custom_components/blueprint/sensor.py @@ -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 @@ async def async_update(self): @property def name(self): """Return the name of the sensor.""" - return NAME + return self._name @property def state(self):