2018-08-22 07:59:22 +02:00
|
|
|
from typing import Any, Dict, Generator, Optional, Type
|
2015-02-18 01:45:39 +01:00
|
|
|
|
|
|
|
|
2017-08-30 00:07:54 +02:00
|
|
|
class ProjectorElement:
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
|
|
|
Base class for an element on the projector.
|
|
|
|
|
|
|
|
Every app which wants to add projector elements has to create classes
|
2015-06-17 09:45:00 +02:00
|
|
|
subclassing from this base class with different names. The name attribute
|
2017-08-30 00:07:54 +02:00
|
|
|
has to be set.
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2018-08-22 22:00:08 +02:00
|
|
|
name: Optional[str] = None
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2017-08-24 12:26:55 +02:00
|
|
|
def check_and_update_data(self, projector_object: Any, config_entry: Any) -> Any:
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2016-02-27 20:25:06 +01:00
|
|
|
Checks projector element data via self.check_data() and updates
|
|
|
|
them via self.update_data(). The projector object and the config
|
|
|
|
entry have to be given.
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
|
|
|
self.projector_object = projector_object
|
|
|
|
self.config_entry = config_entry
|
|
|
|
assert self.config_entry.get('name') == self.name, (
|
|
|
|
'To get data of a projector element, the correct config entry has to be given.')
|
2016-02-27 20:25:06 +01:00
|
|
|
self.check_data()
|
|
|
|
return self.update_data() or {}
|
2015-02-18 01:45:39 +01:00
|
|
|
|
2017-08-24 12:26:55 +02:00
|
|
|
def check_data(self) -> None:
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2016-02-27 20:25:06 +01:00
|
|
|
Method can be overridden to validate projector element data. This
|
|
|
|
may raise ProjectorException in case of an error.
|
|
|
|
|
|
|
|
Default: Does nothing.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2017-08-24 12:26:55 +02:00
|
|
|
def update_data(self) -> Dict[Any, Any]:
|
2015-02-18 01:45:39 +01:00
|
|
|
"""
|
2016-02-27 20:25:06 +01:00
|
|
|
Method can be overridden to update the projector element data
|
|
|
|
output. This should return a dictonary. Use this for server
|
|
|
|
calculated data which have to be forwared to the client.
|
|
|
|
|
|
|
|
Default: Does nothing.
|
|
|
|
"""
|
|
|
|
pass
|
2015-06-12 21:08:57 +02:00
|
|
|
|
2017-08-30 00:07:54 +02:00
|
|
|
|
2018-08-22 22:00:08 +02:00
|
|
|
projector_elements: Dict[str, ProjectorElement] = {}
|
2017-08-30 00:07:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
def register_projector_elements(elements: Generator[Type[ProjectorElement], None, None]) -> None:
|
|
|
|
"""
|
|
|
|
Registers projector elements for later use.
|
|
|
|
|
|
|
|
Has to be called in the app.ready method.
|
|
|
|
"""
|
|
|
|
for Element in elements:
|
|
|
|
element = Element()
|
2018-08-22 22:00:08 +02:00
|
|
|
projector_elements[element.name] = element # type: ignore
|
2017-08-30 00:07:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_all_projector_elements() -> Dict[str, ProjectorElement]:
|
|
|
|
"""
|
|
|
|
Returns all projector elements that where registered with
|
|
|
|
register_projector_elements()
|
|
|
|
"""
|
|
|
|
return projector_elements
|