core/homeassistant/components/uptime/sensor.py

82 lines
2.2 KiB
Python
Raw Normal View History

"""
2018-06-02 12:31:06 +00:00
Platform to retrieve uptime for Home Assistant.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.uptime/
"""
import logging
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
2018-06-02 12:31:06 +00:00
from homeassistant.const import CONF_NAME, CONF_UNIT_OF_MEASUREMENT
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
2018-06-02 12:31:06 +00:00
import homeassistant.util.dt as dt_util
_LOGGER = logging.getLogger(__name__)
DEFAULT_NAME = 'Uptime'
2018-06-02 12:31:06 +00:00
ICON = 'mdi:clock'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_UNIT_OF_MEASUREMENT, default='days'):
2017-10-23 18:38:16 +00:00
vol.All(cv.string, vol.In(['minutes', 'hours', 'days']))
})
2018-06-02 12:31:06 +00:00
async def async_setup_platform(
hass, config, async_add_entities, discovery_info=None):
"""Set up the uptime sensor platform."""
name = config.get(CONF_NAME)
units = config.get(CONF_UNIT_OF_MEASUREMENT)
2018-06-02 12:31:06 +00:00
async_add_entities([UptimeSensor(name, units)], True)
class UptimeSensor(Entity):
"""Representation of an uptime sensor."""
2018-06-02 12:31:06 +00:00
def __init__(self, name, unit):
"""Initialize the uptime sensor."""
self._name = name
2018-06-02 12:31:06 +00:00
self._unit = unit
self.initial = dt_util.now()
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def icon(self):
"""Icon to display in the front end."""
2018-06-02 12:31:06 +00:00
return ICON
@property
def unit_of_measurement(self):
"""Return the unit of measurement the value is expressed in."""
2018-06-02 12:31:06 +00:00
return self._unit
@property
def state(self):
"""Return the state of the sensor."""
return self._state
2018-06-02 12:31:06 +00:00
async def async_update(self):
"""Update the state of the sensor."""
delta = dt_util.now() - self.initial
div_factor = 3600
2018-06-02 12:31:06 +00:00
if self.unit_of_measurement == 'days':
div_factor *= 24
2017-10-23 18:38:16 +00:00
elif self.unit_of_measurement == 'minutes':
div_factor /= 60
2018-06-02 12:31:06 +00:00
delta = delta.total_seconds() / div_factor
2017-10-23 18:38:16 +00:00
self._state = round(delta, 2)
_LOGGER.debug("New value: %s", delta)