pub struct Bulb { /* private fields */ }
Expand description
Bulb connection
Implementations§
source§impl Bulb
impl Bulb
sourcepub async fn connect(addr: &str, port: u16) -> Result<Self, Box<dyn Error>>
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();
sourcepub fn attach(stream: TcpStream) -> Result<Self, Box<dyn Error>>
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();
sourcepub fn attach_tokio(stream: TcpStream) -> Self
pub fn attach_tokio(stream: TcpStream) -> Self
Same as attach(stream: std::net::TcpStream)
but for tokio::net::TcpStream
;
sourcepub fn no_response(self) -> Self
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`
sourcepub fn get_response(self) -> Self
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
sourcepub async fn get_notify(&mut self) -> Receiver<Notification>
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
sourcepub async fn set_notify(&mut self, chan: Sender<Notification>)
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
sourcepub async fn start_music(&mut self, host: &str) -> Result<Self, Box<dyn Error>>
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
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"),
}
sourcepub async fn get_prop(
&mut self,
properties: &Properties
) -> Result<Option<Response>, BulbError>
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.
sourcepub async fn set_power(
&mut self,
power: Power,
effect: Effect,
duration: Duration,
mode: Mode
) -> Result<Option<Response>, BulbError>
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 ifeffect
isSudden
)mode
: Mode in which the lamp will turn on (Mode::Normal
to keep the current mode)
sourcepub async fn bg_set_power(
&mut self,
power: Power,
effect: Effect,
duration: Duration,
mode: Mode
) -> Result<Option<Response>, BulbError>
pub async fn bg_set_power( &mut self, power: Power, effect: Effect, duration: Duration, mode: Mode ) -> Result<Option<Response>, BulbError>
pub async fn on( &mut self, _cron_type: CronType ) -> Result<Option<Response>, BulbError>
pub async fn off( &mut self, _cron_type: CronType ) -> Result<Option<Response>, BulbError>
pub async fn bg_on( &mut self, _cron_type: CronType ) -> Result<Option<Response>, BulbError>
pub async fn bg_off( &mut self, _cron_type: CronType ) -> Result<Option<Response>, BulbError>
sourcepub async fn toggle(&mut self) -> Result<Option<Response>, BulbError>
pub async fn toggle(&mut self) -> Result<Option<Response>, BulbError>
Flip the main light power state
sourcepub async fn bg_toggle(&mut self) -> Result<Option<Response>, BulbError>
pub async fn bg_toggle(&mut self) -> Result<Option<Response>, BulbError>
Flip the background light power state
sourcepub async fn dev_toggle(&mut self) -> Result<Option<Response>, BulbError>
pub async fn dev_toggle(&mut self) -> Result<Option<Response>, BulbError>
Flip the both the main light and the background light power state
sourcepub async fn set_ct_abx(
&mut self,
ct_value: u16,
effect: Effect,
duration: Duration
) -> Result<Option<Response>, BulbError>
pub async fn set_ct_abx( &mut self, ct_value: u16, effect: Effect, duration: Duration ) -> Result<Option<Response>, BulbError>
Set light color temperature
sourcepub async fn bg_set_ct_abx(
&mut self,
ct_value: u16,
effect: Effect,
duration: Duration
) -> Result<Option<Response>, BulbError>
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
pub async fn set_rgb( &mut self, rgb_value: u32, effect: Effect, duration: Duration ) -> Result<Option<Response>, BulbError>
pub async fn bg_set_rgb( &mut self, rgb_value: u32, effect: Effect, duration: Duration ) -> Result<Option<Response>, BulbError>
pub async fn set_hsv( &mut self, hue: u16, sat: u8, effect: Effect, duration: Duration ) -> Result<Option<Response>, BulbError>
pub async fn bg_set_hsv( &mut self, hue: u16, sat: u8, effect: Effect, duration: Duration ) -> Result<Option<Response>, BulbError>
pub async fn set_bright( &mut self, brightness: u8, effect: Effect, duration: Duration ) -> Result<Option<Response>, BulbError>
pub async fn bg_set_bright( &mut self, brightness: u8, effect: Effect, duration: Duration ) -> Result<Option<Response>, BulbError>
pub async fn set_scene( &mut self, class: Class, val1: u64, val2: u64, val3: u64 ) -> Result<Option<Response>, BulbError>
pub async fn bg_set_scene( &mut self, class: Class, val1: u64, val2: u64, val3: u64 ) -> Result<Option<Response>, BulbError>
pub async fn start_cf( &mut self, count: u8, action: CfAction, flow_expression: FlowExpresion ) -> Result<Option<Response>, BulbError>
pub async fn bg_start_cf( &mut self, count: u8, action: CfAction, flow_expression: FlowExpresion ) -> Result<Option<Response>, BulbError>
pub async fn stop_cf(&mut self) -> Result<Option<Response>, BulbError>
pub async fn bg_stop_cf(&mut self) -> Result<Option<Response>, BulbError>
sourcepub async fn set_adjust(
&mut self,
action: AdjustAction,
prop: Prop
) -> Result<Option<Response>, BulbError>
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
-
action: The direction of the adjustment (AdjustAction::Increase, AdjustAction::Decrease)
-
prop: The property to adjust (Prop::Bright, Prop::CT, Prop::Color)
NOTES: When prop
is Prop::Color, the action
can only be
AdjustAction::Circle, otherwise the request will be invalid.
sourcepub async fn bg_set_adjust(
&mut self,
action: AdjustAction,
prop: Prop
) -> Result<Option<Response>, BulbError>
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
pub async fn adjust_bright( &mut self, percentage: i8, duration: Duration ) -> Result<Option<Response>, BulbError>
pub async fn bg_adjust_bright( &mut self, percentage: i8, duration: Duration ) -> Result<Option<Response>, BulbError>
pub async fn adjust_ct( &mut self, percentage: i8, duration: Duration ) -> Result<Option<Response>, BulbError>
pub async fn bg_adjust_ct( &mut self, percentage: i8, duration: Duration ) -> Result<Option<Response>, BulbError>
pub async fn adjust_color( &mut self, percentage: i8, duration: Duration ) -> Result<Option<Response>, BulbError>
pub async fn bg_adjust_color( &mut self, percentage: i8, duration: Duration ) -> Result<Option<Response>, BulbError>
sourcepub async fn set_default(&mut self) -> Result<Option<Response>, BulbError>
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.
sourcepub async fn bg_set_default(&mut self) -> Result<Option<Response>, BulbError>
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
sourcepub async fn set_name(
&mut self,
name: &str
) -> Result<Option<Response>, BulbError>
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.
sourcepub async fn set_music(
&mut self,
action: MusicAction,
host: &str,
port: u16
) -> Result<Option<Response>, BulbError>
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.
sourcepub async fn cron_add(
&mut self,
cron_type: CronType,
value: u64
) -> Result<Option<Response>, BulbError>
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.