craftbeerpi4-pione/cbpi/http_endpoints/http_actor.py

286 lines
7.5 KiB
Python
Raw Normal View History

2021-02-16 20:37:51 +01:00
from cbpi.api.dataclasses import Actor, Props
2019-01-02 21:20:44 +01:00
from aiohttp import web
2019-01-05 20:43:48 +01:00
from cbpi.api import *
2019-01-02 21:20:44 +01:00
auth = False
2021-01-22 23:25:20 +01:00
class ActorHttpEndpoints():
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.actor
self.cbpi.register(self, "/actor")
@request_mapping(path="/", auth_required=False)
async def http_get_all(self, request):
"""
---
description: Switch actor on
tags:
- Actor
responses:
"204":
description: successful operation
"""
2021-01-22 23:25:20 +01:00
return web.json_response(data=self.controller.get_state())
2022-08-31 20:46:27 +02:00
@request_mapping(path="/ws_update", auth_required=False)
async def http_get_all(self, request):
"""
---
description: Update WS actors
tags:
- Actor
responses:
"204":
description: successful operation
"""
return web.json_response(data=await self.controller.ws_actor_update())
@request_mapping(path="/{id:\w+}", auth_required=False)
async def http_get_one(self, request):
"""
---
description: Get one Actor
tags:
- Actor
parameters:
- name: "id"
in: "path"
description: "Actor ID"
required: true
type: "integer"
format: "int64"
responses:
"200":
description: successful operation
"404":
description: Actor not found
"""
actor = self.controller.find_by_id(request.match_info['id'])
if (actor is None):
return web.json_response(status=404)
return web.json_response(data=actor.to_dict(), status=200)
2021-01-22 23:25:20 +01:00
2019-01-02 21:20:44 +01:00
@request_mapping(path="/", method="POST", auth_required=False)
async def http_add(self, request):
"""
---
2019-01-14 07:33:59 +01:00
description: add one Actor
2019-01-02 21:20:44 +01:00
tags:
- Actor
parameters:
- in: body
name: body
description: Created an actor
2019-07-31 07:58:54 +02:00
required: true
2021-01-22 23:25:20 +01:00
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()
2021-02-16 20:37:51 +01:00
actor = Actor(name=data.get("name"), props=Props(data.get("props", {})), type=data.get("type"))
response_data = await self.controller.add(actor)
2019-01-02 21:20:44 +01:00
2021-02-16 20:37:51 +01:00
return web.json_response(data=response_data.to_dict())
2021-01-22 23:25:20 +01:00
2019-01-02 21:20:44 +01:00
@request_mapping(path="/{id}", method="PUT", auth_required=False)
async def http_update(self, request):
"""
---
description: Update an actor
tags:
- Actor
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
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()
2021-02-16 20:37:51 +01:00
actor = Actor(id=id, name=data.get("name"), props=Props(data.get("props", {})), type=data.get("type"))
return web.json_response(data=(await self.controller.update(actor)).to_dict())
2021-01-22 23:25:20 +01:00
2019-01-02 21:20:44 +01:00
@request_mapping(path="/{id}", method="DELETE", auth_required=False)
async def http_delete_one(self, request):
"""
---
description: Delete an actor
tags:
- Actor
parameters:
- name: "id"
in: "path"
description: "Actor ID"
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-02 21:20:44 +01:00
2021-01-22 23:25:20 +01:00
@request_mapping(path="/{id}/on", method="POST", auth_required=False)
2019-01-02 21:20:44 +01:00
async def http_on(self, request) -> web.Response:
"""
---
description: Switch actor on
tags:
- Actor
parameters:
- name: "id"
in: "path"
description: "Actor ID"
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
"405":
description: invalid HTTP Met
"""
2021-01-22 23:25:20 +01:00
id = request.match_info['id']
await self.controller.on(id)
2019-01-02 21:20:44 +01:00
return web.Response(status=204)
2021-01-22 23:25:20 +01:00
@request_mapping(path="/{id}/off", method="POST", auth_required=False)
2019-01-02 21:20:44 +01:00
async def http_off(self, request) -> web.Response:
"""
---
2021-01-22 23:25:20 +01:00
description: Switch actor on
2019-01-02 21:20:44 +01:00
tags:
- Actor
parameters:
- name: "id"
in: "path"
description: "Actor ID"
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
"405":
description: invalid HTTP Met
"""
2021-01-22 23:25:20 +01:00
id = request.match_info['id']
await self.controller.off(id)
2019-01-04 09:29:09 +01:00
return web.Response(status=204)
2021-01-22 23:25:20 +01:00
2019-01-04 09:29:09 +01:00
2021-01-22 23:25:20 +01:00
@request_mapping(path="/{id}/action", method="POST", auth_required=auth)
2019-01-04 09:29:09 +01:00
async def http_action(self, request) -> web.Response:
"""
---
description: Toogle an actor on or off
tags:
- Actor
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-04 09:29:09 +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()
2023-01-22 16:37:10 +01:00
#print(data)
2021-02-27 20:09:19 +01:00
await self.controller.call_action(actor_id, data.get("action"), data.get("parameter"))
2019-01-04 09:29:09 +01:00
2023-03-04 15:03:21 +01:00
return web.Response(status=204)
@request_mapping(path="/{id}/set_power", method="POST", auth_required=auth)
async def http_set_power(self, request) -> web.Response:
"""
---
description: Set actor power
tags:
- Actor
parameters:
- name: "id"
in: "path"
description: "Actor ID"
required: true
type: "integer"
format: "int64"
- in: body
name: body
description: Set Power
required: true
schema:
type: object
properties:
temp:
type: integer
responses:
"204":
description: successful operation
"""
id = request.match_info['id']
data = await request.json()
await self.controller.set_power(id,data.get("power"))
2019-01-02 21:20:44 +01:00
return web.Response(status=204)