Renamed AddCooldown to Throttle and added bypass functionality
This commit is contained in:
1 parent
31b9f65513
commit
48089b01ab
7 files changed
+91
-65
No files matched your search
@@ -87,7 +87,7 @@ class LuciDeviceScanner(object):
|
||||
return
|
||||
return self.mac2name.get(device, None)
|
||||
|
||||
@util.AddCooldown(MIN_TIME_BETWEEN_SCANS)
|
||||
@util.Throttle(MIN_TIME_BETWEEN_SCANS)
|
||||
def _update_info(self):
|
||||
""" Ensures the information from the Luci router is up to date.
|
||||
Returns boolean if scanning successful. """
|
||||
|
||||
@@ -82,7 +82,7 @@ class NetgearDeviceScanner(object):
|
||||
else:
|
||||
return None
|
||||
|
||||
@util.AddCooldown(MIN_TIME_BETWEEN_SCANS)
|
||||
@util.Throttle(MIN_TIME_BETWEEN_SCANS)
|
||||
def _update_info(self):
|
||||
""" Retrieves latest information from the Netgear router.
|
||||
Returns boolean if scanning successful. """
|
||||
|
||||
@@ -78,7 +78,7 @@ class TomatoDeviceScanner(object):
|
||||
else:
|
||||
return filter_named[0]
|
||||
|
||||
@util.AddCooldown(MIN_TIME_BETWEEN_SCANS)
|
||||
@util.Throttle(MIN_TIME_BETWEEN_SCANS)
|
||||
def _update_tomato_info(self):
|
||||
""" Ensures the information from the Tomato router is up to date.
|
||||
Returns boolean if scanning successful. """
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
""" Support for Hue lights. """
|
||||
import logging
|
||||
import socket
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import timedelta
|
||||
|
||||
import homeassistant as ha
|
||||
import homeassistant.util as util
|
||||
from homeassistant.components import ToggleDevice, ATTR_FRIENDLY_NAME
|
||||
from homeassistant.components.light import (
|
||||
ATTR_BRIGHTNESS, ATTR_XY_COLOR, ATTR_TRANSITION)
|
||||
|
||||
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
||||
MIN_TIME_BETWEEN_FORCED_SCANS = timedelta(seconds=1)
|
||||
|
||||
PHUE_CONFIG_FILE = "phue.conf"
|
||||
|
||||
@@ -37,25 +39,9 @@ def get_lights(hass, config):
|
||||
|
||||
lights = {}
|
||||
|
||||
def update_lights(force_reload=False):
|
||||
""" Updates the light states. """
|
||||
now = datetime.now()
|
||||
|
||||
try:
|
||||
time_scans = now - update_lights.last_updated
|
||||
|
||||
# force_reload == True, return if updated in last second
|
||||
# force_reload == False, return if last update was less then
|
||||
# MIN_TIME_BETWEEN_SCANS ago
|
||||
if force_reload and time_scans.seconds < 1 or \
|
||||
not force_reload and time_scans < MIN_TIME_BETWEEN_SCANS:
|
||||
return
|
||||
except AttributeError:
|
||||
# First time we run last_updated is not set, continue as usual
|
||||
pass
|
||||
|
||||
update_lights.last_updated = now
|
||||
|
||||
@util.Throttle(MIN_TIME_BETWEEN_SCANS, MIN_TIME_BETWEEN_FORCED_SCANS)
|
||||
def update_lights():
|
||||
""" Updates the Hue light objects with latest info from the bridge. """
|
||||
try:
|
||||
api = bridge.get_api()
|
||||
except socket.error:
|
||||
@@ -142,4 +128,4 @@ class HueLight(ToggleDevice):
|
||||
|
||||
def update(self):
|
||||
""" Synchronize state with bridge. """
|
||||
self.update_lights(True)
|
||||
self.update_lights(no_throttle=True)
|
||||
@@ -4,7 +4,7 @@ homeassistant.components.switch
|
||||
Component to interface with various switches that can be controlled remotely.
|
||||
"""
|
||||
import logging
|
||||
from datetime import datetime, timedelta
|
||||
from datetime import timedelta
|
||||
|
||||
import homeassistant as ha
|
||||
import homeassistant.util as util
|
||||
@@ -96,21 +96,16 @@ def setup(hass, config):
|
||||
ent_to_switch[entity_id] = switch
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def update_states(time, force_reload=False):
|
||||
@util.Throttle(MIN_TIME_BETWEEN_SCANS)
|
||||
def update_states(now):
|
||||
""" Update states of all switches. """
|
||||
|
||||
# First time this method gets called, force_reload should be True
|
||||
if force_reload or \
|
||||
datetime.now() - update_states.last_updated > \
|
||||
MIN_TIME_BETWEEN_SCANS:
|
||||
logger.info("Updating switch states")
|
||||
|
||||
logger.info("Updating switch states")
|
||||
update_states.last_updated = datetime.now()
|
||||
for switch in switches:
|
||||
switch.update_ha_state(hass)
|
||||
|
||||
for switch in switches:
|
||||
switch.update_ha_state(hass)
|
||||
|
||||
update_states(None, True)
|
||||
update_states(None)
|
||||
|
||||
def handle_switch_service(service):
|
||||
""" Handles calls to the switch services. """
|
||||
|
||||
Reference in new issue
Block a user