Skip to content
Permalink
Browse files

Add example light/sensor from dev docs

  • Loading branch information...
balloob committed Apr 6, 2019
1 parent 90174e4 commit 179dcd3bd10e4ee1d828ebeac9b687a4576cf18a
@@ -0,0 +1,22 @@
# Awesome Lights

This integration shows how you would go ahead and integrate a phyisical light into Home Assistant.

If you use this integration as a template, make sure you tweak the following places:

- `manifest.json`: update the requirements to point at your Python library
- `light.py`: update the code to interact with your library

### Installation

Copy this folder to `<config_dir>/custom_components/example_light/`.

Add the following entry in your `configuration.yaml`:

```yaml
light:
- platform: awesome_lights
host: HOST_HERE
username: USERNAME_HERE
password: PASSWORD_HERE_OR_secrets.yaml
```
@@ -0,0 +1 @@
"""Example Lights integration."""
@@ -0,0 +1,94 @@
"""Platform for light integration."""
import logging

import voluptuous as vol

# Import the device class from the component that you want to support
from homeassistant.components.light import ATTR_BRIGHTNESS, Light, PLATFORM_SCHEMA
from homeassistant.const import CONF_HOST, CONF_USERNAME, CONF_PASSWORD
import homeassistant.helpers.config_validation as cv


_LOGGER = logging.getLogger(__name__)

# Validation of the user's configuration
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_USERNAME, default='admin'): cv.string,
vol.Optional(CONF_PASSWORD): cv.string,
})


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Awesome Light platform."""
import awesomelights

# Assign configuration variables. The configuration check takes care they are
# present.
host = config.get(CONF_HOST)
username = config.get(CONF_USERNAME)
password = config.get(CONF_PASSWORD)

# Setup connection with devices/cloud
hub = awesomelights.Hub(host, username, password)

# Verify that passed in configuration works
if not hub.is_valid_login():
_LOGGER.error("Could not connect to AwesomeLight hub")
return

# Add devices
add_devices(AwesomeLight(light) for light in hub.lights())



class AwesomeLight(Light):
"""Representation of an Awesome Light."""

def __init__(self, light):
"""Initialize an AwesomeLight."""
self._light = light
self._name = light.name
self._state = None
self._brightness = None

@property
def name(self):
"""Return the display name of this light."""
return self._name

@property
def brightness(self):
"""Return the brightness of the light.
This method is optional. Removing it indicates to Home Assistant
that brightness is not supported for this light.
"""
return self._brightness

@property
def is_on(self):
"""Return true if light is on."""
return self._state

def turn_on(self, **kwargs):
"""Instruct the light to turn on.
You can skip the brightness part if your light does not support
brightness control.
"""
self._light.brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
self._light.turn_on()

def turn_off(self, **kwargs):
"""Instruct the light to turn off."""
self._light.turn_off()

def update(self):
"""Fetch new state data for this light.
This is the only method that should fetch new data for Home Assistant.
"""
self._light.update()
self._state = self._light.is_on()
self._brightness = self._light.brightness
@@ -0,0 +1,8 @@
{
"domain": "example_light",
"name": "Example Light",
"documentation": "https://github.com/home-assistant/example-custom-config/tree/master/custom_components/example_light/",
"dependencies": [],
"codeowners": [],
"requirements": ["awesomelights==1.2.3"]
}
@@ -0,0 +1,15 @@
# Example Sensor

This is a minimum implementation of an integration providing a sensor measurement.

### Installation

Copy this folder to `<config_dir>/custom_components/example_sensor/`.

Add the following to your `configuration.yaml` file:

```yaml
# Example configuration.yaml entry
sensor:
platform: example_sensor
```
@@ -0,0 +1 @@
"""The example sensor integration."""
@@ -0,0 +1,8 @@
{
"domain": "example_sensor",
"name": "Example Sensor",
"documentation": "https://github.com/home-assistant/example-custom-config/tree/master/custom_components/example_sensor/",
"dependencies": [],
"codeowners": [],
"requirements": []
}
@@ -0,0 +1,37 @@
from homeassistant.const import TEMP_CELSIUS
from homeassistant.helpers.entity import Entity


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the sensor platform."""
add_devices([ExampleSensor()])


class ExampleSensor(Entity):
"""Representation of a Sensor."""

def __init__(self):
"""Initialize the sensor."""
self._state = None

@property
def name(self):
"""Return the name of the sensor."""
return 'Example Temperature'

@property
def state(self):
"""Return the state of the sensor."""
return self._state

@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return TEMP_CELSIUS

def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
self._state = 23

0 comments on commit 179dcd3

Please sign in to comment.
You can’t perform that action at this time.