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

New Function? Get Instant Power Usage #4

Open
simmyr94 opened this issue Dec 12, 2019 · 0 comments
Open

New Function? Get Instant Power Usage #4

simmyr94 opened this issue Dec 12, 2019 · 0 comments

Comments

@simmyr94
Copy link

simmyr94 commented Dec 12, 2019

Hello evaneliasyoung,
I'm trying to use your code to develop a new function, to show the instant power usage of Wemo Insight.
But for some reason it didn't work really well, is there any suggest/tip you can offer?

Thank you

Here is the code:

class power:
    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/insight1'

    def xmlHeads(self, soapa: str) -> Dict[str, str]:
        return {'accept': '', 'content-type': "text/xml; charset='utf-8'", 'SOAPACTION': f'"urn:Belkin:service:insight: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:insight: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 getPower(self) -> str:
        hd: Dict[str, str] = self.xmlHeads('GetPower')
        data: str = self.xmlData('GetPower', '')
        rsp: str = post(self.url, headers=hd, data=data).text
        return int(self.tagger(rsp, 'InstantPower'))

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

if __name__ == '__main__':
    print("Hello Console!")
    wemoname = switch('192.168.1.20')
    wemopower = power('192.168.1.20')


def test_main() -> None:
    assert True

wemopower.getPower()


'''
If will give me a 501 error if I try to  request API
'''
print(post('http://192.168.1.20:49153/upnp/control/insight1',headers = {'accept': '', 'content-type': "text/xml; charset='utf-8'", 'SOAPACTION': '"urn:Belkin:service:insight:1#GetPower"'}, data='<?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:GetPower xmlns:u="urn:Belkin:service:insight:1"></u:GetPower></s:Body></s:Envelope>').text)
'''
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants