Beispiel #1
0
def setup(opp, config):
    """Set up is called when Open Peer Power is loading our component."""
    dominos = Dominos(opp, config)

    component = EntityComponent(_LOGGER, DOMAIN, opp)
    opp.data[DOMAIN] = {}
    entities = []
    conf = config[DOMAIN]

    opp.services.register(DOMAIN, "order", dominos.handle_order)

    if conf.get(ATTR_SHOW_MENU):
        opp.http.register_view(DominosProductListView(dominos))

    for order_info in conf.get(ATTR_ORDERS):
        order = DominosOrder(order_info, dominos)
        entities.append(order)

    if entities:
        component.add_entities(entities)

    # Return boolean to indicate that initialization was successfully.
    return True
Beispiel #2
0
async def test_update_state_adds_entities(opp):
    """Test if updating poll entities cause an entity to be added works."""
    component = EntityComponent(_LOGGER, DOMAIN, opp)

    ent1 = MockEntity()
    ent2 = MockEntity(should_poll=True)

    await component.async_add_entities([ent2])
    assert len(opp.states.async_entity_ids()) == 1
    ent2.update = lambda *_: component.add_entities([ent1])

    async_fire_time_changed(opp, dt_util.utcnow() + DEFAULT_SCAN_INTERVAL)
    await opp.async_block_till_done()

    assert len(opp.states.async_entity_ids()) == 2
Beispiel #3
0
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