Merge pull request #10082 from arsaboo/uptimefix

Add minutes to Uptime sensor
pull/10099/merge
Pascal Vizeli 2017-10-24 08:32:46 +02:00 committed by GitHub
commit a4b0e8f897
2 changed files with 32 additions and 1 deletions

View File

@ -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)

View File

@ -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)