commit
a4b0e8f897
|
@ -23,7 +23,7 @@ DEFAULT_NAME = 'Uptime'
|
|||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_UNIT_OF_MEASUREMENT, default='days'):
|
||||
vol.All(cv.string, vol.In(['hours', 'days']))
|
||||
vol.All(cv.string, vol.In(['minutes', 'hours', 'days']))
|
||||
})
|
||||
|
||||
|
||||
|
@ -73,6 +73,8 @@ class UptimeSensor(Entity):
|
|||
div_factor = 3600
|
||||
if self.unit_of_measurement == 'days':
|
||||
div_factor *= 24
|
||||
elif self.unit_of_measurement == 'minutes':
|
||||
div_factor /= 60
|
||||
delta = delta.total_seconds() / div_factor
|
||||
self._state = round(delta, 2)
|
||||
_LOGGER.debug("New value: %s", delta)
|
||||
|
|
|
@ -49,6 +49,16 @@ class TestUptimeSensor(unittest.TestCase):
|
|||
}
|
||||
assert setup_component(self.hass, 'sensor', config)
|
||||
|
||||
def test_uptime_sensor_config_minutes(self):
|
||||
"""Test uptime sensor with minutes defined in config."""
|
||||
config = {
|
||||
'sensor': {
|
||||
'platform': 'uptime',
|
||||
'unit_of_measurement': 'minutes',
|
||||
}
|
||||
}
|
||||
assert setup_component(self.hass, 'sensor', config)
|
||||
|
||||
def test_uptime_sensor_days_output(self):
|
||||
"""Test uptime sensor output data."""
|
||||
sensor = UptimeSensor('test', 'days')
|
||||
|
@ -86,3 +96,22 @@ class TestUptimeSensor(unittest.TestCase):
|
|||
self.hass.loop
|
||||
).result()
|
||||
self.assertEqual(sensor.state, 72.50)
|
||||
|
||||
def test_uptime_sensor_minutes_output(self):
|
||||
"""Test uptime sensor output data."""
|
||||
sensor = UptimeSensor('test', 'minutes')
|
||||
self.assertEqual(sensor.unit_of_measurement, 'minutes')
|
||||
new_time = sensor.initial + timedelta(minutes=16)
|
||||
with patch('homeassistant.util.dt.now', return_value=new_time):
|
||||
run_coroutine_threadsafe(
|
||||
sensor.async_update(),
|
||||
self.hass.loop
|
||||
).result()
|
||||
self.assertEqual(sensor.state, 16.00)
|
||||
new_time = sensor.initial + timedelta(minutes=12.499)
|
||||
with patch('homeassistant.util.dt.now', return_value=new_time):
|
||||
run_coroutine_threadsafe(
|
||||
sensor.async_update(),
|
||||
self.hass.loop
|
||||
).result()
|
||||
self.assertEqual(sensor.state, 12.50)
|
||||
|
|
Loading…
Reference in New Issue