Source code for switchbot_utility.switchbot_device

import json
import sys

import requests

from .switchbot import Switchbot


[docs]class SwitchbotDevice(Switchbot): """Switchbot device class""" _body = {"commandType": "command", "parameter": "default"} _baseurl = "https://api.switch-bot.com/v1.1/devices/" def __init__(self, deviceId): """Constructor""" self.deviceId = deviceId
[docs] def get_status(self) -> dict: """Get device information""" header = self.gen_sign() response = requests.get( self._baseurl + self.deviceId + "/status", headers=header, ) status = json.loads(response.text) if status["message"] != "success": sys.exit(status["message"]) else: return status["body"]
[docs] def command(self, deviceId: str, body: dict): """Send command""" header = self.gen_sign() return requests.post( self._baseurl + deviceId + "/commands", headers=header, data=json.dumps(body), )