core/homeassistant/components/sensor/demo.py

66 lines
1.8 KiB
Python
Raw Normal View History

2015-05-10 21:43:37 +00:00
"""
Demo platform that has a couple of fake sensors.
For more details about this platform, please refer to the documentation
https://home-assistant.io/components/demo/
2015-05-10 21:43:37 +00:00
"""
from homeassistant.const import (
ATTR_BATTERY_LEVEL, TEMP_CELSIUS, DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_TEMPERATURE)
from homeassistant.helpers.entity import Entity
2015-03-01 09:35:58 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Demo sensors."""
2015-03-01 09:35:58 +00:00
add_devices([
DemoSensor('Outside Temperature', 15.6, DEVICE_CLASS_TEMPERATURE,
TEMP_CELSIUS, 12),
DemoSensor('Outside Humidity', 54, DEVICE_CLASS_HUMIDITY, '%', None),
2015-03-01 09:35:58 +00:00
])
class DemoSensor(Entity):
2016-03-08 15:46:34 +00:00
"""Representation of a Demo sensor."""
def __init__(self, name, state, device_class,
unit_of_measurement, battery):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
self._name = name
self._state = state
self._device_class = device_class
self._unit_of_measurement = unit_of_measurement
self._battery = battery
@property
def should_poll(self):
2016-02-23 05:21:49 +00:00
"""No polling needed for a demo sensor."""
return False
@property
def device_class(self):
"""Return the device class of the sensor."""
return self._device_class
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit this state is expressed in."""
return self._unit_of_measurement
@property
def device_state_attributes(self):
"""Return the state attributes."""
if self._battery:
return {
ATTR_BATTERY_LEVEL: self._battery,
}