def trigger(hass, config, action): """Listen for state changes based on configuration.""" if CONF_AFTER in config: after = config.get(CONF_AFTER) hours, minutes, seconds = after.hour, after.minute, after.second else: hours = config.get(CONF_HOURS) minutes = config.get(CONF_MINUTES) seconds = config.get(CONF_SECONDS) def time_automation_listener(now): """Listen for time changes and calls action.""" action({ 'trigger': { 'platform': 'time', 'now': now, }, }) track_time_change(hass, time_automation_listener, hour=hours, minute=minutes, second=seconds) return True
def __init__(self, hass, config): """Initialize the data object.""" self.data = None track_time_change(hass, self.update, second=config.get(CONF_SECOND, 0), minute=config.get(CONF_MINUTE, 0), hour=config.get(CONF_HOUR, None), day=config.get(CONF_DAY, None))
def setup_platform(hass, config, add_devices_callback, discovery_info=None): """Setup the LIFX platform.""" server_addr = config.get(CONF_SERVER, None) broadcast_addr = config.get(CONF_BROADCAST, None) lifx_library = LIFX(add_devices_callback, server_addr, broadcast_addr) # Register our poll service track_time_change(hass, lifx_library.poll, second=[10, 40]) lifx_library.probe()
def setup(hass, config): """Setup the updater component.""" if 'master' in CURRENT_VERSION: # This component only makes sense in release versions _LOGGER.warning('Updater not supported in development version') return False def check_newest_version(_=None): """Check if a new version is available and report if one is.""" newest = get_newest_version() if newest != CURRENT_VERSION and newest is not None: hass.states.set( ENTITY_ID, newest, {ATTR_FRIENDLY_NAME: 'Update Available'}) event.track_time_change(hass, check_newest_version, hour=[0, 12], minute=0, second=0) check_newest_version() return True
def test_track_time_change(self): """Test tracking time change.""" wildcard_runs = [] specific_runs = [] track_time_change(self.hass, lambda x: wildcard_runs.append(1)) track_utc_time_change( self.hass, lambda x: specific_runs.append(1), second=[0, 30]) self._send_time_changed(datetime(2014, 5, 24, 12, 0, 0)) self.hass.pool.block_till_done() self.assertEqual(1, len(specific_runs)) self.assertEqual(1, len(wildcard_runs)) self._send_time_changed(datetime(2014, 5, 24, 12, 0, 15)) self.hass.pool.block_till_done() self.assertEqual(1, len(specific_runs)) self.assertEqual(2, len(wildcard_runs)) self._send_time_changed(datetime(2014, 5, 24, 12, 0, 30)) self.hass.pool.block_till_done() self.assertEqual(2, len(specific_runs)) self.assertEqual(3, len(wildcard_runs))
def test_track_time_change(self): """Test tracking time change.""" wildcard_runs = [] specific_runs = [] track_time_change(self.hass, lambda x: wildcard_runs.append(1)) track_utc_time_change(self.hass, lambda x: specific_runs.append(1), second=[0, 30]) self._send_time_changed(datetime(2014, 5, 24, 12, 0, 0)) self.hass.pool.block_till_done() self.assertEqual(1, len(specific_runs)) self.assertEqual(1, len(wildcard_runs)) self._send_time_changed(datetime(2014, 5, 24, 12, 0, 15)) self.hass.pool.block_till_done() self.assertEqual(1, len(specific_runs)) self.assertEqual(2, len(wildcard_runs)) self._send_time_changed(datetime(2014, 5, 24, 12, 0, 30)) self.hass.pool.block_till_done() self.assertEqual(2, len(specific_runs)) self.assertEqual(3, len(wildcard_runs))
def setup(hass, config): """Setup the updater component.""" if 'master' in CURRENT_VERSION: # This component only makes sense in release versions _LOGGER.warning('Updater not supported in development version') return False def check_newest_version(_=None): """Check if a new version is available and report if one is.""" newest = get_newest_version() if newest != CURRENT_VERSION and newest is not None: hass.states.set(ENTITY_ID, newest, {ATTR_FRIENDLY_NAME: 'Update Available'}) event.track_time_change(hass, check_newest_version, hour=[0, 12], minute=0, second=0) check_newest_version() return True
def track_time_change_decorator(action): """Decorator to track time changes.""" event.track_time_change(HASS, functools.partial(action, HASS), year, month, day, hour, minute, second) return action
def setup(hass, config): """Setup Z-Wave. Will automatically load components to support devices found on the network. """ # pylint: disable=global-statement, import-error global NETWORK try: import libopenzwave except ImportError: _LOGGER.error("You are missing required dependency Python Open " "Z-Wave. Please follow instructions at: " "https://home-assistant.io/components/zwave/") return False from pydispatch import dispatcher from openzwave.option import ZWaveOption from openzwave.network import ZWaveNetwork default_zwave_config_path = os.path.join( os.path.dirname(libopenzwave.__file__), 'config') # Load configuration use_debug = str(config[DOMAIN].get(CONF_DEBUG)) == '1' customize = config[DOMAIN].get(CONF_CUSTOMIZE, {}) autoheal = config[DOMAIN].get(CONF_AUTOHEAL, DEFAULT_CONF_AUTOHEAL) # Setup options options = ZWaveOption( config[DOMAIN].get(CONF_USB_STICK_PATH, DEFAULT_CONF_USB_STICK_PATH), user_path=hass.config.config_dir, config_path=config[DOMAIN].get('config_path', default_zwave_config_path), ) options.set_console_output(use_debug) options.lock() NETWORK = ZWaveNetwork(options, autostart=False) if use_debug: def log_all(signal, value=None): """Log all the signals.""" print("") print("SIGNAL *****", signal) if value and signal in (ZWaveNetwork.SIGNAL_VALUE_CHANGED, ZWaveNetwork.SIGNAL_VALUE_ADDED): pprint(_obj_to_dict(value)) print("") dispatcher.connect(log_all, weak=False) def value_added(node, value): """Called when a value is added to a node on the network.""" for (component, discovery_service, command_ids, value_type, value_genre) in DISCOVERY_COMPONENTS: if value.command_class not in command_ids: continue if value_type is not None and value_type != value.type: continue if value_genre is not None and value_genre != value.genre: continue # Ensure component is loaded bootstrap.setup_component(hass, component, config) # Configure node name = "{}.{}".format(component, _object_id(value)) node_config = customize.get(name, {}) polling_intensity = convert( node_config.get(CONF_POLLING_INTENSITY), int) if polling_intensity: value.enable_poll(polling_intensity) else: value.disable_poll() # Fire discovery event hass.bus.fire( EVENT_PLATFORM_DISCOVERED, { ATTR_SERVICE: discovery_service, ATTR_DISCOVERED: { ATTR_NODE_ID: node.node_id, ATTR_VALUE_ID: value.value_id, } }) def scene_activated(node, scene_id): """Called when a scene is activated on any node in the network.""" name = _node_name(node) object_id = "{}_{}".format(slugify(name), node.node_id) hass.bus.fire(EVENT_SCENE_ACTIVATED, { ATTR_ENTITY_ID: object_id, ATTR_SCENE_ID: scene_id }) dispatcher.connect(value_added, ZWaveNetwork.SIGNAL_VALUE_ADDED, weak=False) dispatcher.connect(scene_activated, ZWaveNetwork.SIGNAL_SCENE_EVENT, weak=False) def add_node(service): """Switch into inclusion mode.""" NETWORK.controller.begin_command_add_device() def remove_node(service): """Switch into exclusion mode.""" NETWORK.controller.begin_command_remove_device() def heal_network(service): """Heal the network.""" _LOGGER.info("ZWave heal running.") NETWORK.heal() def soft_reset(service): """Soft reset the controller.""" NETWORK.controller.soft_reset() def test_network(service): """Test the network by sending commands to all the nodes.""" NETWORK.test() def stop_zwave(event): """Stop Z-Wave.""" NETWORK.stop() def start_zwave(event): """Startup Z-Wave.""" NETWORK.start() # Need to be in STATE_AWAKED before talking to nodes. # Wait up to NETWORK_READY_WAIT_SECS seconds for the zwave network # to be ready. for i in range(NETWORK_READY_WAIT_SECS): _LOGGER.debug("network state: %d %s", NETWORK.state, NETWORK.state_str) if NETWORK.state >= NETWORK.STATE_AWAKED: _LOGGER.info("zwave ready after %d seconds", i) break time.sleep(1) else: _LOGGER.warning( "zwave not ready after %d seconds, continuing anyway", NETWORK_READY_WAIT_SECS) _LOGGER.info("final network state: %d %s", NETWORK.state, NETWORK.state_str) polling_interval = convert(config[DOMAIN].get(CONF_POLLING_INTERVAL), int) if polling_interval is not None: NETWORK.set_poll_interval(polling_interval, False) poll_interval = NETWORK.get_poll_interval() _LOGGER.info("zwave polling interval set to %d ms", poll_interval) hass.bus.listen_once(EVENT_BLUMATE_STOP, stop_zwave) # Register add / remove node services for Z-Wave sticks without # hardware inclusion button hass.services.register(DOMAIN, SERVICE_ADD_NODE, add_node) hass.services.register(DOMAIN, SERVICE_REMOVE_NODE, remove_node) hass.services.register(DOMAIN, SERVICE_HEAL_NETWORK, heal_network) hass.services.register(DOMAIN, SERVICE_SOFT_RESET, soft_reset) hass.services.register(DOMAIN, SERVICE_TEST_NETWORK, test_network) # Setup autoheal if autoheal: _LOGGER.info("ZWave network autoheal is enabled.") track_time_change(hass, heal_network, hour=0, minute=0, second=0) hass.bus.listen_once(EVENT_BLUMATE_START, start_zwave) return True