Skip to content
This repository has been archived by the owner on Jun 7, 2021. It is now read-only.

Commit

Permalink
🏷️ Static typing
Browse files Browse the repository at this point in the history
  • Loading branch information
evaneliasyoung committed Dec 7, 2019
1 parent a65c281 commit 4f1f2ec
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 77 deletions.
35 changes: 35 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[mypy]
python_version = 3.7
files = wemo/*.py

# Disallow dynamic typing
disallow_any_unimported = True
disallow_any_expr = True
disallow_any_decorated = True
disallow_any_explicit = True
disallow_any_generics = True
disallow_subclassing_any = True

# Untyped definitions and calls
disallow_untyped_calls = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
check_untyped_defs = True
disallow_untyped_decorators = True

# None and Optional handling
no_implicit_optional = True
strict_optional = True

# Configuring warnings
warn_redundant_casts = True
warn_unused_ignores = True
warn_no_return = True
warn_return_any = True
warn_unreachable = True

# Miscellaneous strictness flags
allow_untyped_globals = False
allow_redefinition = False
implicit_reexport = True
strict_equality = True
40 changes: 20 additions & 20 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#!/usr/bin/env python3
from distutils.core import setup
setup(
name = 'wemo',
packages = ['wemo'],
version = '1.0.4',
description = 'A Wemo API',
author = 'Evan Young',
url = 'https://github.com/DocCodes/wemo',
download_url = 'https://github.com/DocCodes/wemo/archive/master.tar.gz',
keywords = ['wemo', 'api', 'automation'],
classifiers = [
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Topic :: Home Automation',
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
'Programming Language :: Python :: 3.6'
],
install_requires = [
'requests',
'pytest'
],
python_requires = '~=3.6'
name='wemo',
packages=['wemo'],
version='1.1.0',
description='A Wemo API',
author='Evan Elias Young',
url='https://github.com/evaneliasyoung/wemo',
download_url='https://github.com/evaneliasyoung/wemo/archive/master.tar.gz',
keywords=['wemo', 'api', 'automation'],
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Topic :: Home Automation',
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
'Programming Language :: Python :: 3.7'
],
install_requires=[
'requests',
'pytest'
],
python_requires='~=3.7'
)
129 changes: 72 additions & 57 deletions wemo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,85 @@
"""An easy way to integrate wemo switches
"""
from requests import get, post
from typing import Dict

__author__ = "Evan Young"
__copyright__ = "Copyright 2017, Evan Young"
__credits__ = "Evan Young"
__author__ = "Evan Elias Young"
__copyright__ = "Copyright 2017-2019, Evan Elias Young"
__credits__ = "Evan Elias Young"

__license__ = "GNU GLPv3"
__version__ = "1.0.4"
__maintainer__ = "Evan Young"
__version__ = "1.1.0"
__maintainer__ = "Evan Elias Young"
__status__ = "Production"


class switch:
def __init__(self, ip):
check_port_result = self.check(ip)
if(check_port_result == 0): exit
self.ip = ip
self.port = check_port_result
self.full = f'{self.ip}:{self.port}'
self.url = f'http://{self.full}/upnp/control/basicevent1'
self.status = self.getStatus()
self.name = self.getName()
def enable(self):
self.setStatus(1)
def disable(self):
self.setStatus(0)
def toggle(self):
self.setStatus(int(not(self.getStatus())))
def setStatus(self, state):
hd = self.xmlHeads('SetBinaryState')
data = self.xmlData('SetBinaryState', f'<BinaryState>{state}</BinaryState>')
post(self.url, headers=hd, data=data)
def getStatus(self):
hd = self.xmlHeads('GetBinaryState')
data = self.xmlData('GetBinaryState', '')
rsp = post(self.url, headers=hd, data=data).text
return int(self.tagger(rsp, 'BinaryState'))
def getName(self):
hd = self.xmlHeads('GetFriendlyName')
data = self.xmlData('GetFriendlyName', '')
rsp = post(self.url, headers=hd, data=data).text
return self.tagger(rsp, 'FriendlyName')
def xmlHeads(self, soapa):
return {'accept': '','content-type': "text/xml; charset='utf-8'",'SOAPACTION': f'"urn:Belkin:service:basicevent:1#{soapa}"'}
def xmlData(self, tag, val):
return f'<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:{tag} xmlns:u="urn:Belkin:service:basicevent:1">{val}</u:{tag}></s:Body></s:Envelope>'
def tagger(self, txt, tag):
ln = len(f'<{tag}>')
beg = txt.index(f'<{tag}>')
end = txt.index(f'</{tag}>')
return txt[beg+ln:end]
def check(self, ip):
port = 0
for test in range(49152, 49156):
try:
get(f'http://{ip}:{test}')
except:
pass
else:
port = test
return port
def __init__(self, ip: str) -> None:
check_port_result = self.check(ip)
if(check_port_result == 0):
exit
self.ip: str = ip
self.port: int = check_port_result
self.full: str = f'{self.ip}:{self.port}'
self.url: str = f'http://{self.full}/upnp/control/basicevent1'
self.status: str = self.getStatus()
self.name: str = self.getName()

def enable(self) -> None:
self.setStatus(1)

def disable(self) -> None:
self.setStatus(0)

def toggle(self) -> None:
self.setStatus(int(not(self.getStatus())))

def setStatus(self, state: int) -> None:
hd: Dict[str, str] = self.xmlHeads('SetBinaryState')
data: str = self.xmlData(
'SetBinaryState', f'<BinaryState>{state}</BinaryState>')
post(self.url, headers=hd, data=data)

def getStatus(self) -> int:
hd: Dict[str, str] = self.xmlHeads('GetBinaryState')
data: str = self.xmlData('GetBinaryState', '')
rsp: str = post(self.url, headers=hd, data=data).text
return int(self.tagger(rsp, 'BinaryState'))

def getName(self) -> str:
hd: Dict[str, str] = self.xmlHeads('GetFriendlyName')
data: str = self.xmlData('GetFriendlyName', '')
rsp: str = post(self.url, headers=hd, data=data).text
return self.tagger(rsp, 'FriendlyName')

def xmlHeads(self, soapa: str) -> Dict[str, str]:
return {'accept': '', 'content-type': "text/xml; charset='utf-8'", 'SOAPACTION': f'"urn:Belkin:service:basicevent:1#{soapa}"'}

def xmlData(self, tag: str, val: str) -> str:
return f'<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:{tag} xmlns:u="urn:Belkin:service:basicevent:1">{val}</u:{tag}></s:Body></s:Envelope>'

def tagger(self, txt: str, tag: str) -> str:
ln: int = len(f'<{tag}>')
beg: int = txt.index(f'<{tag}>')
end: int = txt.index(f'</{tag}>')
return txt[beg + ln:end]

def check(self, ip: str) -> int:
port: int = 0
for test in range(49152, 49156):
try:
get(f'http://{ip}:{test}')
except:
pass
else:
port = test
return port


if __name__ == '__main__':
print("Hello Console!")
bd = switch('192.168.1.72')
print("Hello Console!")
bd = switch('192.168.1.72')


def test_main():
assert True
def test_main() -> None:
assert True

0 comments on commit 4f1f2ec

Please sign in to comment.