craftbeerpi4-pione/cbpi/http_endpoints/http_sensor.py

206 lines
5.3 KiB
Python
Raw Permalink Normal View History

2019-01-05 22:44:54 +01:00
from aiohttp import web
2021-01-22 23:25:20 +01:00
from cbpi.api import *
auth = False
2019-01-05 22:44:54 +01:00
2021-01-22 23:25:20 +01:00
class SensorHttpEndpoints():
2019-01-02 21:20:44 +01:00
def __init__(self, cbpi):
2021-01-22 23:25:20 +01:00
self.cbpi = cbpi
2019-01-02 21:20:44 +01:00
self.controller = cbpi.sensor
self.cbpi.register(self, "/sensor")
@request_mapping(path="/", auth_required=False)
async def http_get_all(self, request):
"""
---
2021-01-22 23:25:20 +01:00
description: Switch actor on
2019-01-02 21:20:44 +01:00
tags:
- Sensor
responses:
"204":
description: successful operation
"""
2021-01-22 23:25:20 +01:00
return web.json_response(data=self.controller.get_state())
2019-01-02 21:20:44 +01:00
@request_mapping(path="/", method="POST", auth_required=False)
async def http_add(self, request):
"""
---
2021-01-22 23:25:20 +01:00
description: add one Actor
2019-01-02 21:20:44 +01:00
tags:
- Sensor
parameters:
- in: body
name: body
2021-01-22 23:25:20 +01:00
description: Created an actor
required: true
2019-01-02 21:20:44 +01:00
schema:
type: object
2021-01-22 23:25:20 +01:00
2019-01-02 21:20:44 +01:00
properties:
name:
type: string
type:
type: string
2021-01-22 23:25:20 +01:00
props:
2019-01-02 21:20:44 +01:00
type: object
2021-01-22 23:25:20 +01:00
example:
name: "Actor 1"
type: "CustomActor"
props: {}
2019-01-02 21:20:44 +01:00
responses:
"204":
description: successful operation
"""
2021-01-22 23:25:20 +01:00
data = await request.json()
response_data = await self.controller.add(data)
return web.json_response(data=self.controller.create_dict(response_data))
2019-01-02 21:20:44 +01:00
@request_mapping(path="/{id}", method="PUT", auth_required=False)
async def http_update(self, request):
"""
---
2021-01-22 23:25:20 +01:00
description: Update an actor
2019-01-02 21:20:44 +01:00
tags:
- Sensor
parameters:
- name: "id"
in: "path"
2021-01-22 23:25:20 +01:00
description: "Actor ID"
2019-01-02 21:20:44 +01:00
required: true
type: "integer"
format: "int64"
- in: body
name: body
2021-01-22 23:25:20 +01:00
description: Update an actor
2019-01-02 21:20:44 +01:00
required: false
schema:
type: object
properties:
name:
type: string
type:
type: string
config:
2021-01-22 23:25:20 +01:00
props: object
2019-01-02 21:20:44 +01:00
responses:
"200":
description: successful operation
"""
2021-01-22 23:25:20 +01:00
id = request.match_info['id']
data = await request.json()
return web.json_response(data=self.controller.create_dict(await self.controller.update(id, data)))
2019-01-02 21:20:44 +01:00
@request_mapping(path="/{id}", method="DELETE", auth_required=False)
async def http_delete_one(self, request):
"""
---
2021-01-22 23:25:20 +01:00
description: Delete an actor
2019-01-02 21:20:44 +01:00
tags:
- Sensor
parameters:
- name: "id"
in: "path"
2021-01-22 23:25:20 +01:00
description: "Actor ID"
2019-01-02 21:20:44 +01:00
required: true
2021-01-22 23:25:20 +01:00
type: "string"
2019-01-02 21:20:44 +01:00
responses:
"204":
description: successful operation
"""
2021-01-22 23:25:20 +01:00
id = request.match_info['id']
await self.controller.delete(id)
return web.Response(status=204)
2019-01-05 22:44:54 +01:00
2021-01-22 23:25:20 +01:00
@request_mapping(path="/{id}/on", method="POST", auth_required=False)
async def http_on(self, request) -> web.Response:
"""
2019-01-05 22:44:54 +01:00
2021-01-22 23:25:20 +01:00
---
description: Switch actor on
tags:
- Sensor
parameters:
- name: "id"
in: "path"
description: "Actor ID"
required: true
type: "string"
responses:
"204":
description: successful operation
"405":
description: invalid HTTP Met
"""
id = request.match_info['id']
await self.controller.on(id)
return web.Response(status=204)
2019-01-28 22:21:31 +01:00
2021-01-22 23:25:20 +01:00
@request_mapping(path="/{id}/off", method="POST", auth_required=False)
async def http_off(self, request) -> web.Response:
"""
2019-01-05 22:44:54 +01:00
2021-01-22 23:25:20 +01:00
---
description: Switch actor on
tags:
- Sensor
parameters:
- name: "id"
in: "path"
description: "Actor ID"
required: true
type: "string"
responses:
"204":
description: successful operation
"405":
description: invalid HTTP Met
"""
id = request.match_info['id']
await self.controller.off(id)
2019-01-28 22:21:31 +01:00
return web.Response(status=204)
2021-01-22 23:25:20 +01:00
2019-01-28 22:21:31 +01:00
2021-01-22 23:25:20 +01:00
@request_mapping(path="/{id}/action", method="POST", auth_required=auth)
2019-01-28 22:21:31 +01:00
async def http_action(self, request) -> web.Response:
"""
---
2021-01-22 23:25:20 +01:00
description: Toogle an actor on or off
2019-01-28 22:21:31 +01:00
tags:
2019-08-16 21:36:55 +02:00
- Sensor
2019-01-28 22:21:31 +01:00
parameters:
- name: "id"
in: "path"
description: "Actor ID"
required: true
type: "integer"
format: "int64"
- in: body
name: body
description: Update an actor
required: false
schema:
type: object
properties:
name:
type: string
2021-01-22 23:25:20 +01:00
parameter:
2019-01-28 22:21:31 +01:00
type: object
responses:
"204":
description: successful operation
"""
2021-01-22 23:25:20 +01:00
actor_id = request.match_info['id']
data = await request.json()
await self.controller.call_action(actor_id, data.get("name"), data.get("parameter"))
2019-01-28 22:21:31 +01:00
2019-01-05 22:44:54 +01:00
return web.Response(status=204)