コード例 #1
0
    def _write_data(self, path: str, data: dict) -> None:
        """Write the data."""
        if not os.path.isdir(os.path.dirname(path)):
            os.makedirs(os.path.dirname(path))

        _LOGGER.debug("Writing data for %s to %s", self.key, path)
        json_util.save_json(path, data, self._private, encoder=self._encoder)
コード例 #2
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
コード例 #3
0
    async def post(self, request):
        """Handle the POST request for device identification."""
        try:
            data = await request.json()
        except ValueError:
            return self.json_message("Invalid JSON", HTTP_BAD_REQUEST)

        opp = request.app["opp"]

        # Commented for now while iOS app is getting frequent updates
        # try:
        #     data = IDENTIFY_SCHEMA(req_data)
        # except vol.Invalid as ex:
        #     return self.json_message(
        #         vol.humanize.humanize_error(request.json, ex),
        #         HTTP_BAD_REQUEST)

        data[ATTR_LAST_SEEN_AT] = datetime.datetime.now().isoformat()

        device_id = data[ATTR_DEVICE_ID]

        opp.data[DOMAIN][ATTR_DEVICES][device_id] = data

        async_dispatcher_send(opp, f"{DOMAIN}.{device_id}", data)

        try:
            save_json(self._config_path, opp.data[DOMAIN])
        except OpenPeerPowerError:
            return self.json_message("Error saving device.",
                                     HTTP_INTERNAL_SERVER_ERROR)

        return self.json({"status": "registered"})
コード例 #4
0
    def get(self, request):
        """Finish OAuth callback request."""
        opp = request.app["opp"]
        data = request.query

        response_message = """Wink has been successfully authorized!
         You can close this window now! For the best results you should reboot
         Open Peer Power"""
        html_response = """<html><head><title>Wink Auth</title></head>
                <body><h1>{}</h1></body></html>"""

        if data.get("code") is not None:
            response = self.request_token(data.get("code"),
                                          self.config_file[CONF_CLIENT_SECRET])

            config_contents = {
                ATTR_ACCESS_TOKEN: response["access_token"],
                ATTR_REFRESH_TOKEN: response["refresh_token"],
                CONF_CLIENT_ID: self.config_file[CONF_CLIENT_ID],
                CONF_CLIENT_SECRET: self.config_file[CONF_CLIENT_SECRET],
            }
            save_json(opp.config.path(WINK_CONFIG_FILE), config_contents)

            opp.async_add_job(setup, opp, self.config)

            return Response(text=html_response.format(response_message),
                            content_type="text/html")

        error_msg = "No code returned from Wink API"
        _LOGGER.error(error_msg)
        return Response(text=html_response.format(error_msg),
                        content_type="text/html")
コード例 #5
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)
コード例 #6
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
コード例 #7
0
def test_save_bad_data():
    """Test error from trying to save unserialisable data."""
    with pytest.raises(SerializationError) as excinfo:
        save_json("test4", {"hello": set()})

    assert (
        "Failed to serialize to JSON: test4. Bad data at $.hello=set()(<class 'set'>"
        in str(excinfo.value))
コード例 #8
0
ファイル: sensor.py プロジェクト: OpenPeerPower/core
    async def get(self, request: Request) -> str:
        """Finish OAuth callback request."""
        opp: OpenPeerPower = request.app["opp"]
        data = request.query

        response_message = """Fitbit has been successfully authorized!
        You can close this window now!"""

        result = None
        if data.get("code") is not None:
            redirect_uri = f"{get_url(opp, require_current_request=True)}{FITBIT_AUTH_CALLBACK_PATH}"

            try:
                result = await opp.async_add_executor_job(
                    self.oauth.fetch_access_token, data.get("code"),
                    redirect_uri)
            except MissingTokenError as error:
                _LOGGER.error("Missing token: %s", error)
                response_message = f"""Something went wrong when
                attempting authenticating with Fitbit. The error
                encountered was {error}. Please try again!"""
            except MismatchingStateError as error:
                _LOGGER.error("Mismatched state, CSRF error: %s", error)
                response_message = f"""Something went wrong when
                attempting authenticating with Fitbit. The error
                encountered was {error}. Please try again!"""
        else:
            _LOGGER.error("Unknown error when authing")
            response_message = """Something went wrong when
                attempting authenticating with Fitbit.
                An unknown error occurred. Please try again!
                """

        if result is None:
            _LOGGER.error("Unknown error when authing")
            response_message = """Something went wrong when
                attempting authenticating with Fitbit.
                An unknown error occurred. Please try again!
                """

        html_response = f"""<html><head><title>Fitbit Auth</title></head>
        <body><h1>{response_message}</h1></body></html>"""

        if result:
            config_contents = {
                ATTR_ACCESS_TOKEN: result.get("access_token"),
                ATTR_REFRESH_TOKEN: result.get("refresh_token"),
                CONF_CLIENT_ID: self.oauth.client_id,
                CONF_CLIENT_SECRET: self.oauth.client_secret,
                ATTR_LAST_SAVED_AT: int(time.time()),
            }
        save_json(opp.config.path(FITBIT_CONFIG_FILE), config_contents)

        opp.async_add_job(setup_platform, opp, self.config, self.add_entities)

        return html_response
コード例 #9
0
ファイル: light.py プロジェクト: OpenPeerPower/core
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)
コード例 #10
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"
コード例 #11
0
ファイル: notify.py プロジェクト: OpenPeerPower/core
    def _push_message(self, payload, **kwargs):
        """Send the message."""

        timestamp = int(time.time())
        ttl = int(kwargs.get(ATTR_TTL, DEFAULT_TTL))
        priority = kwargs.get(ATTR_PRIORITY, DEFAULT_PRIORITY)
        if priority not in ["normal", "high"]:
            priority = DEFAULT_PRIORITY
        payload["timestamp"] = timestamp * 1000  # Javascript ms since epoch
        targets = kwargs.get(ATTR_TARGET)

        if not targets:
            targets = self.registrations.keys()

        for target in list(targets):
            info = self.registrations.get(target)
            try:
                info = REGISTER_SCHEMA(info)
            except vol.Invalid:
                _LOGGER.error(
                    "%s is not a valid HTML5 push notification target", target
                )
                continue
            payload[ATTR_DATA][ATTR_JWT] = add_jwt(
                timestamp,
                target,
                payload[ATTR_TAG],
                info[ATTR_SUBSCRIPTION][ATTR_KEYS][ATTR_AUTH],
            )
            webpusher = WebPusher(info[ATTR_SUBSCRIPTION])
            if self._vapid_prv and self._vapid_email:
                vapid_headers = create_vapid_headers(
                    self._vapid_email, info[ATTR_SUBSCRIPTION], self._vapid_prv
                )
                vapid_headers.update({"urgency": priority, "priority": priority})
                response = webpusher.send(
                    data=json.dumps(payload), headers=vapid_headers, ttl=ttl
                )
            else:
                # Only pass the gcm key if we're actually using GCM
                # If we don't, notifications break on FireFox
                gcm_key = (
                    self._gcm_key
                    if "googleapis.com" in info[ATTR_SUBSCRIPTION][ATTR_ENDPOINT]
                    else None
                )
                response = webpusher.send(json.dumps(payload), gcm_key=gcm_key, ttl=ttl)

            if response.status_code == 410:
                _LOGGER.info("Notification channel has expired")
                reg = self.registrations.pop(target)
                if not save_json(self.registrations_json_path, self.registrations):
                    self.registrations[target] = reg
                    _LOGGER.error("Error saving registration")
                else:
                    _LOGGER.info("Configuration saved")
コード例 #12
0
    def wink_configuration_callback(callback_data):
        """Handle configuration updates."""
        _config_path = opp.config.path(WINK_CONFIG_FILE)
        if not os.path.isfile(_config_path):
            setup(opp, config)
            return

        client_id = callback_data.get(CONF_CLIENT_ID).strip()
        client_secret = callback_data.get(CONF_CLIENT_SECRET).strip()
        if None not in (client_id, client_secret):
            save_json(
                _config_path,
                {
                    CONF_CLIENT_ID: client_id,
                    CONF_CLIENT_SECRET: client_secret
                },
            )
            setup(opp, config)
            return
        error_msg = "Your input was invalid. Please try again."
        _configurator = opp.data[DOMAIN]["configuring"][DOMAIN]
        configurator.notify_errors(_configurator, error_msg)
コード例 #13
0
ファイル: media_player.py プロジェクト: OpenPeerPower/core
    def gpmdp_configuration_callback(callback_data):
        """Handle configuration changes."""
        while True:

            try:
                msg = json.loads(websocket.recv())
            except _exceptions.WebSocketConnectionClosedException:
                continue
            if msg["channel"] != "connect":
                continue
            if msg["payload"] != "CODE_REQUIRED":
                continue
            pin = callback_data.get("pin")
            websocket.send(
                json.dumps({
                    "namespace": "connect",
                    "method": "connect",
                    "arguments": ["Open Peer Power", pin],
                }))
            tmpmsg = json.loads(websocket.recv())
            if tmpmsg["channel"] == "time":
                _LOGGER.error("Error setting up GPMDP. Please pause "
                              "the desktop player and try again")
                break
            code = tmpmsg["payload"]
            if code == "CODE_REQUIRED":
                continue
            setup_gpmdp(opp, config, code, add_entities_callback)
            save_json(opp.config.path(GPMDP_CONFIG_FILE), {"CODE": code})
            websocket.send(
                json.dumps({
                    "namespace": "connect",
                    "method": "connect",
                    "arguments": ["Open Peer Power", code],
                }))
            websocket.close()
            break
コード例 #14
0
 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)
コード例 #15
0
ファイル: __init__.py プロジェクト: OpenPeerPower/core
 def save(self):
     """Save the items."""
     save_json(self.opp.config.path(PERSISTENCE), self.items)
コード例 #16
0
def _write_data(path: str, data: dict) -> None:
    """Write the data."""
    if not os.path.isdir(os.path.dirname(path)):
        os.makedirs(os.path.dirname(path))
    json_util.save_json(path, data)
コード例 #17
0
    def _store_auth_token(self, token):
        """Store authentication token to session and persistent storage."""
        self._auth_tokens[self._mx_id] = token

        save_json(self._session_filepath, self._auth_tokens)
コード例 #18
0
ファイル: sensor.py プロジェクト: OpenPeerPower/core
    def update(self) -> None:
        """Get the latest data from the Fitbit API and update the states."""
        if self.resource_type == "devices/battery" and self.extra is not None:
            registered_devs: list[dict[str, Any]] = self.client.get_devices()
            device_id = self.extra.get("id")
            self.extra = list(
                filter(lambda device: device.get("id") == device_id,
                       registered_devs))[0]
            self._state = self.extra.get("battery")

        else:
            container = self.resource_type.replace("/", "-")
            response = self.client.time_series(self.resource_type, period="7d")
            raw_state = response[container][-1].get("value")
            if self.resource_type == "activities/distance":
                self._state = format(float(raw_state), ".2f")
            elif self.resource_type == "activities/tracker/distance":
                self._state = format(float(raw_state), ".2f")
            elif self.resource_type == "body/bmi":
                self._state = format(float(raw_state), ".1f")
            elif self.resource_type == "body/fat":
                self._state = format(float(raw_state), ".1f")
            elif self.resource_type == "body/weight":
                self._state = format(float(raw_state), ".1f")
            elif self.resource_type == "sleep/startTime":
                if raw_state == "":
                    self._state = "-"
                elif self.clock_format == "12H":
                    hours, minutes = raw_state.split(":")
                    hours, minutes = int(hours), int(minutes)
                    setting = "AM"
                    if hours > 12:
                        setting = "PM"
                        hours -= 12
                    elif hours == 0:
                        hours = 12
                    self._state = f"{hours}:{minutes:02d} {setting}"
                else:
                    self._state = raw_state
            else:
                if self.is_metric:
                    self._state = raw_state
                else:
                    try:
                        self._state = f"{int(raw_state):,}"
                    except TypeError:
                        self._state = raw_state

        if self.resource_type == "activities/heart":
            self._state = response[container][-1].get("value").get(
                "restingHeartRate")

        token = self.client.client.session.token
        config_contents = {
            ATTR_ACCESS_TOKEN: token.get("access_token"),
            ATTR_REFRESH_TOKEN: token.get("refresh_token"),
            CONF_CLIENT_ID: self.client.client.client_id,
            CONF_CLIENT_SECRET: self.client.client.client_secret,
            ATTR_LAST_SAVED_AT: int(time.time()),
        }
        save_json(self.config_path, config_contents)
コード例 #19
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
コード例 #20
0
ファイル: sensor.py プロジェクト: OpenPeerPower/core
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)
コード例 #21
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