Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A basic example please #2

Open
franc-carter opened this issue Jun 25, 2017 · 4 comments
Open

A basic example please #2

franc-carter opened this issue Jun 25, 2017 · 4 comments

Comments

@franc-carter
Copy link

Could you add a simple example of pulling the current readings from the API

thanks

@mezz64
Copy link
Owner

mezz64 commented Jun 25, 2017

The HASS implementation is a good reference. Otherwise I can probably post up a sanitized version of my basic testing code for you in a week or so.

@franc-carter
Copy link
Author

I grabbed the HASS code and had a look, but my Python isn't good enough for me to get get a clear understanding ;-(

Sanitized test code would be great

thanks

@mezz64
Copy link
Owner

mezz64 commented Jul 7, 2017

This is my test workflow that should give you a simpler look at how to use it:

"""
Library Test File

"""

import logging
import asyncio
from pyeight.eight import EightSleep

logging.basicConfig(
    filename='api_out.log',
    filemode='a',
    level=logging.DEBUG,
    format='%(asctime)s - %(levelname)s - %(message)s',
    datefmt='%m/%d/%Y %I:%M:%S %p')

_LOG = logging.getLogger(__name__)


@asyncio.coroutine
def update_device(loop, device):
    while True:
        yield from device.update_device_data()
        device_info(device)

        yield from asyncio.sleep(60, loop)


@asyncio.coroutine
def update_user(loop, device):
    while True:
        yield from device.update_user_data()
        user_info(device)

        yield from asyncio.sleep(60*5, loop)


def user_info(device):
    for user in device.users:
        obj = device.users[user]

        print('{} User Current Sleep Score: {}'.format(obj.side, obj.current_sleep_score))
        #print('{} User Current Sleep Breakdown: {}'.format(obj.side, obj.current_sleep_breakdown))
        #print('{} User Current Sleep Stage: {}'.format(obj.side, obj.current_sleep_stage))
        #print('{} User Current Toss&Turns: {}'.format(obj.side, obj.current_tnt))
        #print('{} User Current Bed Temp: {}'.format(obj.side, obj.current_bed_temp))
        #print('{} User Current Room Temp: {}'.format(obj.side, obj.current_room_temp))
        #print('{} User Current Resp Rate: {}'.format(obj.side, obj.current_resp_rate))
        #print('{} User Current Heart Rate: {}'.format(obj.side, obj.current_heart_rate))

        # print('{} User Last Session Date: {}'.format(obj.side, obj.last_session_date))
        # print('{} User Last Sleep Score: {}'.format(obj.side, obj.last_sleep_score))
        # print('{} User Last Sleep Breakdown: {}'.format(obj.side, obj.last_sleep_breakdown))

        _LOG.debug('%s User Current: %s', obj.side, obj.current_values)
        _LOG.debug('%s User Last: %s', obj.side, obj.last_values)


def device_info(device):
    for user in device.users:
        obj = device.users[user]
        print('{} User Heat Level: {}'.format(obj.side, obj.heating_level))
        # print('{} User Heat Remaining: {}'.format(obj.side, obj.heating_remaining/3600))

        _LOG.debug('%s User Heating: %s', obj.side, obj.heating_values)
        _LOG.debug('%s User In Bed: %s', obj.side, obj.presence)

        heat_hist = []
        for i in range(0, 5):
            heat_hist.append(obj.past_heating_level(i))
        _LOG.debug('%s User Heating History: %s', obj.side, heat_hist)


@asyncio.coroutine
def eight_test(loop):
    """Eight test."""
    _LOG.debug('Starting pyEight test.')

    usr = 'username@email.com'
    passw = 'password'

    eight = EightSleep(usr, passw, 'America/New_York', True, None, loop)

    yield from eight.start()

    print(eight.token)

    while True:
        yield from asyncio.wait((
            update_device(loop, eight),
            update_user(loop, eight)
            ))

    yield from eight._api_session.close()


def main():
    """ Do main things.. """

    # This will server to mimic the HASS loop that we will pass in.
    hassloop = asyncio.get_event_loop()

    try:
        hassloop.run_until_complete(eight_test(hassloop))
    except KeyboardInterrupt:
        # Optionally show a message if the shutdown may take a while
        print("Attempting graceful shutdown…", flush=True)

        # Do not show `asyncio.CancelledError` exceptions during shutdown
        # (a lot of these may be generated, skip this if you prefer to see them)
        def shutdown_exception_handler(loop, context):
            if "exception" not in context \
                    or not isinstance(context["exception"], asyncio.CancelledError):
                hassloop.default_exception_handler(context)
        hassloop.set_exception_handler(shutdown_exception_handler)

        # Handle shutdown gracefully by waiting for all tasks to be cancelled
        tasks = asyncio.gather(*asyncio.Task.all_tasks(loop=hassloop), loop=hassloop, return_exceptions=True)
        tasks.add_done_callback(lambda t: hassloop.stop())
        tasks.cancel()

        # Keep the event loop running until it is either destroyed or all
        # tasks have really terminated
        while not tasks.done() and not hassloop.is_closed():
            hassloop.run_forever()

    finally:
        hassloop.close()


if __name__ == '__main__':
    main()

@franc-carter
Copy link
Author

thanks

much appreciated

cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants