Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion homeassistant/components/sensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa
from homeassistant.const import (
DEVICE_CLASS_BATTERY, DEVICE_CLASS_HUMIDITY, DEVICE_CLASS_ILLUMINANCE,
DEVICE_CLASS_TEMPERATURE)
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_TIMESTAMP)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -28,6 +28,7 @@
DEVICE_CLASS_HUMIDITY, # % of humidity in the air
DEVICE_CLASS_ILLUMINANCE, # current light level (lx/lm)
DEVICE_CLASS_TEMPERATURE, # temperature (C/F)
DEVICE_CLASS_TIMESTAMP, # ISO formatted timestamp
]

DEVICE_CLASSES_SCHEMA = vol.All(vol.Lower, vol.In(DEVICE_CLASSES))
Expand Down
38 changes: 10 additions & 28 deletions homeassistant/components/sensor/uptime.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,38 @@
import voluptuous as vol

from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_NAME, CONF_UNIT_OF_MEASUREMENT
from homeassistant.const import (
CONF_NAME, DEVICE_CLASS_TIMESTAMP)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
import homeassistant.util.dt as dt_util

_LOGGER = logging.getLogger(__name__)

DEFAULT_NAME = 'Uptime'
DEFAULT_NAME = 'Start'

ICON = 'mdi:clock'
ICON = 'mdi:timer'

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(['minutes', 'hours', 'days']))
})


async def async_setup_platform(
hass, config, async_add_devices, discovery_info=None):
"""Set up the uptime sensor platform."""
name = config.get(CONF_NAME)
units = config.get(CONF_UNIT_OF_MEASUREMENT)

async_add_devices([UptimeSensor(name, units)], True)
async_add_devices([UptimeSensor(name)], True)


class UptimeSensor(Entity):
"""Representation of an uptime sensor."""

def __init__(self, name, unit):
def __init__(self, name):
"""Initialize the uptime sensor."""
self._name = name
self._unit = unit
self.initial = dt_util.now()
self._state = None
self._state = dt_util.now()

@property
def name(self):
Expand All @@ -57,25 +53,11 @@ def icon(self):
return ICON

@property
def unit_of_measurement(self):
"""Return the unit of measurement the value is expressed in."""
return self._unit
def device_class(self):
"""Return the device class of the sensor."""
return DEVICE_CLASS_TIMESTAMP

@property
def state(self):
"""Return the state of the sensor."""
return self._state

async def async_update(self):
"""Update the state of the sensor."""
delta = dt_util.now() - self.initial
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)
1 change: 1 addition & 0 deletions homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
DEVICE_CLASS_HUMIDITY = 'humidity'
DEVICE_CLASS_ILLUMINANCE = 'illuminance'
DEVICE_CLASS_TEMPERATURE = 'temperature'
DEVICE_CLASS_TIMESTAMP = 'timestamp'

# #### STATES ####
STATE_ON = 'on'
Expand Down
80 changes: 0 additions & 80 deletions tests/components/sensor/test_uptime.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
"""The tests for the uptime sensor platform."""
import unittest
from unittest.mock import patch
from datetime import timedelta

from homeassistant.util.async_ import run_coroutine_threadsafe
from homeassistant.setup import setup_component
from homeassistant.components.sensor.uptime import UptimeSensor
from tests.common import get_test_home_assistant
Expand Down Expand Up @@ -38,80 +35,3 @@ def test_uptime_sensor_name_change(self):
}
}
assert setup_component(self.hass, 'sensor', config)

def test_uptime_sensor_config_hours(self):
"""Test uptime sensor with hours defined in config."""
config = {
'sensor': {
'platform': 'uptime',
'unit_of_measurement': 'hours',
}
}
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')
self.assertEqual(sensor.unit_of_measurement, 'days')
new_time = sensor.initial + timedelta(days=1)
with patch('homeassistant.util.dt.now', return_value=new_time):
run_coroutine_threadsafe(
sensor.async_update(),
self.hass.loop
).result()
self.assertEqual(sensor.state, 1.00)
new_time = sensor.initial + timedelta(days=111.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, 111.50)

def test_uptime_sensor_hours_output(self):
"""Test uptime sensor output data."""
sensor = UptimeSensor('test', 'hours')
self.assertEqual(sensor.unit_of_measurement, 'hours')
new_time = sensor.initial + timedelta(hours=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(hours=72.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, 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)