Struct hass_rs::client::HassClient
source · pub struct HassClient {
pub subscriptions: HashMap<u64, String>,
/* private fields */
}
Expand description
HassClient is a library that is meant to simplify the conversation with HomeAssistant Web Socket Server it provides a number of convenient functions that creates the requests and read the messages from server
Fields§
§subscriptions: HashMap<u64, String>
Implementations§
source§impl HassClient
impl HassClient
pub fn new( tx: Sender<TungsteniteMessage>, rx: Receiver<Result<TungsteniteMessage, Error>> ) -> Self
sourcepub async fn auth_with_longlivedtoken(&mut self, token: &str) -> HassResult<()>
pub async fn auth_with_longlivedtoken(&mut self, token: &str) -> HassResult<()>
authenticate the session using a long-lived access token
When a client connects to the server, the server sends out auth_required. The first message from the client should be an auth message. You can authorize with an access token. If the client supplies valid authentication, the authentication phase will complete by the server sending the auth_ok message. If the data is incorrect, the server will reply with auth_invalid message and disconnect the session.
sourcepub async fn ping(&mut self) -> HassResult<String>
pub async fn ping(&mut self) -> HassResult<String>
The API supports receiving a ping from the client and returning a pong. This serves as a heartbeat to ensure the connection is still alive.
sourcepub async fn get_config(&mut self) -> HassResult<HassConfig>
pub async fn get_config(&mut self) -> HassResult<HassConfig>
This will get the current config of the Home Assistant.
The server will respond with a result message containing the config.
sourcepub async fn get_states(&mut self) -> HassResult<Vec<HassEntity>>
pub async fn get_states(&mut self) -> HassResult<Vec<HassEntity>>
This will get all the current states from Home Assistant.
The server will respond with a result message containing the states.
sourcepub async fn get_services(&mut self) -> HassResult<HassServices>
pub async fn get_services(&mut self) -> HassResult<HassServices>
This will get all the services from Home Assistant.
The server will respond with a result message containing the services.
sourcepub async fn get_panels(&mut self) -> HassResult<HassPanels>
pub async fn get_panels(&mut self) -> HassResult<HassPanels>
This will get all the registered panels from Home Assistant.
The server will respond with a result message containing the current registered panels.
sourcepub async fn call_service(
&mut self,
domain: String,
service: String,
service_data: Option<Value>
) -> HassResult<String>
pub async fn call_service( &mut self, domain: String, service: String, service_data: Option<Value> ) -> HassResult<String>
This will call a service in Home Assistant. Right now there is no return value. The client can listen to state_changed events if it is interested in changed entities as a result of a service call.
The server will indicate with a message indicating that the service is done executing.
https://developers.home-assistant.io/docs/api/websocket#calling-a-service
additional info : https://developers.home-assistant.io/docs/api/rest ==> Post /api/services/
sourcepub async fn subscribe_event(
&mut self,
event_name: &str
) -> HassResult<WSResult>
pub async fn subscribe_event( &mut self, event_name: &str ) -> HassResult<WSResult>
The command subscribe_event will subscribe your client to the event bus.
You can either listen to all events or to a specific event type. If you want to listen to multiple event types, you will have to send multiple subscribe_events commands. The server will respond with a result message to indicate that the subscription is active. For each event that matches, the server will send a message of type event. The id in the message will point at the original id of the listen_event command.
sourcepub async fn unsubscribe_event(
&mut self,
subscription_id: u64
) -> HassResult<String>
pub async fn unsubscribe_event( &mut self, subscription_id: u64 ) -> HassResult<String>
The command unsubscribe_event will unsubscribe your client from the event bus.
You can unsubscribe from previously created subscription events. Pass the id of the original subscription command as value to the subscription field.