Example #1
0
def test_load_bad_data():
    """Test error from trying to load unserialisable data."""
    fname = _path_for("test5")
    with open(fname, "w") as fh:
        fh.write(TEST_BAD_SERIALIED)
    with pytest.raises(OpenPeerPowerError):
        load_json(fname)
Example #2
0
def setup_platform(opp, config, add_entities, discovery_info=None):
    """Set up the Nanoleaf light."""

    if DATA_NANOLEAF not in opp.data:
        opp.data[DATA_NANOLEAF] = {}

    token = ""
    if discovery_info is not None:
        host = discovery_info["host"]
        name = None
        device_id = discovery_info["properties"]["id"]

        # if device already exists via config, skip discovery setup
        if host in opp.data[DATA_NANOLEAF]:
            return
        _LOGGER.info("Discovered a new Nanoleaf: %s", discovery_info)
        conf = load_json(opp.config.path(CONFIG_FILE))
        if host in conf and device_id not in conf:
            conf[device_id] = conf.pop(host)
            save_json(opp.config.path(CONFIG_FILE), conf)
        token = conf.get(device_id, {}).get("token", "")
    else:
        host = config[CONF_HOST]
        name = config[CONF_NAME]
        token = config[CONF_TOKEN]

    nanoleaf_light = Nanoleaf(host)

    if not token:
        token = nanoleaf_light.request_token()
        if not token:
            _LOGGER.error(
                "Could not generate the auth token, did you press "
                "and hold the power button on %s"
                "for 5-7 seconds?",
                name,
            )
            return
        conf = load_json(opp.config.path(CONFIG_FILE))
        conf[host] = {"token": token}
        save_json(opp.config.path(CONFIG_FILE), conf)

    nanoleaf_light.token = token

    try:
        info = nanoleaf_light.info
    except Unavailable:
        _LOGGER.error("Could not connect to Nanoleaf Light: %s on %s", name,
                      host)
        return

    if name is None:
        name = info.name

    opp.data[DATA_NANOLEAF][host] = nanoleaf_light
    add_entities([NanoleafLight(nanoleaf_light, name)], True)
Example #3
0
def test_overwrite_and_reload():
    """Test that we can overwrite an existing file and read back."""
    fname = _path_for("test3")
    save_json(fname, TEST_JSON_A)
    save_json(fname, TEST_JSON_B)
    data = load_json(fname)
    assert data == TEST_JSON_B
Example #4
0
 def success():
     """Signal successful setup."""
     conf = load_json(opp.config.path(CONFIG_FILE))
     conf[host] = {CONF_API_KEY: api_key}
     save_json(opp.config.path(CONFIG_FILE), conf)
     req_config = _CONFIGURING.pop(host)
     configurator.request_done(req_config)
Example #5
0
def test_save_and_load_private():
    """Test we can load private files and that they are protected."""
    fname = _path_for("test2")
    save_json(fname, TEST_JSON_A, private=True)
    data = load_json(fname)
    assert data == TEST_JSON_A
    stats = os.stat(fname)
    assert stats.st_mode & 0o77 == 0
Example #6
0
    def load_old_config():
        """Load old config."""
        if not os.path.isfile(old_path):
            return None

        if old_conf_load_func is not None:
            return old_conf_load_func(old_path)

        return json_util.load_json(old_path)
def load_translations_files(
        translation_files: Dict[str, str]) -> Dict[str, Dict[str, Any]]:
    """Load and parse translation.json files."""
    loaded = {}
    for component, translation_file in translation_files.items():
        loaded_json = load_json(translation_file)
        assert isinstance(loaded_json, dict)
        loaded[component] = loaded_json

    return loaded
Example #8
0
def test_custom_encoder():
    """Test serializing with a custom encoder."""
    class MockJSONEncoder(JSONEncoder):
        """Mock JSON encoder."""
        def default(self, o):
            """Mock JSON encode method."""
            return "9"

    fname = _path_for("test6")
    save_json(fname, Mock(), encoder=MockJSONEncoder)
    data = load_json(fname)
    assert data == "9"
Example #9
0
def setup_platform(opp, config, add_entities, discovery_info=None):
    """Set up the GPMDP platform."""
    codeconfig = load_json(opp.config.path(GPMDP_CONFIG_FILE))
    if codeconfig:
        code = codeconfig.get("CODE")
    elif discovery_info is not None:
        if "gpmdp" in _CONFIGURING:
            return
        code = None
    else:
        code = None
    setup_gpmdp(opp, config, code, add_entities)
Example #10
0
 def fitbit_configuration_callback(fields: list[dict[str, str]]) -> None:
     """Handle configuration updates."""
     config_path = opp.config.path(FITBIT_CONFIG_FILE)
     if os.path.isfile(config_path):
         config_file = load_json(config_path)
         if config_file == DEFAULT_CONFIG:
             error_msg = (
                 "You didn't correctly modify fitbit.conf",
                 " please try again",
             )
             configurator.notify_errors(_CONFIGURING["fitbit"], error_msg)
         else:
             setup_platform(opp, config, add_entities, discovery_info)
     else:
         setup_platform(opp, config, add_entities, discovery_info)
Example #11
0
    def _get_auth_tokens(self):
        """
        Read sorted authentication tokens from disk.

        Returns the auth_tokens dictionary.
        """
        try:
            auth_tokens = load_json(self._session_filepath)

            return auth_tokens
        except OpenPeerPowerError as ex:
            _LOGGER.warning(
                "Loading authentication tokens from file '%s' failed: %s",
                self._session_filepath,
                str(ex),
            )
            return {}
Example #12
0
def load_translations_files(
        translation_files: dict[str, str]) -> dict[str, dict[str, Any]]:
    """Load and parse translation.json files."""
    loaded = {}
    for component, translation_file in translation_files.items():
        loaded_json = load_json(translation_file)

        if not isinstance(loaded_json, dict):
            _LOGGER.warning(
                "Translation file is unexpected type %s. Expected dict for %s",
                type(loaded_json),
                translation_file,
            )
            continue

        loaded[component] = loaded_json

    return loaded
Example #13
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
Example #14
0
def test_save_and_load():
    """Test saving and loading back."""
    fname = _path_for("test1")
    save_json(fname, TEST_JSON_A)
    data = load_json(fname)
    assert data == TEST_JSON_A
Example #15
0
 def load():
     """Load the items synchronously."""
     return load_json(self.opp.config.path(PERSISTENCE), default=[])
Example #16
0
def _load_config(filename):
    """Load configuration."""
    with suppress(OpenPeerPowerError):
        return load_json(filename)
    return {}
Example #17
0
def setup_platform(
    opp: OpenPeerPower,
    config: ConfigType,
    add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:
    """Set up the Fitbit sensor."""
    config_path = opp.config.path(FITBIT_CONFIG_FILE)
    if os.path.isfile(config_path):
        config_file: ConfigType = cast(ConfigType, load_json(config_path))
        if config_file == DEFAULT_CONFIG:
            request_app_setup(opp,
                              config,
                              add_entities,
                              config_path,
                              discovery_info=None)
            return
    else:
        save_json(config_path, DEFAULT_CONFIG)
        request_app_setup(opp,
                          config,
                          add_entities,
                          config_path,
                          discovery_info=None)
        return

    if "fitbit" in _CONFIGURING:
        opp.components.configurator.request_done(_CONFIGURING.pop("fitbit"))

    access_token: str | None = config_file.get(ATTR_ACCESS_TOKEN)
    refresh_token: str | None = config_file.get(ATTR_REFRESH_TOKEN)
    expires_at: int | None = config_file.get(ATTR_LAST_SAVED_AT)
    if (access_token is not None and refresh_token is not None
            and expires_at is not None):
        authd_client = Fitbit(
            config_file.get(CONF_CLIENT_ID),
            config_file.get(CONF_CLIENT_SECRET),
            access_token=access_token,
            refresh_token=refresh_token,
            expires_at=expires_at,
            refresh_cb=lambda x: None,
        )

        if int(time.time()) - expires_at > 3600:
            authd_client.client.refresh_token()

        unit_system = config.get(CONF_UNIT_SYSTEM)
        if unit_system == "default":
            authd_client.system = authd_client.user_profile_get(
            )["user"]["locale"]
            if authd_client.system != "en_GB":
                if opp.config.units.is_metric:
                    authd_client.system = "metric"
                else:
                    authd_client.system = "en_US"
        else:
            authd_client.system = unit_system

        dev = []
        registered_devs = authd_client.get_devices()
        clock_format = config.get(CONF_CLOCK_FORMAT, DEFAULT_CLOCK_FORMAT)
        for resource in config.get(CONF_MONITORED_RESOURCES,
                                   FITBIT_DEFAULT_RESOURCES):

            # monitor battery for all linked FitBit devices
            if resource == "devices/battery":
                for dev_extra in registered_devs:
                    dev.append(
                        FitbitSensor(
                            authd_client,
                            config_path,
                            resource,
                            opp.config.units.is_metric,
                            clock_format,
                            dev_extra,
                        ))
            else:
                dev.append(
                    FitbitSensor(
                        authd_client,
                        config_path,
                        resource,
                        opp.config.units.is_metric,
                        clock_format,
                    ))
        add_entities(dev, True)

    else:
        oauth = FitbitOauth2Client(config_file.get(CONF_CLIENT_ID),
                                   config_file.get(CONF_CLIENT_SECRET))

        redirect_uri = f"{get_url(opp)}{FITBIT_AUTH_CALLBACK_PATH}"

        fitbit_auth_start_url, _ = oauth.authorize_token_url(
            redirect_uri=redirect_uri,
            scope=[
                "activity",
                "heartrate",
                "nutrition",
                "profile",
                "settings",
                "sleep",
                "weight",
            ],
        )

        opp.http.register_redirect(FITBIT_AUTH_START, fitbit_auth_start_url)
        opp.http.register_view(
            FitbitAuthCallbackView(config, add_entities, oauth))

        request_oauth_completion(opp)