Skip to content

Commit

Permalink
Merge pull request #2 from rado0x54/develop
Browse files Browse the repository at this point in the history
Release v0.1.1
  • Loading branch information
rado0x54 authored Feb 17, 2021
2 parents 5289e85 + d401472 commit 9ff344a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 24 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# pysnoo
[![PyPI version](https://badge.fury.io/py/pysnoo.svg)](https://pypi.python.org/pypi/pysnoo)
[![Coverage Status](https://coveralls.io/repos/github/rado0x54/pysnoo/badge.svg?branch=develop)](https://coveralls.io/github/rado0x54/pysnoo?branch=develop)
![Python package](https://github.com/rado0x54/pysnoo/workflows/Python%20package/badge.svg)

[![PyPI license](https://img.shields.io/pypi/l/pysnoo)](https://pypi.python.org/pypi/pysnoo)
[![PyPI versions](https://img.shields.io/pypi/pyversions/pysnoo)](https://pypi.python.org/pypi/pysnoo)
# pysnoo
pysnoo is a python library to interact with the SNOO Smart Sleeper Bassinet

## Disclaimer
Expand Down
38 changes: 28 additions & 10 deletions pysnoo/pubnub.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""PySnoo PubNub Interface."""
import asyncio
import logging
from typing import Callable, Optional
from typing import Callable, Optional, List

from pubnub.callbacks import SubscribeCallback
from pubnub.pnconfiguration import PNConfiguration
Expand Down Expand Up @@ -53,18 +53,17 @@ class SnooPubNub:

def __init__(self,
access_token: str,
snoo_serial: str,
serial_number: str,
uuid: str,
callback: Callable[[ActivityState], None],
custom_event_loop=None):
"""Initialize the Snoo PubNub object."""
# self._access_token = access_token
# self._snoo_serial = snoo_serial
self._activiy_channel = 'ActivityState.{}'.format(snoo_serial)
self._controlcommand_channel = 'ControlCommand.{}'.format(snoo_serial)
self._pnconfig = self._setup_pnconfig(access_token, uuid)
self._pubnub = PubNubAsyncio(self._pnconfig, custom_event_loop=custom_event_loop)
self._listener = SnooSubscribeListener(callback)
self.config = self._setup_pnconfig(access_token, uuid)
self.serial_number = serial_number
self._activiy_channel = 'ActivityState.{}'.format(serial_number)
self._controlcommand_channel = 'ControlCommand.{}'.format(serial_number)
self._pubnub = PubNubAsyncio(self.config, custom_event_loop=custom_event_loop)
self._listener = SnooSubscribeListener(self._activy_state_callback)
self._external_listeners: List[Callable[[ActivityState], None]] = []

@staticmethod
def _setup_pnconfig(access_token, uuid):
Expand All @@ -77,6 +76,25 @@ def _setup_pnconfig(access_token, uuid):
pnconfig.ssl = True
return pnconfig

def add_listener(self, update_callback: Callable[[ActivityState], None]) -> Callable[[], None]:
"""Add a AcitivyState Listener to the SnooPubNub Entity and returns a remove_listener CB for that listener"""
self._external_listeners.append(update_callback)

def remove_listener_cb() -> None:
"""Remove listener."""
self.remove_listener(update_callback)

return remove_listener_cb

def remove_listener(self, update_callback: Callable[[ActivityState], None]) -> None:
"""Remove data update."""
self._external_listeners.remove(update_callback)

def _activy_state_callback(self, state: ActivityState):
"""Internal Callback of SnooSubscribeListener"""
for update_callback in self._external_listeners:
update_callback(state)

async def subscribe(self):
"""Subscribe to Snoo Activity Channel"""
self._pubnub.add_listener(self._listener)
Expand Down
18 changes: 9 additions & 9 deletions scripts/snoo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ from pysnoo.models import dt_str_to_dt
# pylint: disable=unused-argument


async def _setup_pubnub(snoo: Snoo, callback):
async def _setup_pubnub(snoo: Snoo):
"""Utility Function to setup SnooPubNub"""
# Also checks for valid token
devices = await snoo.get_devices()
Expand All @@ -24,8 +24,7 @@ async def _setup_pubnub(snoo: Snoo, callback):

return SnooPubNub(snoo.auth.access_token,
devices[0].serial_number,
f'pn-pysnoo-{devices[0].serial_number}',
callback)
f'pn-pysnoo-{devices[0].serial_number}')


async def user(snoo: Snoo, args):
Expand Down Expand Up @@ -84,7 +83,8 @@ async def monitor(snoo: Snoo, args):
def as_callback(activity_state):
pprint(activity_state.to_dict())

pubnub = await _setup_pubnub(snoo, as_callback)
pubnub = await _setup_pubnub(snoo)
pubnub.add_listener(as_callback)

for activity_state in await pubnub.history():
as_callback(activity_state)
Expand All @@ -103,7 +103,7 @@ async def monitor(snoo: Snoo, args):

async def history(snoo: Snoo, args):
"""history command"""
pubnub = await _setup_pubnub(snoo, None)
pubnub = await _setup_pubnub(snoo)

for activity_state in await pubnub.history(100):
pprint(activity_state.to_dict())
Expand All @@ -113,7 +113,7 @@ async def history(snoo: Snoo, args):

async def toggle(snoo: Snoo, args):
"""toggle command"""
pubnub = await _setup_pubnub(snoo, None)
pubnub = await _setup_pubnub(snoo)

last_activity_state = (await pubnub.history())[0]
if last_activity_state.state_machine.state == SessionLevel.ONLINE:
Expand All @@ -128,7 +128,7 @@ async def toggle(snoo: Snoo, args):

async def toggle_hold(snoo: Snoo, args):
"""toggleHold command"""
pubnub = await _setup_pubnub(snoo, None)
pubnub = await _setup_pubnub(snoo)

last_activity_state = (await pubnub.history())[0]
current_state = last_activity_state.state_machine.state
Expand All @@ -144,7 +144,7 @@ async def toggle_hold(snoo: Snoo, args):

async def level_up(snoo: Snoo, args):
"""up command"""
pubnub = await _setup_pubnub(snoo, None)
pubnub = await _setup_pubnub(snoo)

last_activity_state = (await pubnub.history())[0]
up_transition = last_activity_state.state_machine.up_transition
Expand All @@ -159,7 +159,7 @@ async def level_up(snoo: Snoo, args):

async def level_down(snoo: Snoo, args):
"""down command"""
pubnub = await _setup_pubnub(snoo, None)
pubnub = await _setup_pubnub(snoo)

last_activity_state = (await pubnub.history())[0]
down_transition = last_activity_state.state_machine.down_transition
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""PySnoo setup script."""
from setuptools import setup

_VERSION = '0.1.0'
_VERSION = '0.1.1'


def readme():
Expand Down
12 changes: 10 additions & 2 deletions tests/test_snoo_pubnub.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def setUp(self):
self.pubnub = SnooPubNub('ACCESS_TOKEN',
'SERIAL_NUMBER',
'UUID',
MagicMock(),
custom_event_loop=self.loop)

async def tearDown(self):
Expand Down Expand Up @@ -163,9 +162,18 @@ async def test_message_callback(self):
load_fixture('', 'pubnub_message_ActivityState.json'))
activity_state = ActivityState.from_dict(activity_state_msg_payload)

callback = self.pubnub._listener._callback
# Add callback
callback = MagicMock()
remove_cb = self.pubnub.add_listener(callback)

self.assertEqual(self.pubnub._external_listeners, [callback])

# Trigger callback
self.pubnub._listener.message(self.pubnub._pubnub, PNMessageResult(
activity_state_msg_payload, None, None, 0))

callback.assert_called_once_with(activity_state)

# Remove callback
remove_cb()
self.assertEqual(self.pubnub._external_listeners, [])

0 comments on commit 9ff344a

Please sign in to comment.