add relative time option to simulated sensors (#14038)

By default simulated sensors are relative to when they're activated,
instead we make this togglable with this new option 'relative_to_epoch',
and instead they become relative to 1970-01-01 00:00:00.
pull/14969/head
Benedict Aas 2018-06-14 19:06:49 +01:00 committed by Paulus Schoutsen
parent b2440a6d95
commit c8e0de19b6
2 changed files with 20 additions and 5 deletions

View File

@ -7,6 +7,7 @@ https://home-assistant.io/components/sensor.simulated/
import logging import logging
import math import math
from random import Random from random import Random
from datetime import datetime
import voluptuous as vol import voluptuous as vol
@ -25,6 +26,7 @@ CONF_PERIOD = 'period'
CONF_PHASE = 'phase' CONF_PHASE = 'phase'
CONF_SEED = 'seed' CONF_SEED = 'seed'
CONF_UNIT = 'unit' CONF_UNIT = 'unit'
CONF_RELATIVE_TO_EPOCH = 'relative_to_epoch'
DEFAULT_AMP = 1 DEFAULT_AMP = 1
DEFAULT_FWHM = 0 DEFAULT_FWHM = 0
@ -34,6 +36,7 @@ DEFAULT_PERIOD = 60
DEFAULT_PHASE = 0 DEFAULT_PHASE = 0
DEFAULT_SEED = 999 DEFAULT_SEED = 999
DEFAULT_UNIT = 'value' DEFAULT_UNIT = 'value'
DEFAULT_RELATIVE_TO_EPOCH = True
ICON = 'mdi:chart-line' ICON = 'mdi:chart-line'
@ -46,6 +49,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_PHASE, default=DEFAULT_PHASE): vol.Coerce(float), vol.Optional(CONF_PHASE, default=DEFAULT_PHASE): vol.Coerce(float),
vol.Optional(CONF_SEED, default=DEFAULT_SEED): cv.positive_int, vol.Optional(CONF_SEED, default=DEFAULT_SEED): cv.positive_int,
vol.Optional(CONF_UNIT, default=DEFAULT_UNIT): cv.string, vol.Optional(CONF_UNIT, default=DEFAULT_UNIT): cv.string,
vol.Optional(CONF_RELATIVE_TO_EPOCH, default=DEFAULT_RELATIVE_TO_EPOCH):
cv.boolean,
}) })
@ -59,15 +64,18 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
phase = config.get(CONF_PHASE) phase = config.get(CONF_PHASE)
fwhm = config.get(CONF_FWHM) fwhm = config.get(CONF_FWHM)
seed = config.get(CONF_SEED) seed = config.get(CONF_SEED)
relative_to_epoch = config.get(CONF_RELATIVE_TO_EPOCH)
sensor = SimulatedSensor(name, unit, amp, mean, period, phase, fwhm, seed) sensor = SimulatedSensor(name, unit, amp, mean, period, phase, fwhm, seed,
relative_to_epoch)
add_devices([sensor], True) add_devices([sensor], True)
class SimulatedSensor(Entity): class SimulatedSensor(Entity):
"""Class for simulated sensor.""" """Class for simulated sensor."""
def __init__(self, name, unit, amp, mean, period, phase, fwhm, seed): def __init__(self, name, unit, amp, mean, period, phase, fwhm, seed,
relative_to_epoch):
"""Init the class.""" """Init the class."""
self._name = name self._name = name
self._unit = unit self._unit = unit
@ -78,7 +86,11 @@ class SimulatedSensor(Entity):
self._fwhm = fwhm self._fwhm = fwhm
self._seed = seed self._seed = seed
self._random = Random(seed) # A local seeded Random self._random = Random(seed) # A local seeded Random
self._start_time = dt_util.utcnow() self._start_time = (
datetime(1970, 1, 1, tzinfo=dt_util.UTC) if relative_to_epoch
else dt_util.utcnow()
)
self._relative_to_epoch = relative_to_epoch
self._state = None self._state = None
def time_delta(self): def time_delta(self):
@ -136,5 +148,6 @@ class SimulatedSensor(Entity):
'phase': self._phase, 'phase': self._phase,
'spread': self._fwhm, 'spread': self._fwhm,
'seed': self._seed, 'seed': self._seed,
'relative_to_epoch': self._relative_to_epoch,
} }
return attr return attr

View File

@ -5,8 +5,8 @@ from tests.common import get_test_home_assistant
from homeassistant.components.sensor.simulated import ( from homeassistant.components.sensor.simulated import (
CONF_AMP, CONF_FWHM, CONF_MEAN, CONF_PERIOD, CONF_PHASE, CONF_SEED, CONF_AMP, CONF_FWHM, CONF_MEAN, CONF_PERIOD, CONF_PHASE, CONF_SEED,
CONF_UNIT, DEFAULT_AMP, DEFAULT_FWHM, DEFAULT_MEAN, DEFAULT_NAME, CONF_UNIT, CONF_RELATIVE_TO_EPOCH, DEFAULT_AMP, DEFAULT_FWHM, DEFAULT_MEAN,
DEFAULT_PHASE, DEFAULT_SEED) DEFAULT_NAME, DEFAULT_PHASE, DEFAULT_SEED, DEFAULT_RELATIVE_TO_EPOCH)
from homeassistant.const import CONF_FRIENDLY_NAME from homeassistant.const import CONF_FRIENDLY_NAME
from homeassistant.setup import setup_component from homeassistant.setup import setup_component
@ -42,3 +42,5 @@ class TestSimulatedSensor(unittest.TestCase):
assert state.attributes.get(CONF_PHASE) == DEFAULT_PHASE assert state.attributes.get(CONF_PHASE) == DEFAULT_PHASE
assert state.attributes.get(CONF_FWHM) == DEFAULT_FWHM assert state.attributes.get(CONF_FWHM) == DEFAULT_FWHM
assert state.attributes.get(CONF_SEED) == DEFAULT_SEED assert state.attributes.get(CONF_SEED) == DEFAULT_SEED
assert state.attributes.get(
CONF_RELATIVE_TO_EPOCH) == DEFAULT_RELATIVE_TO_EPOCH