2019-04-03 15:40:03 +00:00
|
|
|
"""Support for showing random states."""
|
2017-10-29 10:15:57 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
BinarySensorDevice, PLATFORM_SCHEMA, DEVICE_CLASSES_SCHEMA)
|
|
|
|
from homeassistant.const import CONF_NAME, CONF_DEVICE_CLASS
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DEFAULT_NAME = 'Random Binary Sensor'
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-06-02 10:00:01 +00:00
|
|
|
async def async_setup_platform(
|
2018-08-24 14:37:30 +00:00
|
|
|
hass, config, async_add_entities, discovery_info=None):
|
2017-10-29 10:15:57 +00:00
|
|
|
"""Set up the Random binary sensor."""
|
|
|
|
name = config.get(CONF_NAME)
|
|
|
|
device_class = config.get(CONF_DEVICE_CLASS)
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities([RandomSensor(name, device_class)], True)
|
2017-10-29 10:15:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RandomSensor(BinarySensorDevice):
|
|
|
|
"""Representation of a Random binary sensor."""
|
|
|
|
|
|
|
|
def __init__(self, name, device_class):
|
|
|
|
"""Initialize the Random binary sensor."""
|
|
|
|
self._name = name
|
|
|
|
self._device_class = device_class
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if sensor is on."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_class(self):
|
|
|
|
"""Return the sensor class of the sensor."""
|
|
|
|
return self._device_class
|
|
|
|
|
2018-06-02 10:00:01 +00:00
|
|
|
async def async_update(self):
|
2017-10-29 10:15:57 +00:00
|
|
|
"""Get new state and update the sensor's state."""
|
|
|
|
from random import getrandbits
|
|
|
|
self._state = bool(getrandbits(1))
|