pub struct Device { /* private fields */ }
Expand description
Represents a device.
For the details of fields, please refer to the devices section of the API documentation.
Implementations§
Source§impl Device
impl Device
Sourcepub fn device_name(&self) -> &str
pub fn device_name(&self) -> &str
The device name. This is the name configured in the SwitchBot app.
Sourcepub fn device_type(&self) -> &str
pub fn device_type(&self) -> &str
The device type. This is empty if this is an infrared remote device.
Sourcepub fn remote_type(&self) -> &str
pub fn remote_type(&self) -> &str
The device type for an infrared remote device.
Sourcepub fn hub_device_id(&self) -> &str
pub fn hub_device_id(&self) -> &str
The parent Hub ID.
Sourcepub async fn command(&self, command: &CommandRequest) -> Result<()>
pub async fn command(&self, command: &CommandRequest) -> Result<()>
Send the command
to the SwitchBot API.
Please also see the CommandRequest
.
§Examples
let command = CommandRequest { command: "turnOn".into(), ..Default::default() };
device.command(&command).await?;
Sourcepub async fn update_status(&self) -> Result<()>
pub async fn update_status(&self) -> Result<()>
Get the device status from the SwitchBot API.
Please see Device::status_by_key()
and some other functions
to retrieve the status captured by this function.
Sourcepub fn status_by_key(&self, key: &str) -> Option<Value>
pub fn status_by_key(&self, key: &str) -> Option<Value>
Get the value of a key from the device status.
The Device::update_status()
must be called prior to this function.
§Examples
device.update_status().await?;
println!("Power = {}", device.status_by_key("power").unwrap());
Sourcepub fn eval_condition(&self, condition: &str) -> Result<bool>
pub fn eval_condition(&self, condition: &str) -> Result<bool>
Evaluate a conditional expression.
Following operators are supported.
key
,key=true
, andkey=false
for boolean types.=
,<
,<=
,>
, and>=
for numeric types.=
for string and other types.
Returns an error if the expression is invalid,
or if the key
does not exist.
Please also see the switchbot-cli
documentation about the
“if-command”.
The Device::update_status()
must be called prior to this function.
§Examples
device.update_status().await?;
println!("Power-on = {}", device.eval_condition("power=on")?);
Sourcepub fn write_status_to(&self, writer: impl Write) -> Result<()>
pub fn write_status_to(&self, writer: impl Write) -> Result<()>
Write the list of the device status to the writer
.
The Device::update_status()
must be called prior to this function.
§Examples
device.update_status().await?;
device.write_status_to(std::io::stdout());
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Device
impl<'de> Deserialize<'de> for Device
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl !Freeze for Device
impl !RefUnwindSafe for Device
impl Send for Device
impl Sync for Device
impl Unpin for Device
impl !UnwindSafe for Device
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string
, but without panic on OOM.