Source code for caspia.gateway.services.button
from caspia.gateway.services import GatewayService
from caspia.meadow.client import characteristic_read_handler
from caspia.meadow.services import ButtonBase
from caspia.node import components
[docs]class Button(GatewayService, ButtonBase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.button_c = None # The linked Button Component
[docs] def configure(self):
self.button_c = self.network.get_configured_component(self.get_node(), components.Button,
self.config['location'])
@property
def dependant_components(self):
return {self.button_c}
[docs] async def on_component_event(self, component, event):
if self.button_c == component:
if isinstance(event, components.Button.PushEvent):
await self.notify(self.push, None)
await self.notify(self.is_pushed, True)
elif isinstance(event, components.Button.ReleaseEvent):
await self.notify(self.release, None)
await self.notify(self.is_pushed, False)
elif isinstance(event, components.Button.ButtonEvent):
if event.event == components.Button.ButtonEvent.EVENT_CLICK:
await self.notify(self.event, 'click')
elif event.event == components.Button.ButtonEvent.EVENT_HOLD:
await self.notify(self.event, 'hold', extra={'interval': event.interval})
[docs] @characteristic_read_handler('is_pushed')
async def is_pushed_read(self, **kwargs):
if self.button_c.state is None or self.button_c.state.is_pushed is None:
return False
return self.button_c.state.is_pushed