def setup(opp, config): """Set up the Nextcloud integration.""" # Fetch Nextcloud Monitor api data conf = config[DOMAIN] try: ncm = NextcloudMonitor(conf[CONF_URL], conf[CONF_USERNAME], conf[CONF_PASSWORD]) except NextcloudMonitorError: _LOGGER.error("Nextcloud setup failed - Check configuration") opp.data[DOMAIN] = get_data_points(ncm.data) opp.data[DOMAIN]["instance"] = conf[CONF_URL] def nextcloud_update(event_time): """Update data from nextcloud api.""" try: ncm.update() except NextcloudMonitorError: _LOGGER.error("Nextcloud update failed") return False opp.data[DOMAIN] = get_data_points(ncm.data) opp.data[DOMAIN]["instance"] = conf[CONF_URL] # Update sensors on time interval track_time_interval(opp, nextcloud_update, conf[CONF_SCAN_INTERVAL]) for platform in PLATFORMS: discovery.load_platform(opp, platform, DOMAIN, {}, config) return True
def setup_platform(opp, config, add_entities, discovery_info=None): """Set up the Steam platform.""" steam.api.key.set(config.get(CONF_API_KEY)) # Initialize steammods app list before creating sensors # to benefit from internal caching of the list. opp.data[APP_LIST_KEY] = steam.apps.app_list() entities = [ SteamSensor(account, steam) for account in config.get(CONF_ACCOUNTS) ] if not entities: return add_entities(entities, True) # Only one sensor update once every 60 seconds to avoid # flooding steam and getting disconnected. entity_next = 0 @callback def do_update(time): nonlocal entity_next entities[entity_next].async_schedule_update_op_state(True) entity_next = (entity_next + 1) % len(entities) track_time_interval(opp, do_update, BASE_INTERVAL)
def setup(opp, config): """Set up the Repetier Server component.""" opp.data[REPETIER_API] = {} for repetier in config[DOMAIN]: _LOGGER.debug("Repetier server config %s", repetier[CONF_HOST]) url = f"http://{repetier[CONF_HOST]}" port = repetier[CONF_PORT] api_key = repetier[CONF_API_KEY] client = pyrepetier.Repetier(url=url, port=port, apikey=api_key) printers = client.getprinters() if not printers: return False sensors = repetier[CONF_SENSORS][CONF_MONITORED_CONDITIONS] api = PrinterAPI(opp, client, printers, sensors, repetier[CONF_NAME], config) api.update() track_time_interval(opp, api.update, SCAN_INTERVAL) opp.data[REPETIER_API][repetier[CONF_NAME]] = api return True
def __init__(self, opp, config, see, apis): """Initialize Life360Scanner.""" self._opp = opp self._see = see self._max_gps_accuracy = config.get(CONF_MAX_GPS_ACCURACY) self._max_update_wait = config.get(CONF_MAX_UPDATE_WAIT) self._prefix = config[CONF_PREFIX] self._circles_filter = config.get(CONF_CIRCLES) self._members_filter = config.get(CONF_MEMBERS) self._driving_speed = config.get(CONF_DRIVING_SPEED) self._show_as_state = config[CONF_SHOW_AS_STATE] self._apis = apis self._errs = {} self._error_threshold = config[CONF_ERROR_THRESHOLD] self._warning_threshold = config[CONF_WARNING_THRESHOLD] self._max_errs = self._error_threshold + 1 self._dev_data = {} self._circles_logged = set() self._members_logged = set() _dump_filter(self._circles_filter, "Circles") _dump_filter(self._members_filter, "device IDs", self._dev_id) self._started = dt_util.utcnow() self._update_life360() track_time_interval(self._opp, self._update_life360, config[CONF_SCAN_INTERVAL])
def setup(opp, config): """Set up the Hunter Hydrawise component.""" conf = config[DOMAIN] access_token = conf[CONF_ACCESS_TOKEN] scan_interval = conf.get(CONF_SCAN_INTERVAL) try: hydrawise = Hydrawiser(user_token=access_token) opp.data[DATA_HYDRAWISE] = HydrawiseHub(hydrawise) except (ConnectTimeout, HTTPError) as ex: _LOGGER.error("Unable to connect to Hydrawise cloud service: %s", str(ex)) opp.components.persistent_notification.create( f"Error: {ex}<br />You will need to restart opp after fixing.", title=NOTIFICATION_TITLE, notification_id=NOTIFICATION_ID, ) return False def hub_refresh(event_time): """Call Hydrawise hub to refresh information.""" _LOGGER.debug("Updating Hydrawise Hub component") opp.data[DATA_HYDRAWISE].data.update_controller_info() dispatcher_send(opp, SIGNAL_UPDATE_HYDRAWISE) # Call the Hydrawise API to refresh updates track_time_interval(opp, hub_refresh, scan_interval) return True
def setup(opp, config): """Set up the Melnor RainCloud component.""" conf = config[DOMAIN] username = conf.get(CONF_USERNAME) password = conf.get(CONF_PASSWORD) scan_interval = conf.get(CONF_SCAN_INTERVAL) try: raincloud = RainCloudy(username=username, password=password) if not raincloud.is_connected: raise HTTPError opp.data[DATA_RAINCLOUD] = RainCloudHub(raincloud) except (ConnectTimeout, HTTPError) as ex: _LOGGER.error("Unable to connect to Rain Cloud service: %s", str(ex)) opp.components.persistent_notification.create( f"Error: {ex}<br />" "You will need to restart opp after fixing.", title=NOTIFICATION_TITLE, notification_id=NOTIFICATION_ID, ) return False def hub_refresh(event_time): """Call Raincloud hub to refresh information.""" _LOGGER.debug("Updating RainCloud Hub component") opp.data[DATA_RAINCLOUD].data.update() dispatcher_send(opp, SIGNAL_UPDATE_RAINCLOUD) # Call the Raincloud API to refresh updates track_time_interval(opp, hub_refresh, scan_interval) return True
def setup(opp, config): """Set up the smarty environment.""" conf = config[DOMAIN] host = conf[CONF_HOST] name = conf[CONF_NAME] _LOGGER.debug("Name: %s, host: %s", name, host) smarty = Smarty(host=host) opp.data[DOMAIN] = {"api": smarty, "name": name} # Initial update smarty.update() # Load platforms discovery.load_platform(opp, "fan", DOMAIN, {}, config) discovery.load_platform(opp, "sensor", DOMAIN, {}, config) discovery.load_platform(opp, "binary_sensor", DOMAIN, {}, config) def poll_device_update(event_time): """Update Smarty device.""" _LOGGER.debug("Updating Smarty device") if smarty.update(): _LOGGER.debug("Update success") dispatcher_send(opp, SIGNAL_UPDATE_SMARTY) else: _LOGGER.debug("Update failed") track_time_interval(opp, poll_device_update, timedelta(seconds=30)) return True
def _start_recovery(self): self._wrap_event_flag.clear() dispatcher_send(self._opp, service_signal(SERVICE_UPDATE, self._wrap_name)) self._unsub_recheck = track_time_interval(self._opp, self._wrap_test_online, RECHECK_INTERVAL)
def setup(opp, config): """Set up an Arlo component.""" conf = config[DOMAIN] username = conf[CONF_USERNAME] password = conf[CONF_PASSWORD] scan_interval = conf[CONF_SCAN_INTERVAL] try: arlo = PyArlo(username, password, preload=False) if not arlo.is_connected: return False # assign refresh period to base station thread arlo_base_station = next((station for station in arlo.base_stations), None) if arlo_base_station is not None: arlo_base_station.refresh_rate = scan_interval.total_seconds() elif not arlo.cameras: _LOGGER.error("No Arlo camera or base station available") return False opp.data[DATA_ARLO] = arlo except (ConnectTimeout, HTTPError) as ex: _LOGGER.error("Unable to connect to Netgear Arlo: %s", str(ex)) opp.components.persistent_notification.create( f"Error: {ex}<br />You will need to restart opp after fixing.", title=NOTIFICATION_TITLE, notification_id=NOTIFICATION_ID, ) return False def hub_refresh(event_time): """Call ArloHub to refresh information.""" _LOGGER.debug("Updating Arlo Hub component") opp.data[DATA_ARLO].update(update_cameras=True, update_base_station=True) dispatcher_send(opp, SIGNAL_UPDATE_ARLO) # register service opp.services.register(DOMAIN, "update", hub_refresh) # register scan interval for ArloHub track_time_interval(opp, hub_refresh, scan_interval) return True
def __init__(self, opp, config: ConfigType, see) -> None: """Initialize the scanner.""" self.see = see self.username = config[CONF_USERNAME] self.max_gps_accuracy = config[CONF_MAX_GPS_ACCURACY] self.scan_interval = config.get(CONF_SCAN_INTERVAL) or timedelta( seconds=60) self._prev_seen = {} credfile = f"{opp.config.path(CREDENTIALS_FILE)}.{slugify(self.username)}" try: self.service = Service(credfile, self.username) self._update_info() track_time_interval(opp, self._update_info, self.scan_interval) self.success_init = True except InvalidCookies: _LOGGER.error( "The cookie file provided does not provide a valid session. Please create another one and try again" ) self.success_init = False
def setup(opp, config): """Set up the Route53 component.""" domain = config[DOMAIN][CONF_DOMAIN] records = config[DOMAIN][CONF_RECORDS] zone = config[DOMAIN][CONF_ZONE] aws_access_key_id = config[DOMAIN][CONF_ACCESS_KEY_ID] aws_secret_access_key = config[DOMAIN][CONF_SECRET_ACCESS_KEY] ttl = config[DOMAIN][CONF_TTL] def update_records_interval(now): """Set up recurring update.""" _update_route53(aws_access_key_id, aws_secret_access_key, zone, domain, records, ttl) def update_records_service(now): """Set up service for manual trigger.""" _update_route53(aws_access_key_id, aws_secret_access_key, zone, domain, records, ttl) track_time_interval(opp, update_records_interval, INTERVAL) opp.services.register(DOMAIN, "update_records", update_records_service) return True
def _init_regular_updates(self): """Schedule regular updates at the specified interval.""" track_time_interval(self._opp, lambda now: self._feed_manager.update(), self._scan_interval)
def _init_regular_updates(self): """Schedule regular updates based on configured time interval.""" track_time_interval(self._opp, lambda now: self._update(), DEFAULT_UPDATE_INTERVAL)
def _init_regular_updates(self, opp): """Schedule regular updates at the top of the clock.""" track_time_interval(opp, lambda now: self._update(), self._scan_interval)
def setup(opp, config): # noqa: C901 """Set up the Wink component.""" if opp.data.get(DOMAIN) is None: opp.data[DOMAIN] = { "unique_ids": [], "entities": {}, "oauth": {}, "configuring": {}, "pubnub": None, "configurator": False, } if config.get(DOMAIN) is not None: client_id = config[DOMAIN].get(CONF_CLIENT_ID) client_secret = config[DOMAIN].get(CONF_CLIENT_SECRET) email = config[DOMAIN].get(CONF_EMAIL) password = config[DOMAIN].get(CONF_PASSWORD) local_control = config[DOMAIN].get(CONF_LOCAL_CONTROL) else: client_id = None client_secret = None email = None password = None local_control = None opp.data[DOMAIN]["configurator"] = True if None not in [client_id, client_secret]: _LOGGER.info("Using legacy OAuth authentication") if not local_control: pywink.disable_local_control() opp.data[DOMAIN]["oauth"][CONF_CLIENT_ID] = client_id opp.data[DOMAIN]["oauth"][CONF_CLIENT_SECRET] = client_secret opp.data[DOMAIN]["oauth"]["email"] = email opp.data[DOMAIN]["oauth"]["password"] = password pywink.legacy_set_wink_credentials(email, password, client_id, client_secret) else: _LOGGER.info("Using OAuth authentication") if not local_control: pywink.disable_local_control() config_path = opp.config.path(WINK_CONFIG_FILE) if os.path.isfile(config_path): config_file = load_json(config_path) if config_file == DEFAULT_CONFIG: _request_app_setup(opp, config) return True # else move on because the user modified the file else: save_json(config_path, DEFAULT_CONFIG) _request_app_setup(opp, config) return True if DOMAIN in opp.data[DOMAIN]["configuring"]: _configurator = opp.data[DOMAIN]["configuring"] opp.components.configurator.request_done(_configurator.pop(DOMAIN)) # Using oauth access_token = config_file.get(ATTR_ACCESS_TOKEN) refresh_token = config_file.get(ATTR_REFRESH_TOKEN) # This will be called after authorizing Open-Peer-Power if None not in (access_token, refresh_token): pywink.set_wink_credentials( config_file.get(CONF_CLIENT_ID), config_file.get(CONF_CLIENT_SECRET), access_token=access_token, refresh_token=refresh_token, ) # This is called to create the redirect so the user can Authorize # Home . else: redirect_uri = f"{get_url(opp)}{WINK_AUTH_CALLBACK_PATH}" wink_auth_start_url = pywink.get_authorization_url( config_file.get(CONF_CLIENT_ID), redirect_uri) opp.http.register_redirect(WINK_AUTH_START, wink_auth_start_url) opp.http.register_view( WinkAuthCallbackView(config, config_file, pywink.request_token)) _request_oauth_completion(opp, config) return True pywink.set_user_agent(USER_AGENT) sub_details = pywink.get_subscription_details() opp.data[DOMAIN]["pubnub"] = PubNubSubscriptionHandler( sub_details[0], origin=sub_details[1]) def _subscribe(): opp.data[DOMAIN]["pubnub"].subscribe() # Call subscribe after the user sets up wink via the configurator # All other methods will complete setup before # EVENT_OPENPEERPOWER_START is called meaning they # will call subscribe via the method below. (start_subscription) if opp.data[DOMAIN]["configurator"]: _subscribe() def keep_alive_call(event_time): """Call the Wink API endpoints to keep PubNub working.""" _LOGGER.info("Polling the Wink API to keep PubNub updates flowing") pywink.set_user_agent(str(int(time.time()))) _temp_response = pywink.get_user() _LOGGER.debug(str(json.dumps(_temp_response))) time.sleep(1) pywink.set_user_agent(USER_AGENT) _temp_response = pywink.wink_api_fetch() _LOGGER.debug("%s", _temp_response) _temp_response = pywink.post_session() _LOGGER.debug("%s", _temp_response) # Call the Wink API every hour to keep PubNub updates flowing track_time_interval(opp, keep_alive_call, timedelta(minutes=60)) def start_subscription(event): """Start the PubNub subscription.""" _subscribe() opp.bus.listen(EVENT_OPENPEERPOWER_START, start_subscription) def stop_subscription(event): """Stop the PubNub subscription.""" opp.data[DOMAIN]["pubnub"].unsubscribe() opp.data[DOMAIN]["pubnub"] = None opp.bus.listen(EVENT_OPENPEERPOWER_STOP, stop_subscription) def save_credentials(event): """Save currently set OAuth credentials.""" if opp.data[DOMAIN]["oauth"].get("email") is None: config_path = opp.config.path(WINK_CONFIG_FILE) _config = pywink.get_current_oauth_credentials() save_json(config_path, _config) opp.bus.listen(EVENT_OPENPEERPOWER_STOP, save_credentials) # Save the users potentially updated oauth credentials at a regular # interval to prevent them from being expired after a OPP reboot. track_time_interval(opp, save_credentials, timedelta(minutes=60)) def force_update(call): """Force all devices to poll the Wink API.""" _LOGGER.info("Refreshing Wink states from API") for entity_list in opp.data[DOMAIN]["entities"].values(): # Throttle the calls to Wink API for entity in entity_list: time.sleep(1) entity.schedule_update_op_state(True) opp.services.register(DOMAIN, SERVICE_REFRESH_STATES, force_update) def pull_new_devices(call): """Pull new devices added to users Wink account since startup.""" _LOGGER.info("Getting new devices from Wink API") for _component in WINK_COMPONENTS: discovery.load_platform(opp, _component, DOMAIN, {}, config) opp.services.register(DOMAIN, SERVICE_ADD_NEW_DEVICES, pull_new_devices) def set_pairing_mode(call): """Put the hub in provided pairing mode.""" hub_name = call.data.get("hub_name") pairing_mode = call.data.get("pairing_mode") kidde_code = call.data.get("kidde_radio_code") for hub in WINK_HUBS: if hub.name() == hub_name: hub.pair_new_device(pairing_mode, kidde_radio_code=kidde_code) def rename_device(call): """Set specified device's name.""" # This should only be called on one device at a time. found_device = None entity_id = call.data.get("entity_id")[0] all_devices = [] for list_of_devices in opp.data[DOMAIN]["entities"].values(): all_devices += list_of_devices for device in all_devices: if device.entity_id == entity_id: found_device = device if found_device is not None: name = call.data.get("name") found_device.wink.set_name(name) opp.services.register(DOMAIN, SERVICE_RENAME_DEVICE, rename_device, schema=RENAME_DEVICE_SCHEMA) def delete_device(call): """Delete specified device.""" # This should only be called on one device at a time. found_device = None entity_id = call.data.get("entity_id")[0] all_devices = [] for list_of_devices in opp.data[DOMAIN]["entities"].values(): all_devices += list_of_devices for device in all_devices: if device.entity_id == entity_id: found_device = device if found_device is not None: found_device.wink.remove_device() opp.services.register(DOMAIN, SERVICE_DELETE_DEVICE, delete_device, schema=DELETE_DEVICE_SCHEMA) hubs = pywink.get_hubs() for hub in hubs: if hub.device_manufacturer() == "wink": WINK_HUBS.append(hub) if WINK_HUBS: opp.services.register( DOMAIN, SERVICE_SET_PAIRING_MODE, set_pairing_mode, schema=SET_PAIRING_MODE_SCHEMA, ) def nimbus_service_handle(service): """Handle nimbus services.""" entity_id = service.data.get("entity_id")[0] _all_dials = [] for sensor in opp.data[DOMAIN]["entities"]["sensor"]: if isinstance(sensor, WinkNimbusDialDevice): _all_dials.append(sensor) for _dial in _all_dials: if _dial.entity_id == entity_id: if service.service == SERVICE_SET_DIAL_CONFIG: _dial.set_configuration(**service.data) if service.service == SERVICE_SET_DIAL_STATE: _dial.wink.set_state(service.data.get("value"), service.data.get("labels")) def siren_service_handle(service): """Handle siren services.""" entity_ids = service.data.get("entity_id") all_sirens = [] for switch in opp.data[DOMAIN]["entities"]["switch"]: if isinstance(switch, WinkSirenDevice): all_sirens.append(switch) sirens_to_set = [] if entity_ids is None: sirens_to_set = all_sirens else: for siren in all_sirens: if siren.entity_id in entity_ids: sirens_to_set.append(siren) for siren in sirens_to_set: _man = siren.wink.device_manufacturer() if (service.service != SERVICE_SET_AUTO_SHUTOFF and service.service != SERVICE_ENABLE_SIREN and _man not in ("dome", "wink")): _LOGGER.error("Service only valid for Dome or Wink sirens") return if service.service == SERVICE_ENABLE_SIREN: siren.wink.set_state(service.data.get(ATTR_ENABLED)) elif service.service == SERVICE_SET_AUTO_SHUTOFF: siren.wink.set_auto_shutoff( service.data.get(ATTR_AUTO_SHUTOFF)) elif service.service == SERVICE_SET_CHIME_VOLUME: siren.wink.set_chime_volume(service.data.get(ATTR_VOLUME)) elif service.service == SERVICE_SET_SIREN_VOLUME: siren.wink.set_siren_volume(service.data.get(ATTR_VOLUME)) elif service.service == SERVICE_SET_SIREN_TONE: siren.wink.set_siren_sound(service.data.get(ATTR_TONE)) elif service.service == SERVICE_ENABLE_CHIME: siren.wink.set_chime(service.data.get(ATTR_TONE)) elif service.service == SERVICE_SIREN_STROBE_ENABLED: siren.wink.set_siren_strobe_enabled( service.data.get(ATTR_ENABLED)) elif service.service == SERVICE_CHIME_STROBE_ENABLED: siren.wink.set_chime_strobe_enabled( service.data.get(ATTR_ENABLED)) # Load components for the devices in Wink that we support for wink_component in WINK_COMPONENTS: opp.data[DOMAIN]["entities"][wink_component] = [] discovery.load_platform(opp, wink_component, DOMAIN, {}, config) component = EntityComponent(_LOGGER, DOMAIN, opp) sirens = [] has_dome_or_wink_siren = False for siren in pywink.get_sirens(): _man = siren.device_manufacturer() if _man in ("dome", "wink"): has_dome_or_wink_siren = True _id = siren.object_id() + siren.name() if _id not in opp.data[DOMAIN]["unique_ids"]: sirens.append(WinkSirenDevice(siren, opp)) if sirens: opp.services.register( DOMAIN, SERVICE_SET_AUTO_SHUTOFF, siren_service_handle, schema=SET_AUTO_SHUTOFF_SCHEMA, ) opp.services.register( DOMAIN, SERVICE_ENABLE_SIREN, siren_service_handle, schema=ENABLED_SIREN_SCHEMA, ) if has_dome_or_wink_siren: opp.services.register( DOMAIN, SERVICE_SET_SIREN_TONE, siren_service_handle, schema=SET_SIREN_TONE_SCHEMA, ) opp.services.register( DOMAIN, SERVICE_ENABLE_CHIME, siren_service_handle, schema=SET_CHIME_MODE_SCHEMA, ) opp.services.register( DOMAIN, SERVICE_SET_SIREN_VOLUME, siren_service_handle, schema=SET_VOLUME_SCHEMA, ) opp.services.register( DOMAIN, SERVICE_SET_CHIME_VOLUME, siren_service_handle, schema=SET_VOLUME_SCHEMA, ) opp.services.register( DOMAIN, SERVICE_SIREN_STROBE_ENABLED, siren_service_handle, schema=SET_STROBE_ENABLED_SCHEMA, ) opp.services.register( DOMAIN, SERVICE_CHIME_STROBE_ENABLED, siren_service_handle, schema=SET_STROBE_ENABLED_SCHEMA, ) component.add_entities(sirens) nimbi = [] dials = {} all_nimbi = pywink.get_cloud_clocks() all_dials = [] for nimbus in all_nimbi: if nimbus.object_type() == "cloud_clock": nimbi.append(nimbus) dials[nimbus.object_id()] = [] for nimbus in all_nimbi: if nimbus.object_type() == "dial": dials[nimbus.parent_id()].append(nimbus) for nimbus in nimbi: for dial in dials[nimbus.object_id()]: all_dials.append(WinkNimbusDialDevice(nimbus, dial, opp)) if nimbi: opp.services.register( DOMAIN, SERVICE_SET_DIAL_CONFIG, nimbus_service_handle, schema=DIAL_CONFIG_SCHEMA, ) opp.services.register( DOMAIN, SERVICE_SET_DIAL_STATE, nimbus_service_handle, schema=DIAL_STATE_SCHEMA, ) component.add_entities(all_dials) return True