-
Notifications
You must be signed in to change notification settings - Fork 9
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
[WIP] Feature/typings #65
Open
cojmeister
wants to merge
32
commits into
sinricpro:master
Choose a base branch
from
cojmeister:feature/typings
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
2b311ee
Blinds example
cojmeister 4208a69
Added typings to _power_controller.py
cojmeister e9a0ede
Added typings to _range_value_controller.py
cojmeister 407b783
Commenting
cojmeister 9d87223
Typings for camera
cojmeister fd10036
Typing for _mode_controller.py
cojmeister 89e1edb
Typing for custom_device.py
cojmeister 695c28e
Calling all CallbackFunctions
cojmeister 88437de
Typings!
cojmeister 185d742
Typings and dict.get
cojmeister be872d5
Made consts
cojmeister 4374fe9
Typings on doorbell.py
cojmeister 8a1125b
Typings on garagedoor.py
cojmeister 87f3203
Typings for light and all relevant controllers
cojmeister a305bf5
Typings for smart lock
cojmeister 22be9e5
Added types files
cojmeister f4d0caa
Began Speaker
cojmeister a3671f2
Typings for SinricPro
cojmeister 7ea8989
Added typings
cojmeister 25d7972
Began adding typings to event and hanlder
cojmeister 68d547b
Merge branch 'master' into feature/typings
cojmeister 0c020b8
Typings for switch and multiswitch
cojmeister 640fe20
Typings for push notifications
cojmeister 03ce8c5
Typings for temperature sensor
cojmeister b8880cb
Typings for speaker
cojmeister f0c7e4a
Typings for leaky bucket
cojmeister ea99eb2
Typings for signature
cojmeister 14c59a3
Bugfix
cojmeister f2d2de8
Cleanup
cojmeister 7b1898c
Added connection
cojmeister 4c8ef4b
Began typing in callback handler
cojmeister fee33c3
Removed .vscode
cojmeister File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,66 @@ | ||
from collections.abc import Callable | ||
from typing import ByteString, Final, Optional, Union | ||
from sinric import SinricPro, SinricProConstants | ||
import requests | ||
import asyncio | ||
import base64 | ||
|
||
APP_KEY = "" | ||
APP_SECRET = "" | ||
CAMERA_ID = '' | ||
APP_KEY: Final[str] = '' | ||
APP_SECRET: Final[str] = '' | ||
CAMERA_ID: Final[str] = '' | ||
|
||
def get_webrtc_answer(device_id, offer): | ||
sdp_offer = base64.b64decode(offer) | ||
OfferType = Union[str, ByteString] | ||
|
||
CallbackFunctions = Union[ | ||
Callable[[str, str], tuple[bool, str]], # Power state / Camera Stream Url | ||
Callable[[str, OfferType], tuple[bool, Optional[bytes]]], # WEBRTC Answer | ||
|
||
] | ||
|
||
|
||
def get_webrtc_answer(device_id: str, offer: OfferType) -> tuple[bool, Optional[bytes]]: | ||
sdp_offer: Final[bytes] = base64.b64decode(offer) | ||
print('device_id: {} offer: {}'.format(device_id, offer)) | ||
|
||
# PORT 8889 for WebRTC. eg: for PiCam, use http://<mediamtx-hostname>:8889/cam/whep | ||
mediamtx_url = "http://<mediamtx-hostname>:8889/<device>/whep" | ||
headers = {"Content-Type": "application/sdp"} | ||
response = requests.post(mediamtx_url, headers=headers, data=sdp_offer) | ||
mediamtx_url: Final[str] = "http://<mediamtx-hostname>:8889/<device>/whep" | ||
headers: dict[str, str] = {"Content-Type": "application/sdp"} | ||
response: requests.Response = requests.post( | ||
mediamtx_url, headers=headers, data=sdp_offer) | ||
|
||
if response.status_code == 201: | ||
answer = base64.b64encode(response.content).decode("utf-8") | ||
answer: bytes = base64.b64encode(response.content).decode("utf-8") | ||
return True, answer | ||
else: | ||
return False | ||
return False, None | ||
|
||
|
||
def power_state(device_id, state): | ||
def power_state(device_id: str, state: str) -> tuple[bool, str]: | ||
print('device_id: {} power state: {}'.format(device_id, state)) | ||
return True, state | ||
|
||
def get_camera_stream_url(device_id, protocol): | ||
|
||
def get_camera_stream_url(device_id: str, protocol: str) -> tuple[bool, str]: | ||
# TODO: Should protocol be a string literal? | ||
# Google Home: RTSP protocol not supported. Requires a Chromecast TV or Google Nest Hub | ||
# Alexa: RTSP url must be interleaved TCP on port 443 (for both RTP and RTSP) over TLS 1.2 port 443 | ||
|
||
print('device_id: {} protocol: {}'.format(device_id, protocol)) | ||
|
||
if protocol == "rstp": | ||
return True, 'rtsp://rtspurl:443' # RSTP. | ||
return True, 'rtsp://rtspurl:443' # RSTP. | ||
else: | ||
return True, 'https://cph-p2p-msl.akamaized.net/hls/live/2000341/test/master.m3u8' # HLS | ||
return True, 'https://cph-p2p-msl.akamaized.net/hls/live/2000341/test/master.m3u8' # HLS | ||
|
||
|
||
callbacks = { | ||
callbacks: dict[str, CallbackFunctions] = { | ||
SinricProConstants.GET_WEBRTC_ANSWER: get_webrtc_answer, | ||
SinricProConstants.GET_CAMERA_STREAM_URL: get_camera_stream_url, | ||
SinricProConstants.SET_POWER_STATE: power_state | ||
} | ||
|
||
if __name__ == '__main__': | ||
loop = asyncio.get_event_loop() | ||
client = SinricPro(APP_KEY, [CAMERA_ID], callbacks, | ||
enable_log=False, restore_states=False, secret_key=APP_SECRET) | ||
loop: asyncio.AbstractEventLoop = asyncio.get_event_loop() | ||
client: SinricPro = SinricPro(APP_KEY, [CAMERA_ID], callbacks, | ||
enable_log=False, restore_states=False, secret_key=APP_SECRET) | ||
loop.run_until_complete(client.connect()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,44 @@ | ||
from collections.abc import Callable | ||
from numbers import Real | ||
from typing import Any, Final, Union | ||
from sinric import SinricPro, SinricProConstants | ||
import asyncio | ||
|
||
APP_KEY = '' | ||
APP_SECRET = '' | ||
DEVICE_ID = '' | ||
APP_KEY: Final[str] = '' | ||
APP_SECRET: Final[str] = '' | ||
DEVICE_ID: Final[str] = '' | ||
|
||
CallbackFunctions = Union[ | ||
Callable[[str, str], tuple[bool, str]], # Power state | ||
Callable[[str, Real, str], tuple[bool, Real, str]], # Range Value | ||
Callable[[str, Any, str], tuple[bool, Any, str]] # Mode Value | ||
] | ||
|
||
def power_state(device_id, state): | ||
|
||
def power_state(device_id: str, state: str) -> tuple[bool, str]: | ||
print(device_id, state) | ||
return True, state | ||
|
||
|
||
def range_value(device_id, range_value, instance_id): | ||
def range_value(device_id: str, range_value: Real, instance_id: str) -> tuple[bool, Real, str]: | ||
print(device_id, range_value, instance_id) | ||
return True, range_value, instance_id | ||
|
||
|
||
def mode_value(device_id, mode_value, instance_id): | ||
def mode_value(device_id: str, mode_value: Any, instance_id: str) -> tuple[bool, Any, str]: | ||
# TODO: what is mode_Value? str?Enum? | ||
print(device_id, mode_value, instance_id) | ||
return True, mode_value, instance_id | ||
|
||
|
||
callbacks = { | ||
callbacks: dict[str, CallbackFunctions] = { | ||
SinricProConstants.SET_POWER_STATE: power_state, | ||
SinricProConstants.SET_RANGE_VALUE: range_value, | ||
SinricProConstants.SET_MODE: mode_value | ||
} | ||
|
||
if __name__ == '__main__': | ||
loop = asyncio.get_event_loop() | ||
client = SinricPro(APP_KEY, [DEVICE_ID], callbacks, | ||
enable_log=False, restore_states=False, secret_key=APP_SECRET) | ||
loop: asyncio.AbstractEventLoop = asyncio.get_event_loop() | ||
client: SinricPro = SinricPro(APP_KEY, [DEVICE_ID], callbacks, | ||
enable_log=False, restore_states=False, secret_key=APP_SECRET) | ||
loop.run_until_complete(client.connect()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think mode_value can be str here.