2018-10-01 09:21:00 +00:00
|
|
|
"""Message templates for websocket commands."""
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
|
|
|
|
from . import const
|
|
|
|
|
|
|
|
|
2019-09-29 17:07:49 +00:00
|
|
|
# mypy: allow-untyped-defs
|
|
|
|
|
2018-10-01 09:21:00 +00:00
|
|
|
# Minimal requirements of a message
|
2019-07-31 19:25:30 +00:00
|
|
|
MINIMAL_MESSAGE_SCHEMA = vol.Schema(
|
|
|
|
{vol.Required("id"): cv.positive_int, vol.Required("type"): cv.string},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
# Base schema to extend by message handlers
|
2019-07-31 19:25:30 +00:00
|
|
|
BASE_COMMAND_MESSAGE_SCHEMA = vol.Schema({vol.Required("id"): cv.positive_int})
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def result_message(iden, result=None):
|
|
|
|
"""Return a success result message."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return {"id": iden, "type": const.TYPE_RESULT, "success": True, "result": result}
|
2018-10-01 09:21:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def error_message(iden, code, message):
|
|
|
|
"""Return an error result message."""
|
|
|
|
return {
|
2019-07-31 19:25:30 +00:00
|
|
|
"id": iden,
|
|
|
|
"type": const.TYPE_RESULT,
|
|
|
|
"success": False,
|
|
|
|
"error": {"code": code, "message": message},
|
2018-10-01 09:21:00 +00:00
|
|
|
}
|
2019-03-11 03:07:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
def event_message(iden, event):
|
|
|
|
"""Return an event message."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return {"id": iden, "type": "event", "event": event}
|