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

source

pub fn new( tx: Sender<TungsteniteMessage>, rx: Receiver<Result<TungsteniteMessage, Error>> ) -> Self

source

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.

source

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.

source

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.

source

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.

source

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.

source

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.

source

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//

source

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.

source

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.

Trait Implementations§

source§

impl Debug for HassClient

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V