Struct yeelight::Bulb

source ·
pub struct Bulb { /* private fields */ }
Expand description

Bulb connection

Implementations§

source§

impl Bulb

source

pub async fn connect(addr: &str, port: u16) -> Result<Self, Box<dyn Error>>

Connect to bulb at the specified address and port.

If port is 0, the default value (55443) is used.

Example
let my_bulb_ip = "192.168.1.204";
let mut bulb = Bulb::connect(my_bulb_ip, 55443).await
    .expect("Connection failed");
bulb.toggle().await.unwrap();
source

pub fn attach(stream: TcpStream) -> Result<Self, Box<dyn Error>>

Attach to existing std::net::TcpStream.

Example
let stream = std::net::TcpStream::connect("192.168.1.204:55443")
    .expect("Connection failed");
let mut bulb = Bulb::attach(stream).unwrap();
bulb.toggle().await.unwrap();
source

pub fn attach_tokio(stream: TcpStream) -> Self

Same as attach(stream: std::net::TcpStream) but for tokio::net::TcpStream;

source

pub fn no_response(self) -> Self

Set the Bulb connection so that it does not wait for response from the bulb

If this is used, all the methods will return None even if they fail. It is not recommended unless you know the bulb will not respond (Poor connection, music mode or firewall). However it is often better to wrap the calls in a tokio timeout.

Example
let my_bulb_ip = "192.168.1.204";
let mut bulb = Bulb::connect(my_bulb_ip, 55443).await
    .expect("Connection failed").no_response();
let response = bulb.toggle().await.unwrap(); // response will be `None`
source

pub fn get_response(self) -> Self

Set the Bulb connection so that it does wait for response from the bulb

This reverses the changes made with Bulb::no_response

source

pub async fn get_notify(&mut self) -> Receiver<Notification>

Get a new notification reciever from the Bulb

This method creates a new channel and replaces the old one.

NOTE: The channel has 10 message buffer. If more are needed manually create a mpsc::channel and use Bulb::set_notify

source

pub async fn set_notify(&mut self, chan: Sender<Notification>)

Attach the Bulb notification channel to the provided one

This replaces the current channel

See also: Bulb::get_notify

source

pub async fn start_music(&mut self, host: &str) -> Result<Self, Box<dyn Error>>

Establishes a Music mode connection with bulb.

This method returns another Bulb object to send commands to the bulb in music mode. Note that all commands send to the bulb get no response and produce no notification message, so there is no way to know if the command was executed successfully by the bulb.

source§

impl Bulb

Messages

This are all the methods as by the yeelight API spec. They all take the parameters specified by the spec, build a message, send it to the bulb and wait for the response which is parsed into a Response.

Example
let mut bulb = Bulb::connect("192.168.1.204", 0).await.expect("Connection failed");
let response = bulb.set_power(Power::On, Effect::Smooth, Duration::from_secs(1), Mode::Normal).await.unwrap();

match response {
    Some(vec) => {
        // In this case, the response should be ["ok"].
        for v in vec.iter() {
            println!("{}", v);
        }
    },
    None => eprintln!("This should not happen"),
}
source

pub async fn get_prop( &mut self, properties: &Properties ) -> Result<Option<Response>, BulbError>

Retrieve current propertes of smart LED.

Parameters:

  • properties: List of properties. The answer will follow the same order.
source

pub async fn set_power( &mut self, power: Power, effect: Effect, duration: Duration, mode: Mode ) -> Result<Option<Response>, BulbError>

Switch on or off the smart LED (software managed on/off).

Parameters:

  • power:
  • effect:
  • duration: total time of the gradual changing (minimum 30 milliseconds) (Ignored if effect is Sudden)
  • mode: Mode in which the lamp will turn on (Mode::Normal to keep the current mode)
source

pub async fn bg_set_power( &mut self, power: Power, effect: Effect, duration: Duration, mode: Mode ) -> Result<Option<Response>, BulbError>

source

pub async fn on( &mut self, _cron_type: CronType ) -> Result<Option<Response>, BulbError>

source

pub async fn off( &mut self, _cron_type: CronType ) -> Result<Option<Response>, BulbError>

source

pub async fn bg_on( &mut self, _cron_type: CronType ) -> Result<Option<Response>, BulbError>

source

pub async fn bg_off( &mut self, _cron_type: CronType ) -> Result<Option<Response>, BulbError>

source

pub async fn toggle(&mut self) -> Result<Option<Response>, BulbError>

Flip the main light power state

source

pub async fn bg_toggle(&mut self) -> Result<Option<Response>, BulbError>

Flip the background light power state

source

pub async fn dev_toggle(&mut self) -> Result<Option<Response>, BulbError>

Flip the both the main light and the background light power state

source

pub async fn set_ct_abx( &mut self, ct_value: u16, effect: Effect, duration: Duration ) -> Result<Option<Response>, BulbError>

Set light color temperature

source

pub async fn bg_set_ct_abx( &mut self, ct_value: u16, effect: Effect, duration: Duration ) -> Result<Option<Response>, BulbError>

Set background light color temperature

source

pub async fn set_rgb( &mut self, rgb_value: u32, effect: Effect, duration: Duration ) -> Result<Option<Response>, BulbError>

source

pub async fn bg_set_rgb( &mut self, rgb_value: u32, effect: Effect, duration: Duration ) -> Result<Option<Response>, BulbError>

source

pub async fn set_hsv( &mut self, hue: u16, sat: u8, effect: Effect, duration: Duration ) -> Result<Option<Response>, BulbError>

source

pub async fn bg_set_hsv( &mut self, hue: u16, sat: u8, effect: Effect, duration: Duration ) -> Result<Option<Response>, BulbError>

source

pub async fn set_bright( &mut self, brightness: u8, effect: Effect, duration: Duration ) -> Result<Option<Response>, BulbError>

source

pub async fn bg_set_bright( &mut self, brightness: u8, effect: Effect, duration: Duration ) -> Result<Option<Response>, BulbError>

source

pub async fn set_scene( &mut self, class: Class, val1: u64, val2: u64, val3: u64 ) -> Result<Option<Response>, BulbError>

source

pub async fn bg_set_scene( &mut self, class: Class, val1: u64, val2: u64, val3: u64 ) -> Result<Option<Response>, BulbError>

source

pub async fn start_cf( &mut self, count: u8, action: CfAction, flow_expression: FlowExpresion ) -> Result<Option<Response>, BulbError>

source

pub async fn bg_start_cf( &mut self, count: u8, action: CfAction, flow_expression: FlowExpresion ) -> Result<Option<Response>, BulbError>

source

pub async fn stop_cf(&mut self) -> Result<Option<Response>, BulbError>

source

pub async fn bg_stop_cf(&mut self) -> Result<Option<Response>, BulbError>

source

pub async fn set_adjust( &mut self, action: AdjustAction, prop: Prop ) -> Result<Option<Response>, BulbError>

Change brightness, CT or color of a smart LED without knowing the current value.

Parameters

NOTES: When prop is Prop::Color, the action can only be AdjustAction::Circle, otherwise the request will be invalid.

source

pub async fn bg_set_adjust( &mut self, action: AdjustAction, prop: Prop ) -> Result<Option<Response>, BulbError>

Change brightness, CT or color of a background smart LED without knowing the current value.

See: Bulb::set_adjust

source

pub async fn adjust_bright( &mut self, percentage: i8, duration: Duration ) -> Result<Option<Response>, BulbError>

source

pub async fn bg_adjust_bright( &mut self, percentage: i8, duration: Duration ) -> Result<Option<Response>, BulbError>

source

pub async fn adjust_ct( &mut self, percentage: i8, duration: Duration ) -> Result<Option<Response>, BulbError>

source

pub async fn bg_adjust_ct( &mut self, percentage: i8, duration: Duration ) -> Result<Option<Response>, BulbError>

source

pub async fn adjust_color( &mut self, percentage: i8, duration: Duration ) -> Result<Option<Response>, BulbError>

source

pub async fn bg_adjust_color( &mut self, percentage: i8, duration: Duration ) -> Result<Option<Response>, BulbError>

source

pub async fn set_default(&mut self) -> Result<Option<Response>, BulbError>

Save current state of smart LED in persistent memory.

If user powers off and then powers on the smart LED again (hard power reset), the smart LED will show last saved state.

NOTE: Only accepted if the smart LED is currently in “on” state.

source

pub async fn bg_set_default(&mut self) -> Result<Option<Response>, BulbError>

Save current state of the background smart LED in persistent memory.

See: Bulb::set_default

source

pub async fn set_name( &mut self, name: &str ) -> Result<Option<Response>, BulbError>

Set the device name.

The name will be stored on the device and reported in discovering response.

source

pub async fn set_music( &mut self, action: MusicAction, host: &str, port: u16 ) -> Result<Option<Response>, BulbError>

Start or stop music mode on a device.

Under music mode, no property will be reported and no message quota is checked.

source

pub async fn cron_add( &mut self, cron_type: CronType, value: u64 ) -> Result<Option<Response>, BulbError>

Start a timer job on the smart LED.

Currently there is only a timer type.

source

pub async fn cron_del( &mut self, cron_type: CronType ) -> Result<Option<Response>, BulbError>

Stop the current cron job

source

pub async fn cron_get( &mut self, _cron_type: CronType ) -> Result<Option<Response>, BulbError>

Get the settings of the current cron job.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Bulb

§

impl Send for Bulb

§

impl Sync for Bulb

§

impl Unpin for Bulb

§

impl !UnwindSafe for Bulb

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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, U> TryFrom<U> for Twhere 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 Twhere 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.