Add switch platform

This commit is contained in:
ludeeus 2019-03-10 15:55:33 +01:00
parent 6ab2f89a30
commit 56d61a8c1f
6 changed files with 102 additions and 13 deletions

View File

@ -27,8 +27,9 @@ _Component to integrate with [blueprint][blueprint]._
Platform | Description
-- | --
`binary_sensor` | Show something `True` or `False`
`binary_sensor` | Show something `True` or `False`.
`sensor` | Show info from blueprint API.
`switch` | Switch something `True` or `False`.
![example][exampleimg]
@ -48,6 +49,7 @@ custom_components/blueprint/__init__.py
custom_components/blueprint/binary_sensor.py
custom_components/blueprint/const.py
custom_components/blueprint/sensor.py
custom_components/blueprint/switch.py
```
## Example configuration.yaml
@ -60,6 +62,9 @@ blueprint:
sensor:
- enabled: true
name: My custom name
switch:
- enabled: true
name: My custom name
```
## Configuration options
@ -84,6 +89,13 @@ Key | Type | Required | Default | Description
`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.
## Contributions are welcome!
If you want to contribute to this please read the [Contribution guidelines](CONTRIBUTING.md)

View File

@ -23,6 +23,7 @@ from .const import (
VERSION,
CONF_BINARY_SENSOR,
CONF_SENSOR,
CONF_SWITCH,
CONF_ENABLED,
CONF_NAME,
DEAFULT_NAME,
@ -46,6 +47,13 @@ SENSOR_SCHEMA = vol.Schema(
}
)
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(
@ -54,6 +62,7 @@ CONFIG_SCHEMA = vol.Schema(
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]),
}
)
},

View File

@ -1,4 +1,4 @@
"""Binary ensor platform for blueprint."""
"""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
@ -7,12 +7,12 @@ from .const import BINARY_SENSOR_DEVICE_CLASS, DOMAIN_DATA
async def async_setup_platform(
hass, config, async_add_entities, discovery_info=None
): # pylint: disable=unused-argument
"""Setup sensor platform."""
"""Setup binary_sensor platform."""
async_add_entities([BlueprintBinarySensor(hass, discovery_info)], True)
class BlueprintBinarySensor(BinarySensorDevice):
"""blueprint Sensor class."""
"""blueprint binary_sensor class."""
def __init__(self, hass, config):
self.hass = hass
@ -21,7 +21,7 @@ class BlueprintBinarySensor(BinarySensorDevice):
self._name = config["name"]
async def async_update(self):
"""Update the sensor."""
"""Update the binary_sensor."""
# Send update "signal" to the component
await update_data(self.hass)
@ -40,17 +40,17 @@ class BlueprintBinarySensor(BinarySensorDevice):
@property
def name(self):
"""Return the name of the sensor."""
"""Return the name of the binary_sensor."""
return self._name
@property
def device_class(self):
"""Return the class of this sensor."""
"""Return the class of this binary_sensor."""
return BINARY_SENSOR_DEVICE_CLASS
@property
def is_on(self):
"""Return true if the binary sensor is on."""
"""Return true if the binary_sensor is on."""
return self._status
@property

View File

@ -3,8 +3,8 @@
DOMAIN = "blueprint"
DOMAIN_DATA = "{}_data".format(DOMAIN)
VERSION = "0.0.1"
PLATFORMS = ["binary_sensor", "sensor"]
REQUIRED_FILES = ["binary_sensor.py", "sensor.py", "const.py"]
PLATFORMS = ["binary_sensor", "sensor", "switch"]
REQUIRED_FILES = ["binary_sensor.py", "const.py", "sensor.py", "switch.py"]
ISSUE_URL = "https://github.com/custom-components/blueprint/issues"
STARTUP = """
@ -21,7 +21,7 @@ If you have any issues with this you need to open an issue here:
URL = "https://jsonplaceholder.typicode.com/todos/1"
# Icons
SENSOR_ICON = "mdi:format-quote-close"
ICON = "mdi:format-quote-close"
# Device classes
BINARY_SENSOR_DEVICE_CLASS = "connectivity"
@ -29,6 +29,7 @@ BINARY_SENSOR_DEVICE_CLASS = "connectivity"
# Configuration
CONF_BINARY_SENSOR = "binary_sensor"
CONF_SENSOR = "sensor"
CONF_SWITCH = "switch"
CONF_ENABLED = "enabled"
CONF_NAME = "name"

View File

@ -1,7 +1,7 @@
"""Sensor platform for blueprint."""
from homeassistant.helpers.entity import Entity
from . import update_data
from .const import DOMAIN_DATA, SENSOR_ICON
from .const import DOMAIN_DATA, ICON
async def async_setup_platform(
@ -51,7 +51,7 @@ class BlueprintSensor(Entity):
@property
def icon(self):
"""Return the icon of the sensor."""
return SENSOR_ICON
return ICON
@property
def device_state_attributes(self):

View File

@ -0,0 +1,67 @@
"""Switch platform for blueprint."""
from homeassistant.components.switch import SwitchDevice
from . import update_data
from .const import ICON, DOMAIN_DATA
async def async_setup_platform(
hass, config, async_add_entities, discovery_info=None
): # pylint: disable=unused-argument
"""Setup switch platform."""
async_add_entities([BlueprintBinarySwitch(hass, discovery_info)], True)
class BlueprintBinarySwitch(SwitchDevice):
"""blueprint switch class."""
def __init__(self, hass, config):
self.hass = hass
self.attr = {}
self._status = False
self._name = config["name"]
async def async_update(self):
"""Update the switch."""
# Send update "signal" to the component
await update_data(self.hass)
# Get new data (if any)
updated = self.hass.data[DOMAIN_DATA]
# Check the data and update the value.
if updated.get("completed") is None:
self._status = self._status
else:
self._status = updated.get("completed")
# Set/update attributes
self.attr["user_id"] = updated.get("userId")
self.attr["title"] = updated.get("title")
async def async_turn_on(self, **kwargs): # pylint: disable=unused-argument
"""Turn on the switch."""
self._status = True
async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument
"""Turn off the switch."""
self._status = False
@property
def name(self):
"""Return the name of the switch."""
return self._name
@property
def icon(self):
"""Return the icon of this switch."""
return ICON
@property
def is_on(self):
"""Return true if the switch is on."""
return self._status
@property
def device_state_attributes(self):
"""Return the state attributes."""
return self.attr