Skip to content

Commit

Permalink
Add configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus committed Mar 9, 2019
1 parent 011400b commit 53f2adf
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 13 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -28,6 +29,7 @@ Platform | Description
-- | --
`binary_sensor` | Show something `True` or `False`
`sensor` | Show info from blueprint API.
`switch`| Switchable device.

![example][exampleimg]

Expand All @@ -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
Expand Down
50 changes: 45 additions & 5 deletions custom_components/blueprint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions custom_components/blueprint/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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):
Expand Down
11 changes: 11 additions & 0 deletions custom_components/blueprint/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 5 additions & 4 deletions custom_components/blueprint/sensor.py
Original file line number Diff line number Diff line change
@@ -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."""
Expand All @@ -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):
Expand Down

0 comments on commit 53f2adf

Please sign in to comment.