mirror of
https://github.com/home-assistant/core.git
synced 2025-07-30 08:47:09 +00:00

* Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
82 lines
2.2 KiB
Python
82 lines
2.2 KiB
Python
"""
|
|
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
|
|
from homeassistant.const import CONF_NAME, CONF_UNIT_OF_MEASUREMENT
|
|
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'
|
|
|
|
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'):
|
|
vol.All(cv.string, vol.In(['minutes', 'hours', 'days']))
|
|
})
|
|
|
|
|
|
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)
|
|
|
|
async_add_entities([UptimeSensor(name, units)], True)
|
|
|
|
|
|
class UptimeSensor(Entity):
|
|
"""Representation of an uptime sensor."""
|
|
|
|
def __init__(self, name, unit):
|
|
"""Initialize the uptime sensor."""
|
|
self._name = name
|
|
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."""
|
|
return ICON
|
|
|
|
@property
|
|
def unit_of_measurement(self):
|
|
"""Return the unit of measurement the value is expressed in."""
|
|
return self._unit
|
|
|
|
@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)
|