pysmarlaapi 0.1.0__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/PKG-INFO +1 -1
  2. pysmarlaapi-0.2.0/pysmarlaapi/__init__.py +4 -0
  3. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/pysmarlaapi/classes/connection.py +6 -1
  4. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/pysmarlaapi/connection_hub/__init__.py +3 -4
  5. pysmarlaapi-0.2.0/pysmarlaapi/federwiege/__init__.py +62 -0
  6. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/pysmarlaapi/federwiege/classes/service.py +2 -5
  7. pysmarlaapi-0.2.0/pysmarlaapi/federwiege/services/__init__.py +2 -0
  8. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/pysmarlaapi/federwiege/services/analyser_service.py +0 -1
  9. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/pysmarlaapi/federwiege/services/babywiege_service.py +0 -1
  10. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/tests/test.py +10 -12
  11. pysmarlaapi-0.1.0/pysmarlaapi/__init__.py +0 -4
  12. pysmarlaapi-0.1.0/pysmarlaapi/federwiege/__init__.py +0 -2
  13. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/.gitignore +0 -0
  14. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/.pre-commit-config.yaml +0 -0
  15. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/.pypirc +0 -0
  16. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/.vscode/launch.json +0 -0
  17. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/.vscode/settings.json +0 -0
  18. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/README.md +0 -0
  19. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/pyproject.toml +0 -0
  20. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/pysmarlaapi/classes/__init__.py +0 -0
  21. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/pysmarlaapi/classes/auth_token.py +0 -0
  22. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/pysmarlaapi/federwiege/classes/__init__.py +0 -0
  23. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/pysmarlaapi/federwiege/classes/property.py +0 -0
  24. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/requirements.txt +0 -0
  25. {pysmarlaapi-0.1.0 → pysmarlaapi-0.2.0}/requirements_dev.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: pysmarlaapi
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: Swing2Sleep Smarla API via websocket with signalr protocol
5
5
  Author-email: Robin Lintermann <robin.lintermann@explicatis.com>
6
6
  Requires-Python: >=3.11.9
@@ -0,0 +1,4 @@
1
+ __version__ = "0.2.0"
2
+
3
+ from .classes import Connection
4
+ from .federwiege import Federwiege
@@ -1,3 +1,5 @@
1
+ import asyncio
2
+
1
3
  import aiohttp
2
4
  import jsonpickle
3
5
 
@@ -6,7 +8,10 @@ from . import AuthToken
6
8
 
7
9
  class Connection:
8
10
 
9
- def __init__(self, url: str, token: AuthToken = None, token_str=None, token_json=None):
11
+ def __init__(
12
+ self, event_loop: asyncio.AbstractEventLoop, url: str, token: AuthToken = None, token_str=None, token_json=None
13
+ ):
14
+ self.event_loop = event_loop
10
15
  self.url = url
11
16
  if token is not None:
12
17
  self.token = token
@@ -31,13 +31,12 @@ class ConnectionHub:
31
31
 
32
32
  def __init__(
33
33
  self,
34
- async_loop: asyncio.AbstractEventLoop,
35
34
  connection: Connection,
36
35
  interval: int = 60,
37
36
  backoff: int = 300,
38
37
  ):
39
38
  self.connection: Connection = connection
40
- self._loop: asyncio.AbstractEventLoop = async_loop
39
+ self._loop: asyncio.AbstractEventLoop = self.connection.event_loop
41
40
  self._interval = interval
42
41
  self._backoff = backoff
43
42
 
@@ -54,9 +53,9 @@ class ConnectionHub:
54
53
  async def notifycontrollerconnection(self, args):
55
54
  value = args[0]
56
55
  if value == "ControllerConnected":
57
- await self.notify_listeners(1)
56
+ await self.notify_listeners(True)
58
57
  else:
59
- await self.notify_listeners(0)
58
+ await self.notify_listeners(False)
60
59
 
61
60
  def setup(self):
62
61
  self.client = SignalRClient(self.connection.url + "/MobileAppHub", retry_count=1)
@@ -0,0 +1,62 @@
1
+ import threading
2
+
3
+ from ..classes import Connection
4
+ from ..connection_hub import ConnectionHub
5
+ from .classes import Service
6
+ from .services import AnalyserService, BabywiegeService
7
+
8
+
9
+ class Federwiege:
10
+
11
+ @property
12
+ def running(self):
13
+ return self.hub.running
14
+
15
+ @property
16
+ def connected(self):
17
+ return self.hub.connected
18
+
19
+ async def on_controller_connection_change(self, value):
20
+ self.available = value
21
+ if value:
22
+ self.sync()
23
+
24
+ def __init__(self, connection: Connection):
25
+ self.serial_number = connection.token.serialNumber
26
+ self.hub = ConnectionHub(connection)
27
+ self.services: dict[str, Service] = {
28
+ "babywiege": BabywiegeService(self.hub),
29
+ "analyser": AnalyserService(self.hub),
30
+ }
31
+
32
+ self.registered = False
33
+ self._lock = threading.Lock()
34
+
35
+ self.available = False
36
+
37
+ def get_service(self, service):
38
+ if service not in self.services:
39
+ return None
40
+ return self.services[service]
41
+
42
+ def connect(self):
43
+ with self._lock:
44
+ if not self.registered:
45
+ return
46
+ self.hub.start()
47
+
48
+ def disconnect(self):
49
+ self.hub.stop()
50
+
51
+ def sync(self):
52
+ for service in self.services.values():
53
+ service.sync()
54
+
55
+ def register(self):
56
+ with self._lock:
57
+ if self.registered:
58
+ return
59
+ self.registered = True
60
+ self.hub.add_listener(self.on_controller_connection_change)
61
+ for service in self.services.values():
62
+ service.register()
@@ -4,10 +4,6 @@ from .property import Property
4
4
 
5
5
  class Service:
6
6
 
7
- async def on_connection_change(self, value):
8
- if value:
9
- self.sync()
10
-
11
7
  def __init__(self, connection_hub: ConnectionHub):
12
8
  self.hub = connection_hub
13
9
  self.registered = False
@@ -20,12 +16,13 @@ class Service:
20
16
  return self.props
21
17
 
22
18
  def get_property(self, key: str):
19
+ if key not in self.props:
20
+ return None
23
21
  return self.props[key]
24
22
 
25
23
  def register(self):
26
24
  for prop in self.props.values():
27
25
  prop.register()
28
- self.hub.add_listener(self.on_connection_change)
29
26
  self.registered = True
30
27
 
31
28
  def sync(self):
@@ -0,0 +1,2 @@
1
+ from .analyser_service import AnalyserService
2
+ from .babywiege_service import BabywiegeService
@@ -8,7 +8,6 @@ class AnalyserService(Service):
8
8
  super().__init__(connection_hub)
9
9
  self.add_property("oscillation", OscillationProperty(self.hub))
10
10
  self.add_property("activity", ActivityProperty(self.hub))
11
- self.register()
12
11
 
13
12
 
14
13
  class OscillationProperty(Property[list[int, int]]):
@@ -9,7 +9,6 @@ class BabywiegeService(Service):
9
9
  self.add_property("swing_active", SwingActiveProperty(self.hub))
10
10
  self.add_property("intensity", IntensityProperty(self.hub))
11
11
  self.add_property("smartmode", SmartModeProperty(self.hub))
12
- self.register()
13
12
 
14
13
 
15
14
  class SwingActiveProperty(Property[bool]):
@@ -7,8 +7,7 @@ import time
7
7
  sys.path.append("../")
8
8
  logging.basicConfig(level=20)
9
9
 
10
- from pysmarlaapi import Connection, ConnectionHub
11
- from pysmarlaapi.federwiege import AnalyserService, BabywiegeService
10
+ from pysmarlaapi import Connection, Federwiege
12
11
 
13
12
  try:
14
13
  from config import AUTH_TOKEN_PERSONAL, HOST
@@ -19,23 +18,22 @@ except ImportError:
19
18
  loop = asyncio.get_event_loop()
20
19
  async_thread = threading.Thread(target=loop.run_forever)
21
20
 
22
- connection = Connection(url=HOST, token_json=AUTH_TOKEN_PERSONAL)
21
+ connection = Connection(loop, url=HOST, token_json=AUTH_TOKEN_PERSONAL)
23
22
 
24
- hub = ConnectionHub(loop, connection, interval=10, backoff=0)
25
- babywiege_svc = BabywiegeService(hub)
26
- analyser_svc = AnalyserService(hub)
23
+ federwiege = Federwiege(connection)
27
24
 
28
25
 
29
26
  def main():
30
27
  async_thread.start()
31
- hub.start()
28
+ federwiege.register()
29
+ federwiege.connect()
32
30
 
33
- while not hub.connected:
31
+ while not (federwiege.connected and federwiege.available):
34
32
  time.sleep(1)
35
33
 
36
- swing_active_prop = babywiege_svc.get_property("swing_active")
37
- intensity_prop = babywiege_svc.get_property("intensity")
38
- oscillation_prop = analyser_svc.get_property("oscillation")
34
+ swing_active_prop = federwiege.get_service("babywiege").get_property("swing_active")
35
+ intensity_prop = federwiege.get_service("babywiege").get_property("intensity")
36
+ oscillation_prop = federwiege.get_service("analyser").get_property("oscillation")
39
37
 
40
38
  time.sleep(1)
41
39
 
@@ -75,5 +73,5 @@ if __name__ == "__main__":
75
73
  main()
76
74
  except BaseException:
77
75
  pass
78
- hub.stop()
76
+ federwiege.disconnect()
79
77
  loop.call_soon_threadsafe(loop.stop)
@@ -1,4 +0,0 @@
1
- __version__ = "0.1.0"
2
-
3
- from .classes import AuthToken, Connection
4
- from .connection_hub import ConnectionHub
@@ -1,2 +0,0 @@
1
- from .services.analyser_service import AnalyserService
2
- from .services.babywiege_service import BabywiegeService
File without changes
File without changes
File without changes
File without changes