def init():
    # Here we do the setup
    # Store access_token in OS X keychain on first run
    print(
        'Enter your local smappee hostname (by default this is \"smappee.local\"):'
    )
    init_hostname = raw_input()
    print('Enter your local smappee password (by default this is \"admin\")')
    init_password = getpass.getpass()

    try:
        c = smappy.LocalSmappee(init_hostname)
        c.logon(init_password)
    except HTTPError as e:
        print('Error contacting local Smappee appliance. Try again later.')
        print e
        time.sleep(0.5)
        return
    except URLError as e:
        print('Error: Unable to connect. Check your connection settings.')
        print e
        return
    except Exception as e:
        print('Error: Something went wrong:')
        print e
        return
    keyring.set_password("mylocalsmappee-bitbar", "hostname", init_hostname)
    keyring.set_password("mylocalsmappee-bitbar", "password", init_password)
Beispiel #2
0
    def __init__(self, client_id, client_secret, username,
                 password, host, host_password):
        """Initialize the data."""
        import smappy

        self._remote_active = False
        self._local_active = False
        if client_id is not None:
            try:
                self._smappy = smappy.Smappee(client_id, client_secret)
                self._smappy.authenticate(username, password)
                self._remote_active = True
            except RequestException as error:
                self._smappy = None
                _LOGGER.exception(
                    "Smappee server authentication failed (%s)",
                    error)
        else:
            _LOGGER.warning("Smappee server component init skipped.")

        if host is not None:
            try:
                self._localsmappy = smappy.LocalSmappee(host)
                self._localsmappy.logon(host_password)
                self._local_active = True
            except RequestException as error:
                self._localsmappy = None
                _LOGGER.exception(
                    "Local Smappee device authentication failed (%s)",
                    error)
        else:
            _LOGGER.warning("Smappee local component init skipped.")

        self.locations = {}
        self.info = {}
        self.consumption = {}
        self.sensor_consumption = {}
        self.instantaneous = {}

        if self._remote_active or self._local_active:
            self.update()
Beispiel #3
0
    def __init__(self, client_id, client_secret, username, password, host, host_password):
        """Initialize the data."""
        import smappy

        try:
            self._s = smappy.Smappee(client_id, client_secret)
            self._s.authenticate(username, password)
        except Exception as e:
            self._s = None
            _LOGGER.error('Smappee authentication failed, %s', e)

        try:
            self._l = smappy.LocalSmappee(host)
            self._l.logon(host_password)
        except Exception as e:
            self._l = None
            _LOGGER.error('Local Smappee authentication failed, %s', e)

        self.locations = {}
        self.info = {}

        self.update()
def main(argv):

    # CASE 1: init was called
    if 'init' in argv:
        init()
        return

    # CASE 2: init was not called, keyring not initialized
    if DARK_MODE:
        color = '#FFDEDEDE'
        info_color = '#808080'
    else:
        color = 'black'
        info_color = '#808080'

    HOSTNAME = keyring.get_password("mylocalsmappee-bitbar", "hostname")
    PASSWORD = keyring.get_password("mylocalsmappee-bitbar", "password")

    if not HOSTNAME:
        # restart in terminal calling init
        app_print_logo()
        print(
            'Login to your local Smappee | refresh=true terminal=true bash="\'%s\'" param1="%s" color=%s'
            % (sys.argv[0], 'init', color))
        return

    # CASE 3: init was not called, keyring initialized, no connection (access code not valid)
    try:
        # create connection to smappee using token
        c = smappy.LocalSmappee(HOSTNAME)
        c.logon(PASSWORD)
    except:
        app_print_logo()
        print(
            'Login to your local Smappee | refresh=true terminal=true bash="\'%s\'" param1="%s" color=%s'
            % (sys.argv[0], 'init', color))
        return

    # CASE 4: all ok, all other cases
    if STREAMING:
        while True:

            # get the data for the location
            load = c.active_power()

            # print the data for the Smappee appliance
            print('%s Watt | color=%s' % (load, color))
            print('---')
            print('Expert Mode | href=http://%s/smappee.html color=%s' %
                  (HOSTNAME, color))
            print('~~~')
            time.sleep(0.5)
    else:
        app_print_logo()
        prefix = ''

        # get the data for the location
        load = c.active_power()

        # print the data for the Smappee appliance
        print('%sCurrent Load:  %s Watt | color=%s' % (prefix, load, color))
        print('%s---' % prefix)
        print('%sExpert Mode | href=http://%s/smappee.html color=%s' %
              (prefix, HOSTNAME, color))