Last active
June 11, 2024 06:40
-
-
Save DrunkenAlcoholic/e99611e8e806d4845e30e94f6bcdca5b to your computer and use it in GitHub Desktop.
Sensibo API
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
## Sensibo, fetches current temperature, humidity | |
## from users API key and DeviceID | |
## API documentation found at https://sensibo.github.io | |
## eventually want to convert https://github.com/Sensibo/sensibo-python-sdk to the Nim language | |
import std/[httpclient, json] | |
type | |
acStates = object | |
on: bool | |
mode: string | |
fanLevel: string | |
targetTemperature: float | |
temperatureUnit: string | |
swing: string | |
SensiboTimeObj = object | |
time: string | |
secondsAgo: int | |
SensiboResultObj = object | |
time: SensiboTimeObj | |
temperature: float | |
humidity: float | |
feelsLike: float | |
rssi: float | |
SensiboObj = object | |
status: string | |
result: seq[SensiboResultObj] | |
# Example Json string returned | |
# {"status":"success","result":[{"time":{"time":"2024-01-05T06:41:16.461393Z","secondsAgo":49},"temperature":28.2,"humidity":62.9,"feelsLike":30.1,"rssi":-73}]} | |
const | |
Url = "https://home.sensibo.com/api/v2/pods/" | |
ApiKey = "xxxxxxxxxxxxxxxxxxxxxxx" | |
DevID = "xxxxxxxxxx" | |
## Set API key and device ID | |
proc fetchSensibo(): SensiboObj = | |
## Fetch sensibo data function | |
var body = "" | |
# Create a new Http Client for fetching data | |
var client = newHttpClient() | |
try: | |
# Get data from Url | |
var responce = client.get( Url & DevID & "/measurements?apiKey=" & ApiKey) | |
# Place Client body data into a variable | |
body = responce.body | |
except CatchableError as e: | |
echo "An error :", e.repr | |
finally: | |
client.close() | |
# Parse Jason data as a SensiboObj type, and return | |
return parseJson(body).to(SensiboObj) | |
proc SetAcOnOff(onOff: bool): string = | |
var | |
body: string = "" | |
client: HttpClient = newHttpClient() | |
jsonObject = %* {"newValue": onOff} | |
try: | |
var responce: Response = client.patch( Url & DevID & "/acStates/on?apiKey=" & ApiKey, $jsonObject) | |
body = responce.body | |
except CatchableError as e: | |
echo "An error :", e.repr | |
finally: | |
client.close() | |
return body | |
proc main() = | |
## Main entry point | |
try: | |
var meassurements: SensiboObj = fetchSensibo() | |
# Test to see if we can read the value | |
echo meassurements.result[0].temperature, "°" | |
echo meassurements.result[0].humidity, '%' | |
echo meassurements.result[0].feelsLike, "°" | |
echo meassurements.result[0].rssi | |
echo meassurements.status | |
echo SetAcOnOff(false) | |
except CatchableError as e: | |
echo "Failed to fetch Sensibo meassurements ", e.repr | |
when isMainModule: | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment