#!/usr/bin/env python3
#
# BTZen - Bluetooth Smart sensor reading library.
#
# Copyright (C) 2015-2018 by Artur Wroblewski <wrobell@riseup.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

"""
Display weight data from Xiaomi Mi Smart Scale.
"""

import argparse
import asyncio
import logging

import btzen

async def read_weight(reader):
    await reader.connect()
    while True:
        t1 = loop.time()
        value = await reader.read()
        t2 = loop.time()
        print('weight: {} ({:.4f}s)'.format(value, t2 - t1))

logging.basicConfig(level=logging.DEBUG)

parser = argparse.ArgumentParser()
parser.add_argument('device', help='MAC address of device')
args = parser.parse_args()

sensor = btzen.Weight(args.device, notifying=True)
task = read_weight(sensor)

loop = asyncio.get_event_loop()
try:
    loop.run_until_complete(task)
finally:
    sensor.close()

# vim: sw=4:et:ai
