Use time.monotonic instead of time.time where appropriate (#31780)
parent
fbbb29a6ec
commit
3018e8ff47
|
@ -4,7 +4,7 @@ import logging
|
|||
import logging.handlers
|
||||
import os
|
||||
import sys
|
||||
from time import time
|
||||
from time import monotonic
|
||||
from typing import Any, Dict, Optional, Set
|
||||
|
||||
import voluptuous as vol
|
||||
|
@ -110,7 +110,7 @@ async def async_from_config_dict(
|
|||
Dynamically loads required components and its dependencies.
|
||||
This method is a coroutine.
|
||||
"""
|
||||
start = time()
|
||||
start = monotonic()
|
||||
|
||||
core_config = config.get(core.DOMAIN, {})
|
||||
|
||||
|
@ -131,7 +131,7 @@ async def async_from_config_dict(
|
|||
|
||||
await _async_set_up_integrations(hass, config)
|
||||
|
||||
stop = time()
|
||||
stop = monotonic()
|
||||
_LOGGER.info("Home Assistant initialized in %.2fs", stop - start)
|
||||
|
||||
if REQUIRED_NEXT_PYTHON_DATE and sys.version_info[:3] < REQUIRED_NEXT_PYTHON_VER:
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
"""Support for BME680 Sensor over SMBus."""
|
||||
import logging
|
||||
import threading
|
||||
from time import sleep, time
|
||||
from time import monotonic, sleep
|
||||
|
||||
import bme680 # pylint: disable=import-error
|
||||
from smbus import SMBus # pylint: disable=import-error
|
||||
|
@ -240,15 +240,15 @@ class BME680Handler:
|
|||
# Pause to allow initial data read for device validation.
|
||||
sleep(1)
|
||||
|
||||
start_time = time()
|
||||
curr_time = time()
|
||||
start_time = monotonic()
|
||||
curr_time = monotonic()
|
||||
burn_in_data = []
|
||||
|
||||
_LOGGER.info(
|
||||
"Beginning %d second gas sensor burn in for Air Quality", burn_in_time
|
||||
)
|
||||
while curr_time - start_time < burn_in_time:
|
||||
curr_time = time()
|
||||
curr_time = monotonic()
|
||||
if self._sensor.get_sensor_data() and self._sensor.data.heat_stable:
|
||||
gas_resistance = self._sensor.data.gas_resistance
|
||||
burn_in_data.append(gas_resistance)
|
||||
|
|
|
@ -285,7 +285,7 @@ class Doods(ImageProcessingEntity):
|
|||
)
|
||||
|
||||
# Run detection
|
||||
start = time.time()
|
||||
start = time.monotonic()
|
||||
response = self._doods.detect(
|
||||
image, dconfig=self._dconfig, detector_name=self._detector_name
|
||||
)
|
||||
|
@ -293,7 +293,7 @@ class Doods(ImageProcessingEntity):
|
|||
"doods detect: %s response: %s duration: %s",
|
||||
self._dconfig,
|
||||
response,
|
||||
time.time() - start,
|
||||
time.monotonic() - start,
|
||||
)
|
||||
|
||||
matches = {}
|
||||
|
|
|
@ -90,14 +90,14 @@ class MaxCubeHandle:
|
|||
self.cube = cube
|
||||
self.scan_interval = scan_interval
|
||||
self.mutex = Lock()
|
||||
self._updatets = time.time()
|
||||
self._updatets = time.monotonic()
|
||||
|
||||
def update(self):
|
||||
"""Pull the latest data from the MAX! Cube."""
|
||||
# Acquire mutex to prevent simultaneous update from multiple threads
|
||||
with self.mutex:
|
||||
# Only update every update_interval
|
||||
if (time.time() - self._updatets) >= self.scan_interval:
|
||||
if (time.monotonic() - self._updatets) >= self.scan_interval:
|
||||
_LOGGER.debug("Updating")
|
||||
|
||||
try:
|
||||
|
@ -106,6 +106,6 @@ class MaxCubeHandle:
|
|||
_LOGGER.error("Max!Cube connection failed")
|
||||
return False
|
||||
|
||||
self._updatets = time.time()
|
||||
self._updatets = time.monotonic()
|
||||
else:
|
||||
_LOGGER.debug("Skipping update")
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
"""Support for the Netatmo Weather Service."""
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from time import time
|
||||
|
||||
import pyatmo
|
||||
|
||||
|
@ -519,7 +518,6 @@ class NetatmoData:
|
|||
"""Initialize the data object."""
|
||||
self.data = {}
|
||||
self.station_data = station_data
|
||||
self._next_update = time()
|
||||
self.auth = auth
|
||||
|
||||
def get_module_infos(self):
|
||||
|
|
|
@ -147,12 +147,12 @@ class ProxmoxClient:
|
|||
verify_ssl=self._verify_ssl,
|
||||
)
|
||||
|
||||
self._connection_start_time = time.time()
|
||||
self._connection_start_time = time.monotonic()
|
||||
|
||||
def get_api_client(self):
|
||||
"""Return the ProxmoxAPI client and rebuild it if necessary."""
|
||||
|
||||
connection_age = time.time() - self._connection_start_time
|
||||
connection_age = time.monotonic() - self._connection_start_time
|
||||
|
||||
# Workaround for the Proxmoxer bug where the connection stops working after some time
|
||||
if connection_age > 30 * 60:
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""Support for Verisure locks."""
|
||||
import logging
|
||||
from time import sleep, time
|
||||
from time import monotonic, sleep
|
||||
|
||||
from homeassistant.components.lock import LockDevice
|
||||
from homeassistant.const import ATTR_CODE, STATE_LOCKED, STATE_UNLOCKED
|
||||
|
@ -71,7 +71,7 @@ class VerisureDoorlock(LockDevice):
|
|||
|
||||
def update(self):
|
||||
"""Update lock status."""
|
||||
if time() - self._change_timestamp < 10:
|
||||
if monotonic() - self._change_timestamp < 10:
|
||||
return
|
||||
hub.update_overview()
|
||||
status = hub.get_first(
|
||||
|
@ -131,4 +131,4 @@ class VerisureDoorlock(LockDevice):
|
|||
transaction = hub.session.get_lock_state_transaction(transaction_id)
|
||||
if transaction["result"] == "OK":
|
||||
self._state = state
|
||||
self._change_timestamp = time()
|
||||
self._change_timestamp = monotonic()
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
"""Support for Verisure Smartplugs."""
|
||||
import logging
|
||||
from time import time
|
||||
from time import monotonic
|
||||
|
||||
from homeassistant.components.switch import SwitchDevice
|
||||
|
||||
|
@ -44,7 +44,7 @@ class VerisureSmartplug(SwitchDevice):
|
|||
@property
|
||||
def is_on(self):
|
||||
"""Return true if on."""
|
||||
if time() - self._change_timestamp < 10:
|
||||
if monotonic() - self._change_timestamp < 10:
|
||||
return self._state
|
||||
self._state = (
|
||||
hub.get_first(
|
||||
|
@ -67,13 +67,13 @@ class VerisureSmartplug(SwitchDevice):
|
|||
"""Set smartplug status on."""
|
||||
hub.session.set_smartplug_state(self._device_label, True)
|
||||
self._state = True
|
||||
self._change_timestamp = time()
|
||||
self._change_timestamp = monotonic()
|
||||
|
||||
def turn_off(self, **kwargs):
|
||||
"""Set smartplug status off."""
|
||||
hub.session.set_smartplug_state(self._device_label, False)
|
||||
self._state = False
|
||||
self._change_timestamp = time()
|
||||
self._change_timestamp = monotonic()
|
||||
|
||||
# pylint: disable=no-self-use
|
||||
def update(self):
|
||||
|
|
Loading…
Reference in New Issue