mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 23:27:37 +00:00
Update syntax (#14770)
This commit is contained in:
parent
74b7dabf2d
commit
b86cd325fe
@ -1,25 +1,25 @@
|
|||||||
"""
|
"""
|
||||||
Component to retrieve uptime for Home Assistant.
|
Platform to retrieve uptime for Home Assistant.
|
||||||
|
|
||||||
For more details about this platform, please refer to the documentation at
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/sensor.uptime/
|
https://home-assistant.io/components/sensor.uptime/
|
||||||
"""
|
"""
|
||||||
import asyncio
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
|
||||||
import homeassistant.util.dt as dt_util
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||||
from homeassistant.const import (
|
from homeassistant.const import CONF_NAME, CONF_UNIT_OF_MEASUREMENT
|
||||||
CONF_NAME, CONF_UNIT_OF_MEASUREMENT)
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEFAULT_NAME = 'Uptime'
|
DEFAULT_NAME = 'Uptime'
|
||||||
|
|
||||||
|
ICON = 'mdi:clock'
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
vol.Optional(CONF_UNIT_OF_MEASUREMENT, default='days'):
|
vol.Optional(CONF_UNIT_OF_MEASUREMENT, default='days'):
|
||||||
@ -27,22 +27,22 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def async_setup_platform(
|
||||||
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
hass, config, async_add_devices, discovery_info=None):
|
||||||
"""Set up the uptime sensor platform."""
|
"""Set up the uptime sensor platform."""
|
||||||
name = config.get(CONF_NAME)
|
name = config.get(CONF_NAME)
|
||||||
units = config.get(CONF_UNIT_OF_MEASUREMENT)
|
units = config.get(CONF_UNIT_OF_MEASUREMENT)
|
||||||
|
|
||||||
async_add_devices([UptimeSensor(name, units)], True)
|
async_add_devices([UptimeSensor(name, units)], True)
|
||||||
|
|
||||||
|
|
||||||
class UptimeSensor(Entity):
|
class UptimeSensor(Entity):
|
||||||
"""Representation of an uptime sensor."""
|
"""Representation of an uptime sensor."""
|
||||||
|
|
||||||
def __init__(self, name, units):
|
def __init__(self, name, unit):
|
||||||
"""Initialize the uptime sensor."""
|
"""Initialize the uptime sensor."""
|
||||||
self._name = name
|
self._name = name
|
||||||
self._icon = 'mdi:clock'
|
self._unit = unit
|
||||||
self._units = units
|
|
||||||
self.initial = dt_util.now()
|
self.initial = dt_util.now()
|
||||||
self._state = None
|
self._state = None
|
||||||
|
|
||||||
@ -54,27 +54,28 @@ class UptimeSensor(Entity):
|
|||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
"""Icon to display in the front end."""
|
"""Icon to display in the front end."""
|
||||||
return self._icon
|
return ICON
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unit_of_measurement(self):
|
def unit_of_measurement(self):
|
||||||
"""Return the unit of measurement the value is expressed in."""
|
"""Return the unit of measurement the value is expressed in."""
|
||||||
return self._units
|
return self._unit
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
@asyncio.coroutine
|
async def async_update(self):
|
||||||
def async_update(self):
|
|
||||||
"""Update the state of the sensor."""
|
"""Update the state of the sensor."""
|
||||||
delta = dt_util.now() - self.initial
|
delta = dt_util.now() - self.initial
|
||||||
div_factor = 3600
|
div_factor = 3600
|
||||||
|
|
||||||
if self.unit_of_measurement == 'days':
|
if self.unit_of_measurement == 'days':
|
||||||
div_factor *= 24
|
div_factor *= 24
|
||||||
elif self.unit_of_measurement == 'minutes':
|
elif self.unit_of_measurement == 'minutes':
|
||||||
div_factor /= 60
|
div_factor /= 60
|
||||||
|
|
||||||
delta = delta.total_seconds() / div_factor
|
delta = delta.total_seconds() / div_factor
|
||||||
self._state = round(delta, 2)
|
self._state = round(delta, 2)
|
||||||
_LOGGER.debug("New value: %s", delta)
|
_LOGGER.debug("New value: %s", delta)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user