Skip to content
This repository has been archived by the owner on Apr 11, 2020. It is now read-only.

Commit

Permalink
Blinds update batching (#6)
Browse files Browse the repository at this point in the history
* Batch update blinds

* v0.5

* Get state batching
  • Loading branch information
KapJI authored and ianlevesque committed Apr 5, 2019
1 parent 70eb9a8 commit bc15c3e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def readme():

setup(
name='smartblinds_client',
version='0.4',
version='0.5',
description='Unofficial client for the MySmartBlinds Smart Bridge',
long_description=readme(),
url='http://github.com/ianlevesque/smartblinds-client',
Expand Down
72 changes: 43 additions & 29 deletions smartblinds_client/smartblinds.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
import base64


def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]


class Blind:
def __init__(self, name: str, encoded_mac: str, room_id: str, encoded_passkey: str):
self.name = name
Expand Down Expand Up @@ -68,6 +74,8 @@ class SmartBlindsClient:

GRAPHQL_ENDPOINT = "https://api.mysmartblinds.com/v1/graphql"

BATCH_SIZE = 7

def __init__(self, username: str, password: str):
self._username = username
self._password = password
Expand Down Expand Up @@ -130,41 +138,47 @@ def get_blinds_and_rooms(self) -> typing.Tuple[typing.List[Blind], typing.List[R
return blinds, list(rooms.values())

def get_blinds_state(self, blinds: [Blind]) -> typing.Mapping[str, BlindState]:
response = self._graphql(
query='''
query GetBlindsState($blinds: [String]) {
blindsState(encodedMacAddresses: $blinds) {
encodedMacAddress
position
rssi
batteryLevel
blind_states = {}
for blinds_batch in chunks(blinds, self.BATCH_SIZE):
response = self._graphql(
query='''
query GetBlindsState($blinds: [String]) {
blindsState(encodedMacAddresses: $blinds) {
encodedMacAddress
position
rssi
batteryLevel
}
}
}
''',
variables={
'blinds': list(map(lambda b: b.encoded_mac, blinds)),
})
''',
variables={
'blinds': list(map(lambda b: b.encoded_mac, blinds)),
})
blind_states.update(self._parse_states(response))

return self._parse_states(response)
return blind_states

def set_blinds_position(self, blinds: [Blind], position: int) -> typing.Mapping[str, BlindState]:
response = self._graphql(
query='''
mutation UpdateBlindsPosition($blinds: [String], $position: Int!) {
updateBlindsPosition(encodedMacAddresses: $blinds, position: $position) {
encodedMacAddress
position
rssi
batteryLevel
blind_states = {}
for blinds_batch in chunks(blinds, self.BATCH_SIZE):
response = self._graphql(
query='''
mutation UpdateBlindsPosition($blinds: [String], $position: Int!) {
updateBlindsPosition(encodedMacAddresses: $blinds, position: $position) {
encodedMacAddress
position
rssi
batteryLevel
}
}
}
''',
variables={
'position': position,
'blinds': list(map(lambda b: b.encoded_mac, blinds)),
})
''',
variables={
'position': position,
'blinds': list(map(lambda b: b.encoded_mac, blinds_batch)),
})
blind_states.update(self._parse_states(response, 'updateBlindsPosition'))

return self._parse_states(response, 'updateBlindsPosition')
return blind_states

@staticmethod
def _parse_states(response, key='blindsState') -> typing.Mapping[str, BlindState]:
Expand Down

0 comments on commit bc15c3e

Please sign in to comment.