synse package

Submodules

synse.client module

API clients for the Synse Server API.

class synse.client.HTTPClientV3(host: str, port: Optional[int] = 5000, timeout: Optional[float] = 10, session: Optional[aiohttp.client.ClientSession] = None, loop: Optional[asyncio.events.AbstractEventLoop] = None)[source]

Bases: object

An HTTP client for Synse Server’s v3 API.

Parameters:
  • host – The hostname/IP address for the Synse Server instance.
  • port – The port that Synse Server is listening on. (default: 5000)
  • session – A session for reusing connections.
  • timeout – The timeout (in seconds) for a request to Synse Server. (default: 10s)
  • loop – The event loop to use for the client.
api_version = 'v3'
close() → None[source]

Close the client session.

config() → synse.models.Config[source]

Get the unified configuration for the Synse Server instance.

Returns:The Synse v3 API config response.
info(device: str) → synse.models.DeviceInfo[source]

Get all information associated with the specified device.

Parameters:device (str) – The ID of the device to get information for.
Returns:The Synse v3 API info response.
make_request(method: str, url: str, params: Optional[Mapping[str, str]] = None, data: Any = None, json: Any = None, **kwargs) → Union[dict, list][source]

A helper method to issue a request to the configured Synse Server instance.

This method is intended to only be used internally by the client. If finer-grained control over a request is desired, the client user may choose to use this method over the defined API methods at their own risk.

Returns:

The JSON response data marshaled into its Python type (dict or list).

Raises:
  • errors.SynseError – An instance of a SynseError, wrapping any other error
  • which may have occurred. An error will be raised if the client fails to
  • make the request (e.g. connection issue, timeout, etc), or if the response
  • has a non-2xx status code.
plugin(plugin_id: str) → synse.models.PluginInfo[source]

Get all information associated with the specified plugin.

Parameters:plugin_id (str) – The ID of the plugin to get information for.
Returns:The Synse v3 API plugin response.
plugin_health() → synse.models.PluginHealth[source]

Get a summary of the health of all currently registered plugins.

Returns:The Synse v3 API plugin health response.
plugins() → List[synse.models.PluginSummary][source]

Get a summary of all plugins currently registered with the Synse Server instance.

Returns:The Synse v3 API plugins response.
read(ns: str = None, tags: Union[str, Sequence[str], Sequence[Sequence[str]]] = None) → List[synse.models.Reading][source]

Get the latest reading(s) for all devices which match the specified selector(s).

Parameters:
  • ns – The default namespace to use for the tags which do not include a namespace. (default: default)
  • tags – The tags to filter devices on. Tags may be specified in multiple ways. A single string (e.g. ‘foo/bar’) will be taken as a single tag group. Multiple strings (e.g. [‘foo/bar’, ‘abc/123’]) will be taken as a single tag group. Multiple collections of strings (e.g. [[‘foo/bar’], [‘abc/123’, ‘def/456’]]) will be taken as multiple tag groups.
Returns:

The Synse v3 API read response.

read_cache(start: str = None, end: str = None) → AsyncGenerator[synse.models.Reading, None][source]

Get a window of cached device readings.

Parameters:
  • start (str) – An RFC3339 formatted timestamp which specifies a starting bound on the cache data to return. If no timestamp is specified, there will not be a starting bound.
  • end (str) – An RFC3339 formatted timestamp which specifies an ending bound on the cache data to return. If no timestamp is specified, there will not be an ending bound.
Yields:

The Synse v3 API read cache response.

read_device(device: str) → List[synse.models.Reading][source]

Get the latest reading(s) for the specified device.

Parameters:device (str) – The ID of the device to get readings for.
Returns:The Synse v3 API read device response.
scan(force: bool = None, ns: str = None, sort: str = None, tags: Union[str, Sequence[str], Sequence[Sequence[str]]] = None) → List[synse.models.DeviceSummary][source]

Get a summary of all devices currently exposed by the Synse Server instance.

Parameters:
  • force (bool) – Force a re-scan (do not use the cache). If True, the request will take longer since the internal device cache will be rebuilt. Forcing a scan will ensure the set of returned devices is up-to-date.
  • ns (str) – The default namespace to use for the tags which do not include a namespace. (default: default)
  • sort (str) – Specify the fields to sort by. Multiple fields may be specified as a comma-separated string, e.g. “plugin,id”. The “tags” field can not be used for sorting. (default: “plugin,sort_index,id”, where the sort_index is an internal sort preference which a plugin can optionally specify.)
  • tags – The tags to filter devices on. Tags may be specified in multiple ways. A single string (e.g. ‘foo/bar’) will be taken as a single tag group. Multiple strings (e.g. [‘foo/bar’, ‘abc/123’]) will be taken as a single tag group. Multiple collections of strings (e.g. [[‘foo/bar’], [‘abc/123’, ‘def/456’]]) will be taken as multiple tag groups.
Returns:

The Synse v3 API scan response.

status() → synse.models.Status[source]

Check the availability and connectivity status of the Synse Server instance.

If the instance is reachable, this request will resolve; otherwise, it will raise an error.

Returns:The Synse v3 API status response.
stream_request(method: str, url: str, params: Optional[Mapping[str, str]] = None, **kwargs) → AsyncGenerator[Union[dict, list], None][source]

A helper method to issue a request which will return a streamed response from the configured Synse Server instance.

This method is intended to only be used internally by the client. If finer-grained control over a request is desired, the user may choose to use this method over the defined API methods at their own risk.

Returns:

The streamed response data.

Raises:
  • errors.SynseError – An instance of a SynseError, wrapping any other error
  • which may have occurred. An error will be raised if the client fails to
  • make the request (e.g. connection issue, timeout, etc), or if the response
  • has a non-2xx status code.
sync(coro: Coroutine)[source]

Run an asynchronous API call synchronously by wrapping it with this function.

Examples

r = client.sync(client.status())

tags(ns: Union[str, Sequence[str]] = None, ids: bool = False) → List[str][source]

Get a list of the tags currently associated with registered devices.

Parameters:
  • ns – The tag namespace(s) to use when searching for tags. (default: default)
  • ids – Include ID tags in the response. (default: false)
Returns:

The Synse v3 API tags response.

transaction(transaction_id: str) → synse.models.TransactionStatus[source]

Get the status of an asynchronous write transaction.

Parameters:transaction_id (str) – The ID of the transaction to get the status of.
Returns:The Synse v3 API transaction response.
transactions() → List[str][source]

Get a list of the transactions currently tracked by Synse.

Returns:The Synse v3 API transactions response.
version() → synse.models.Version[source]

Get the version information for the configured Synse Server instance.

Returns:The Synse v3 API version response.
write_async(device: str, payload: Union[List[dict], dict]) → List[synse.models.TransactionInfo][source]

Write to the specified device asynchronously.

This method will queue up a write with the device’s plugin and will immediately return information for the transaction associated with the write. This transaction can be checked later to ensure the write completed successfully.

Parameters:
  • device (str) – The ID of the device to write to.
  • payload (list[dict] | dict) – The write payload.
Returns:

The Synse v3 API asynchronous write response.

write_sync(device: str, payload: Union[List[dict], dict]) → List[synse.models.TransactionStatus][source]

Write to the specified device synchronously.

This method will wait until the write action has completed. It is up to the caller to ensure that a suitable timeout is set for the request.

Parameters:
  • device (str) – The ID of the device to write to.
  • payload (list[dict] | dict) – The write payload.
Returns:

The Synse v3 API synchronous write response.

class synse.client.WebsocketClientV3(host: str, port: Optional[int] = 5000, session: Optional[aiohttp.client.ClientSession] = None, loop: Optional[asyncio.events.AbstractEventLoop] = None, **connect_kwargs)[source]

Bases: object

A WebSocket client for Synse Server’s v3 API.

Parameters:
  • host – The hostname/IP address for the Synse Server instance.
  • port – The port that Synse Server is listening on. (default: 5000)
  • session – A session for reusing connections.
  • loop – The event loop to use for the client.
api_version = 'v3'
close() → None[source]

Close the WebSocket client session.

config() → synse.models.Config[source]

Get the unified configuration for the Synse Server instance.

Returns:The Synse v3 API config response.
connect()[source]

Establish a WebSocket client connection with Synse Server.

connection
info(device: str) → synse.models.DeviceInfo[source]

Get all information associated with the specified device.

Parameters:device – The ID of the device to get information for.
Returns:The Synse v3 API info response.
plugin(plugin_id: str) → synse.models.PluginInfo[source]

Get all information associated with the specified plugin.

Parameters:plugin_id – The ID of the plugin to get information for.
Returns:The Synse v3 API plugin response.
plugin_health() → synse.models.PluginHealth[source]

Get a summary of the health of all currently registered plugins.

Returns:The Synse v3 API plugin health response.
plugins() → List[synse.models.PluginSummary][source]

Get a summary of all plugins currently registered with the Synse Server instance.

Returns:The Synse v3 API plugins response.
read(ns: str = None, tags: Union[str, Sequence[str], Sequence[Sequence[str]]] = None) → List[synse.models.Reading][source]

Get the latest reading(s) for all devices which match the specified selector(s).

Parameters:
  • ns – The default namespace to use for the tags which do not include a namespace. (default: default)
  • tags – The tags to filter devices on. Tags may be specified in multiple ways. A single string (e.g. ‘foo/bar’) will be taken as a single tag group. Multiple strings (e.g. [‘foo/bar’, ‘abc/123’]) will be taken as a single tag group. Multiple collections of strings (e.g. [[‘foo/bar’], [‘abc/123’, ‘def/456’]]) will be taken as multiple tag groups.
Returns:

The Synse v3 API read response.

read_cache(start: str = None, end: str = None) → Generator[[synse.models.Reading, None], None][source]

Get a window of cached device readings.

Parameters:
  • start – An RFC3339 formatted timestamp which specifies a starting bound on the cache data to return. If no timestamp is specified, there will not be a starting bound.
  • end – An RFC3339 formatted timestamp which specifies an ending bound on the cache data to return. If no timestamp is specified, there will not be an ending bound.
Yields:

The Synse v3 API read cache response.

read_device(device: str) → List[synse.models.Reading][source]

Get the latest reading(s) for the specified device.

Parameters:device – The ID of the device to get readings for.
Returns:The Synse v3 API read device response.
read_stream(ids: List[str] = None, tags: Union[str, Sequence[str], Sequence[Sequence[str]]] = None, stop: bool = False) → Generator[[synse.models.Reading, None], None][source]

Get a stream of current reading data from Synse Server.

When the stream starts, all new readings will be provided as they are read. In order to stop the stream, this method needs to be called again with the stop parameter set to True.

Parameters:
  • ids – A list of device IDs which can be used to constrain the devices for which readings should be streamed. If no IDs are specified, no filtering by ID is done.
  • tags – A collection of tag groups to constrain the devices for which readings should be streamed. The tags within a group are subtractive (e.g. a device must match all tags in the group to match the filter), but each tag group specified is additive (e.g. readings will be streamed for the union of all specified groups). If no tag groups are specified, no filtering by tags is done.
  • stop – A boolean value indicating whether or not to stop the reading stream.
Returns:

A stream of current reading data.

request(event, data=None) → Union[dict, list][source]

A helper method to issue a WebSocket API request to the configured Synse Server instance.

This method is intended to only be used internally by the client. If finer-grained control over a request is desired, the client user may choose to use this method over the defined API methods at their own risk.

Returns:

The JSON response data marshaled into its Python type (e.g., dict or list).

Raises:
  • errors.SynseError – An instance of a SynseError, wrapping any other error
  • which may have occurred. An error will be raised if the client fails to
  • make the request (e.g. connection issue, timeout, etc).
scan(force: bool = None, ns: str = None, sort: str = None, tags: Union[str, Sequence[str], Sequence[Sequence[str]]] = None) → Generator[[synse.models.DeviceSummary, None], None][source]

Get a summary of all devices currently exposed by the Synse Server instance.

Parameters:
  • force – Force a re-scan (do not use the cache). If True, the request will take longer since the internal device cache will be rebuilt. Forcing a scan will ensure the set of returned devices is up-to-date.
  • ns – The default namespace to use for the tags which do not include a namespace. (default: default)
  • sort – Specify the fields to sort by. Multiple fields may be specified as a comma-separated string, e.g. “plugin,id”. The “tags” field can not be used for sorting. (default: “plugin,sort_index,id”, where the sort_index is an internal sort preference which a plugin can optionally specify.)
  • tags – The tags to filter devices on. Tags may be specified in multiple ways. A single string (e.g. ‘foo/bar’) will be taken as a single tag group. Multiple strings (e.g. [‘foo/bar’, ‘abc/123’]) will be taken as a single tag group. Multiple collections of strings (e.g. [[‘foo/bar’], [‘abc/123’, ‘def/456’]]) will be taken as multiple tag groups.
Returns:

The Synse v3 API scan response.

status() → synse.models.Status[source]

Check the availability and connectivity status of the Synse Server instance.

If the instance is reachable, this request will resolve; otherwise, it will raise an error.

Returns:The Synse v3 API status response.
stream_request(event, data=None) → Generator[[Union[dict, list], None], None][source]

A helper method to issue a WebSocket API request to the configured Synse Server instance where the response is expected to be streamed back as multiple individual messages.

This method is intended to only be used internally by the client. If finer-grained control over a request is desired, the client user may choose to use this method over the defined API methods at their own risk.

Yields:

The JSON response data marshaled into its Python type (e.g., dict or list).

Raises:
  • errors.SynseError – An instance of a SynseError, wrapping any other error
  • which may have occurred. An error will be raised if the client fails to
  • make the request (e.g. connection issue, timeout, etc).
tags(ns: str = None, ids: bool = None) → List[str][source]

Get a list of the tags currently associated with registered devices.

Parameters:
  • ns – The tag namespace(s) to use when searching for tags.
  • ids – Include ID tags in the response. (default: false)
Returns:

The Synse v3 API tags response.

transaction(transaction_id: str) → synse.models.TransactionStatus[source]

Get the status of an asynchronous write transaction.

Parameters:transaction_id – The ID of the transaction to get the status of.
Returns:The Synse v3 API transaction response.
transactions() → List[str][source]

Get a list of the transactions currently tracked by Synse.

Returns:The Synse v3 API transactions response.
version() → synse.models.Version[source]

Get the version information for the configured Synse Server instance.

Returns:The Synse v3 API version response.
write_async(device: str, payload: Union[List[dict], dict]) → List[synse.models.TransactionInfo][source]

Write to the specified device asynchronously.

This method will queue up a write with the device’s plugin and will immediately return information for the transaction associated with the write. This transaction can be checked later to ensure the write completed successfully.

Parameters:
  • device – The ID of the device to write to.
  • payload – The write payload.
Returns:

The Synse v3 API asynchronous write response.

write_sync(device: str, payload: Union[List[dict], dict]) → List[synse.models.TransactionStatus][source]

Write to the specified device synchronously.

This method will wait until the write action has completed. It is up to the caller to ensure that a suitable timeout is set for the request.

Parameters:
  • device – The ID of the device to write to.
  • payload – The write payload.
Returns:

The Synse v3 API synchronous write response.

synse.errors module

Errors and exceptions for the Synse API client.

exception synse.errors.InvalidInput[source]

Bases: synse.errors.SynseError

An error indicating that some user input to the API was invalid.

HTTP Error Code: 400

exception synse.errors.NotFound[source]

Bases: synse.errors.SynseError

An error indicating that a specified resource was not found.

HTTP Error Code: 404

exception synse.errors.ServerError[source]

Bases: synse.errors.SynseError

A general error indicating something went wrong during the server-side processing of the request.

HTTP Error Code: 500

exception synse.errors.SynseError[source]

Bases: Exception

The base exception for all errors raised by the Synse API client.

synse.errors.wrap_and_raise_for_error(response: aiohttp.client_reqrep.ClientResponse, body: Any = None) → None[source]

Check if the response failed with a non-2XX status code.

If the response has a non-2XX error code, wrap it in the appropriate client exception and re-raise. Otherwise, do nothing.

Parameters:
  • response – The response to check for a non-OK status.
  • body – The data which provides context for the error. If this is not specified, the function will attempt to load it from response.text(), and fall back to response.reason. Note that this behavior is undesirable in some cases, such as for handling streamed responses, as calling response.text() will prevent the content from being loaded correctly. In such cases, a non-None body must be provided.
Raises:

SynseError – The response had a non-2XX error code.

synse.models module

Synse client response model definitions.

class synse.models.BaseResponse(data: dict)[source]

Bases: object

The base class for all of the Synse client’s response classes.

_data

The JSON data returned in the response, as a dictionary.

class synse.models.Config(data: dict)[source]

Bases: synse.models.BaseResponse

Config models the response for Synse Server’s v3 HTTP API /v3/config endpoint.

It does not define any attributes. Instead, the configuration value should be taken from the response’s data field, which holds the JSON data returned in the response.

class synse.models.DeviceInfo(data: dict)[source]

Bases: synse.models.BaseResponse

DeviceInfo models the response for Synse Server’s v3 HTTP API /v3/info/<device> endpoint.

alias

A human-readable name for the device.

Type:str
capabilities

Specifies the actions which the device is able to perform (e.g. read, write).

Type:dict
id

The globally unique ID for the device.

Type:str
info

A human readable string providing identifying info about a device.

Type:str
metadata

A dictionary of arbitrary values that provide additional data for the device.

Type:dict
outputs

A list of the output types that the device supports.

Type:list[dict]
plugin

The ID of the plugin that manages the device.

Type:str
sort_index

A custom sort index specified for the device by its plugin. The default value of 0 indicates no special sorting.

Type:int
tags

A list of the tags associated with this device.

Type:list[str]
timestamp

A timestamp describing the time that the device info was gathered.

Type:datetime.datetime
type

The device type, as specified by the plugin.

Type:str
class synse.models.DeviceSummary(data: dict)[source]

Bases: synse.models.BaseResponse

DeviceSummary models the response for Synse Server’s v3 HTTP API /v3/scan endpoint.

alias

The human-readable alias for the device, if set.

Type:str
id

The globally unique ID for the device.

Type:str
info

A human readable string providing identifying info about a device.

Type:str
plugin

The ID of the plugin which the device is managed by.

Type:str
tags

A list of the tags associated with this device.

Type:list[str]
type

The type of the device, as defined by its plugin.

Type:str
class synse.models.PluginHealth(data: dict)[source]

Bases: synse.models.BaseResponse

PluginHealth models the response for Synse Server’s v3 HTTP API /v3/plugin/health endpoint.

active

The count of active plugins.

Type:int
healthy

A list containing the plugin IDs for those plugins deemed to be healthy.

Type:list[str]
inactive

The count of inactive plugins.

Type:int
status

A string describing the overall health state of the registered plugin(s). This can be either “healthy” or “unhealthy”. It will only be healthy if all plugins are found to be healthy, otherwise the overall state is unhealthy.

Type:str
unhealthy

A list containing the plugin IDs for those plugins deemed to be unhealthy.

Type:list[str]
updated

A timestamp describing the time that the plugin health state was last updated.

Type:datetime.datetime
class synse.models.PluginInfo(data: dict)[source]

Bases: synse.models.BaseResponse

PluginInfo models the response for Synse Server’s v3 HTTP API /v3/plugin/<plugin-id> endpoint.

active

Specifies whether the plugin is currently active or not.

Type:bool
description

A short description of the plugin.

Type:str
health

Describes the overall health of the plugin.

Type:dict
id

An ID hash for identifying the plugin, generated from plugin metadata.

Type:str
maintainer

The maintainer of the plugin.

Type:str
name

The name of plugin.

Type:str
network

Describes the network configurations for the plugin.

Type:dict
tag

The plugin tag. This is a normalized string made up of its name and maintainer.

Type:str
version

The version information about the plugin.

Type:dict
vcs

A link to the version control repo for the plugin.

Type:str
class synse.models.PluginSummary(data: dict)[source]

Bases: synse.models.BaseResponse

PluginSummary models the response for Synse Server’s v3 HTTP API /v3/plugin endpoint.

active

Specifies whether the plugin is currently active or not.

Type:bool
description

A short description of the plugin.

Type:str
id

An ID hash for identifying the plugin, generated from plugin metadata.

Type:str
maintainer

The maintainer of the plugin.

Type:str
name

The name of plugin.

Type:str
tag

The plugin tag. This is a normalized string made up of the name and maintainer.

Type:str
class synse.models.Reading(data: dict)[source]

Bases: synse.models.BaseResponse

Reading models the response for Synse Server’s v3 HTTP API /v3/read, /v3/read/<device>, and /v3/readcache endpoints.

context

A mapping of arbitrary values to provide additional context for the reading.

Type:dict
device

The ID of the device which the reading originated from.

Type:str
device_type

The type of the device.

Type:str
timestamp

A timestamp describing the time at which the reading was taken.

Type:datetime.datetime
type

The type of the reading.

Type:str
unit

The unit of measure for the reading.

Type:dict
value

The value of the reading.

class synse.models.Status(data: dict)[source]

Bases: synse.models.BaseResponse

Status models the response for Synse Server’s v3 HTTP API /test endpoint.

status

“ok” if the endpoint returns successfully.

Type:str
timestamp

The time at which the status was tested.

Type:datetime.datetime
class synse.models.TransactionInfo(data: dict)[source]

Bases: synse.models.BaseResponse

TransactionInfo models the response for Synse Server’s v3 HTTP API /v3/write/<device> endpoint.

context

The data written to the device. This is provided as context info to help identify the write action.

Type:dict
device

The ID of the device being written to.

Type:str
id

The ID for the transaction.

Type:str
timeout

The timeout for the write transaction, after which it will be cancelled. This is effectively the maximum wait time for the transaction to resolve.

Type:str
class synse.models.TransactionStatus(data: dict)[source]

Bases: synse.models.BaseResponse

TransactionStatus models the response for Synse Server’s v3 HTTP API /v3/transaction/<transaction-id> and /v3/write/wait/<device> endpoints.

context

The POSTed write data for the given write transaction.

Type:dict
created

The time at which the transaction was created.

Type:datetime.datetime
device

The ID of the device being written to.

Type:str
id

The ID of the transaction.

Type:str
message

Any context information relating to a transaction’s error state. If there is no error, this will be an empty string.

Type:str
status

The current status of the transaction. (pending, writing, done, error)

Type:str
timeout

A string representing the timeout for the write transaction after which it will be cancelled. This is effectively the maximum wait time for the transaction to resolve.

Type:str
updated

The last time the transaction state or status was updated. Once the transaction reaches a done status or error state, no further updates will occur.

Type:datetime.datetime
class synse.models.Version(data: dict)[source]

Bases: synse.models.BaseResponse

Version models the response for Synse Server’s v3 HTTP API /version endpoint.

api_version

The API version (v<major>) of the Synse Server instance.

Type:str
version

The full version (<major>.<minor>.<patch>) of the Synse Server instance.

Type:str
synse.models.make_response(response_class: Optional[Type[synse.models.BaseResponse]], response: Union[dict, list])[source]

Populate a Synse response object with data from the given HTTP response.

Parameters:
  • response_class – The Synse response class to load the data into.
  • response – The JSON response from Synse Server.

synse.utils module

Utilities for the Synse Python Client.

synse.utils.process_ws_response(response: aiohttp.http_websocket.WSMessage) → Union[dict, list][source]

A utility function to process the response from the Synse Server WebSocket API.

Parameters:

response – The WebSocket response message.

Returns:

The JSON response edata marshaled into its Python type (e.g., dict or list).

Raises:
  • errors.SynseError – An instance of a SynseError, wrapping any other error
  • which may have occurred.
synse.utils.tag_params(tags: Union[None, str, List[str], Tuple[str], Sequence[Union[List[str], Tuple[str]]]], params: multidict._multidict.MultiDict) → multidict._multidict.MultiDict[source]

Generate tag query parameters for a request.

If no tags are specified, nothing is added to the query params MultiDict.

Tags may be specified in a number of ways: - A single tag (single tag group) - A collection of tags (single tag group) - A collection of collections of tags (multiple tag groups)

A single tag group is used to filter devices which match all of the tags in the group. If multiple tag groups are specified, the end result will be the set union of the results of the individual tag group filters.

Parameters:
  • tags – The tags to process into query parameters.
  • params – The MultiDict which holds the query parameters for the request.
Returns
The ‘params’ MultiDict which was provided as a parameter.
Raises:ValueError – The incoming tags are specified in an unsupported format.

Module contents

synse - a Python client for interacting with Synse Server.