Client APIs¶
The Knut Message¶
As mentioned in the previous sections, Knut provides various APIs for clients to
control services of different types. For example, to control a light service
like the PyTradfriLight, the
corresponding Light API is used.
When running knutserver, a TCP server is bound on default to the configured
address and port (see Configuration). To interact with Knut, a client can send a
JSON message terminated with a null byte \0.
Each message to be send to Knut or send form Knut, must have the following JSON schema:
{
"$schema": "http://json-schema.org/draft/2019-09/schema",
"title": "Knut Message",
"type": "object",
"required": ["apiId", "msgId", "msg"],
"properties": {
"apiId": {
"type": "integer",
"description": "API identifier"
},
"msgId": {
"type": "integer",
"description": "Message identifier"
},
"msg": {
"type": "object",
"description": "API specific message",
"default": {}
}
}
}
For example, sending a message via netcat as client with the Knut server
bound to 127.0.0.1:8080 could look like the following:
$ echo -ne '{"apiId": 0, "msgId": 0, "msg": {}}\0' | netcat localhost 8080
The key apiId is an identifier of the API which should be used to process
the message. The msgId tells the API which message is received and msg
contains finally an individual JSON message which is supported by the API.
See also
Module knut.server for all configurable server.
The following section describes all available APIs with their supported messages.
Supported Messages¶
Temperature¶
-
apiId= 1
-
TEMPERATURE_STATUS_REQUEST= 1¶ Requests the status of a back-end. The message message must have the key
idwhich is the identifier of the requested back-end. For example:{ "id": "myTemperatureBackend" }
-
TEMPERATURE_STATUS_RESPONSE= 2¶ Upon a
TEMPERATURE_STATUS_REQUESTor change of the back-end is a status response send which has the following keys:The identifier
idof the back-end.The
locationwhere the temperature is measured.The
unitin which the temperature value is provided.The
conditionat the location as a code point for the Weather Icons font.The
temperaturevalue.
{ "id": "myTemperatureBackend", "location": "Miami", "unit": "K", "condition": "\\uf00d", "temperature": 275.13 }
-
TEMPERATURE_LIST_REQUEST= 3¶ Requests a list of all temperature back-ends with their status. The
msgcan be an empty.
-
TEMPERATURE_LIST_RESPONSE= 4¶ The temperature list response is similar to the
TEMPERATURE_STATUS_RESPONSE, only with the keybackends. The value ofbackendsis an array containing the status response of all known back-ends. For example:{ "backends": [ { "id": "myTemperatureBackend1", "location": "Miami", "unit": "K", "condition": "\\uf00d", "temperature": 303.15 }, { "id": "myTemperatureBackend1", "location": "Hamburg", "unit": "K", "condition": "\\uf008", "temperature": 288.05 } ] }
-
TEMPERATURE_HISTORY_REQUEST= 5¶ Request the temperature history of a back-end. The
msgequals theTEMPERATURE_STATUS_REQUESTmessage.
-
TEMPERATURE_HISTORY_RESPONSE= 6¶ The response upon a temperature history request has the following keys:
The identifier
idof the back-end.The
temperaturewith an array of temperature values.The
timewith an array of UNIX timestamps where the entries correspond to the temperature array.
For example:
{ "id": "myTemperatureBackend", "temperature": [ 288.05, 291.05 ], "time": [ 1581863822.2132704, 1581863882.2132704 ] }
Light¶
-
apiId= 2
-
LIGHT_STATUS_REQUEST= 1¶ Requests the status of a back-end. The message has the key
idwith the identifier of the back-end as value. The server will respond with aLIGHT_STATUS_RESPONSE. For example:{ "id": "myLightBackend" }
-
LIGHT_STATUS_RESPONSE= 2¶ The status of a back-end with the following keys:
The identifier
idof the back-end.The
locationwhere the light is located inside a room.The
roomin which the light is.The
stateof the light whether it is on or off as boolean.The
hasTemperaturekey indicating whether the light has a light temperature.The
hasDimlevelkey indicating whether the light is dimmable.The
hasColorkey indicating whether the light can change its color.The lights
temperatureas percentage value where 0 is the coldest and 100 the warmest color temperature.The
colorColddefining the lights color when the temperature is 0 as hex code.The
colorWarmdefining the lights color when the temperature is 100 as hex code.The
dimlevelin percentage.The light’s
coloras hex code.
Note
A client can send the status response to change the state of the light. When sending from a client, only the
idkey is required and the key of which the value should be changed.For example a response send by the server:
{ "id": "myLightBackend", "location": "Somewhere in a room...", "room": "Some room", "state": false, "hasTemperature": true, "hasDimlevel": true, "hasColor": false, "temperature": 100, "colorCold": "#f5faf6", "colorWarm": "#efd275", "dimlevel": 50, "color": "" }
An example response send by a client to switch the light on:
{ "id": "myLightBackend", "state": true }
-
LIGHTS_REQUEST= 3¶ Requests the status of all back-ends. The message
msgcan be empty.
-
LIGHTS_RESPONSE= 4¶ The lights response is similar to the
LIGHT_STATUS_RESPONSE, only with the keybackends. The value ofbackendsis an array containing the status response of all known back-ends. For example:{ "backends": [ { "id": "myLightBackend1", "location": "Somewhere in a room...", "room": "Some room", "state": false, "hasTemperature": true, "hasDimlevel": true, "hasColor": false, "temperature": 100, "colorCold": "#f5faf6", "colorWarm": "#efd275", "dimlevel": 50, "color": "" }, { "id": "myLightBackend1", "location": "Somewhere else in a room...", "room": "Some other room", "state": true, "hasTemperature": true, "hasDimlevel": true, "hasColor": false, "temperature": 100, "colorCold": "#f5faf6", "colorWarm": "#efd275", "dimlevel": 50, "color": "" } ] }
-
ALL_LIGHTS_REQUEST= 5¶ Requests the combined state of all lights. The message can be empty.
-
ALL_LIGHTS_RESPONSE= 6¶ The response has the key
state, where the value -1 indicates all lights off and 1 all lights on. If the value is 0, neither all lights are on nor off.Note
If this message is send from a client, only -1 and 1 can be set as value.
For example:
{ "state": 1 }
-
ROOMS_LIST_REQUEST= 7¶ Requests a list of all rooms with their state. The message can be empty.
-
ROOMS_LIST_RESPONSE= 8¶ The response has a key
roomswith an array as value containing room state objects. A room state object has the following keys:The identifier
idof the room.The room’s
statewith the possible value -1, 0 and 1 analog to theALL_LIGHTS_RESPONSE.
For example:
{ "rooms": [ { "id": "myRoom1", "state": -1 }, { "id": "myRoom2", "state": 0 } ] }
-
ROOM_REQUEST= 9¶ Requests to set the state for a room. The message has the following keys:
The identifier
idof the room.The
statewhich should be set, where only -1 and 1 are valid values.
{ "id": "myRoom1", "state": -1 }
-
ROOM_RESPONSE= 10¶ The response has the same keys as the
ROOM_REQUEST, only that the value ofstatecan be also 0.
Task¶
-
apiId= 3
-
REMINDER= 1¶ A message that is send as reminder. It has the following keys:
The identifier
idof the task.The
timeRemainingin seconds before due.
{ "id": "f3b14c5e-8458-11ea-9daa-b88a60bd7559", "timeRemaining": 3600 }
-
TASK_REQUEST= 2¶ Requests the task with the identifier
id.{ "id": "f3b14c5e-8458-11ea-9daa-b88a60bd7559" }
-
TASK_RESPONSE= 3¶ The task response has the following keys:
The identifier
idof the task.The
assigneewho has the task assigned.The
authorof the task.The task
description.The boolean
donewhich describes whether the task is done or not.The
duetime of the task as UNIX timestamp.The UNIX timestamp
reminder, when aREMINDERshould be send.The
titleof the task.
Note
If the task response is send by a client with an empty
id, a new task will be created.{ "id": "f3b14c5e-8458-11ea-9daa-b88a60bd7559", "assignee": "John", "author": "Doug", "description": "We need 12 bottles Champagne for the party tonight!", "done": false, "due": 1577815200, "reminder": 1577800800, "title": "Drinks for the party" }
-
ALL_TASKS_REQUEST= 4¶ Requests a list of all tasks. The message can be empty.
-
ALL_TASKS_RESPONSE= 5¶ The response has the key
taskswith an array as value, containing the object of theTASK_RESPONSEfor each task. For example:{ "tasks": [ { "id": "f3b14c5e-8458-11ea-9daa-b88a60bd7559", "assignee": "John", "author": "Doug", "description": "We need 12 bottles Champagne for the party tonight!", "done": false, "due": 1577815200, "reminder": 1577800800, "title": "Drinks for the party" }, { "id": "f3b14c5e-8458-11ea-9daa-va131fassd59", "assignee": "John", "author": "Doug", "description": "Just saw we ran out of cigars!", "done": true, "due": 1577815200, "reminder": 1577800800, "title": "Fill up the humidor" } ] }
-
DELETE_TASK_REQUEST= 6¶ Requests to delete a task. The message has the identifier key
idof the task which should be deleted.
Local¶
-
apiId= 4
-
LOCAL_REQUEST= 1¶ Requests local information. The message
msgcan be empty.
-
LOCAL_RESPONSE= 2¶ The local information response has the following keys:
The identifier
idof the local object.The boolean indicator
isDaylightdescribing whether the sun has already set or not.The
locationof the location information.The next
sunriseas UNIX timestamp.The next
sunsetas UNIX timestamp.
For example:
{ "id": "home", "isDaylight": true, "location": "Beverly Hills", "sunrise": 1589513114.5880833, "sunset": 1589483203.418921 }