Lines n/a 0 / 0
Functions and Methods n/a 0 / 0
Classes and Traits n/a 0 / 0
Name Lines Functions and Methods CRAP Classes and Traits
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Contracts;
6
7use Stewart\Contracts\Action\ActionResponse;
8use Stewart\Contracts\Action\ActionTargetSource;
9use Stewart\Contracts\Connection\ConnectionEvent;
10use Stewart\Contracts\Entity\Entity;
11use Stewart\Contracts\Entity\EntityId;
12use Stewart\Contracts\Event\HaEvent;
13use Stewart\Contracts\Exception\ActionCallException;
14use Stewart\Contracts\Exception\EventFireException;
15use Stewart\Contracts\Exception\HistoryException;
16use Stewart\Contracts\Exception\IdentifierException;
17use Stewart\Contracts\Exception\SelectorException;
18use Stewart\Contracts\Exception\StateException;
19use Stewart\Contracts\Exception\TopicException;
20use Stewart\Contracts\Exception\TriggerException;
21use Stewart\Contracts\Exception\WaitException;
22use Stewart\Contracts\History\EntityStateHistory;
23use Stewart\Contracts\History\HistoryQuery;
24use Stewart\Contracts\Registry\EntityFilter;
25use Stewart\Contracts\Registry\Registry;
26use Stewart\Contracts\Selector\Collection\SelectorCollection;
27use Stewart\Contracts\Selector\Selector;
28use Stewart\Contracts\State\Collection\EntityStateCollection;
29use Stewart\Contracts\State\EntityState;
30use Stewart\Contracts\State\EventContext;
31use Stewart\Contracts\State\StateChange;
32use Stewart\Contracts\Time\Duration;
33use Stewart\Contracts\Topic\TopicEvent;
34use Stewart\Contracts\Trigger\Collection\HaTriggerCollection;
35use Stewart\Contracts\Trigger\HaTrigger;
36use Stewart\Contracts\Trigger\TriggerEvent;
37
38interface HaContext
39{
40    /** @throws IdentifierException */
41    public function getState(EntityId|string $entityId): ?EntityState;
42
43    /** @throws IdentifierException */
44    public function getEntity(EntityId|string $entityId): Entity;
45
46    /** @throws StateException|IdentifierException */
47    public function requireState(EntityId|string $entityId): EntityState;
48
49    // Null when the entity does not exist or has no last change, and hasHeldStateFor() is then false.
50    /** @throws IdentifierException */
51    public function getHeldStateDuration(EntityId|string $entityId): ?Duration;
52
53    /** @throws IdentifierException */
54    public function hasHeldStateFor(EntityId|string $entityId, Duration $duration): bool;
55
56    /** @throws HistoryException|IdentifierException */
57    public function getHistory(EntityId|string $entityId, HistoryQuery $query): EntityStateHistory;
58
59    public function listStates(string|EntityId|Selector|SelectorCollection|EntityFilter|null $selector = null): EntityStateCollection;
60
61    public function watchStateChanges(string|EntityId|Selector|SelectorCollection|EntityFilter $selector): StateChangeStream;
62
63    // Returns at once when the entity is already in $state, and null once the timeout passes.
64    /** @throws WaitException|IdentifierException */
65    public function waitForState(EntityId|string $entityId, string $state, ?Duration $timeout = null): ?EntityState;
66
67    public function getRegistry(): Registry;
68
69    /**
70     * @return EventStream<HaEvent>
71     * @throws StateException|SelectorException
72     */
73    public function watchEvents(string|Selector|SelectorCollection $eventType): EventStream;
74
75    /**
76     * @param HaTrigger|HaTriggerCollection|array<array-key, mixed> $trigger
77     * @param array<array-key, mixed> $variables
78     * @return EventStream<TriggerEvent>
79     * @throws TriggerException
80     */
81    public function watchTrigger(HaTrigger|HaTriggerCollection|array $trigger, array $variables = []): EventStream;
82
83    /**
84     * @param array<string, mixed> $data
85     * @throws ActionCallException
86     */
87    public function callAction(string $action, array $data = [], ?ActionTargetSource $target = null): EventContext;
88
89    /**
90     * @param array<string, mixed> $data
91     * @throws ActionCallException
92     */
93    public function callActionForResponse(string $action, array $data = [], ?ActionTargetSource $target = null): ActionResponse;
94
95    /**
96     * @param array<string, mixed> $data
97     * @throws EventFireException
98     */
99    public function fireEvent(string $eventType, array $data = []): EventContext;
100
101    public function wasCausedByStewart(StateChange|EntityState|HaEvent $subject): bool;
102
103    /**
104     * @param array<array-key, mixed>|scalar|null $payload
105     * @throws TopicException
106     */
107    public function publish(string $topic, bool|int|float|string|array|null $payload = null): void;
108
109    /** @return EventStream<TopicEvent> */
110    public function watchTopic(string|Selector|SelectorCollection $topic): EventStream;
111
112    /** @return EventStream<ConnectionEvent> */
113    public function watchConnection(): EventStream;
114
115    public function isConnected(): bool;
116}