mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
Add optional unit of measurement (#6796)
This commit is contained in:
parent
5d5547cdb6
commit
7782e7e948
@ -11,7 +11,8 @@ import voluptuous as vol
|
|||||||
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
from homeassistant.const import (CONF_NAME, CONF_MINIMUM, CONF_MAXIMUM)
|
from homeassistant.const import (
|
||||||
|
CONF_NAME, CONF_MINIMUM, CONF_MAXIMUM, CONF_UNIT_OF_MEASUREMENT)
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -26,6 +27,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
vol.Optional(CONF_MAXIMUM, default=DEFAULT_MAX): cv.positive_int,
|
vol.Optional(CONF_MAXIMUM, default=DEFAULT_MAX): cv.positive_int,
|
||||||
vol.Optional(CONF_MINIMUM, default=DEFAULT_MIN): cv.positive_int,
|
vol.Optional(CONF_MINIMUM, default=DEFAULT_MIN): cv.positive_int,
|
||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
|
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@ -35,19 +37,21 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
|||||||
name = config.get(CONF_NAME)
|
name = config.get(CONF_NAME)
|
||||||
minimum = config.get(CONF_MINIMUM)
|
minimum = config.get(CONF_MINIMUM)
|
||||||
maximum = config.get(CONF_MAXIMUM)
|
maximum = config.get(CONF_MAXIMUM)
|
||||||
|
unit = config.get(CONF_UNIT_OF_MEASUREMENT)
|
||||||
|
|
||||||
async_add_devices([RandomSensor(name, minimum, maximum)], True)
|
async_add_devices([RandomSensor(name, minimum, maximum, unit)], True)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
class RandomSensor(Entity):
|
class RandomSensor(Entity):
|
||||||
"""Representation of a Random number sensor."""
|
"""Representation of a Random number sensor."""
|
||||||
|
|
||||||
def __init__(self, name, minimum, maximum):
|
def __init__(self, name, minimum, maximum, unit_of_measurement):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._name = name
|
self._name = name
|
||||||
self._minimum = minimum
|
self._minimum = minimum
|
||||||
self._maximum = maximum
|
self._maximum = maximum
|
||||||
|
self._unit_of_measurement = unit_of_measurement
|
||||||
self._state = None
|
self._state = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -65,6 +69,11 @@ class RandomSensor(Entity):
|
|||||||
"""Icon to use in the frontend, if any."""
|
"""Icon to use in the frontend, if any."""
|
||||||
return ICON
|
return ICON
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unit_of_measurement(self):
|
||||||
|
"""Return the unit this state is expressed in."""
|
||||||
|
return self._unit_of_measurement
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_update(self):
|
def async_update(self):
|
||||||
"""Get a new number and updates the states."""
|
"""Get a new number and updates the states."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user