from miio.miioprotocol import MiIOProtocol


class MiSwitch:
    def __init__(self, ip, token):
        self.protocol = MiIOProtocol(ip, token)

    def set_connect(self, status: bool):
        self.protocol.send('set_properties', [{'did': 'MYDID', 'siid': 2, 'piid': 1, 'value': status}])

    def on(self):
        self.set_connect(True)

    def off(self):
        self.set_connect(False)

    def power(self):
        return self.protocol.send('get_properties', [{'did': '845778715', 'siid': 11, 'piid': 2}])[0]['value']

    def is_on(self):
        return self.protocol.send('get_properties', [{'did': 'MYDID', 'siid': 2, 'piid': 1}])[0]['value']


if __name__ == '__main__':
    from flandre import C

    mi = MiSwitch(C.switch1_ip, C.switch1_token)
    print(mi.is_on())
    print(mi.power())
