Skip to content

Commit cde6fe8

Browse files
committed
refactor entity setup
Make it a bit easier to add different kinds of entities, and generate entities for the various on_off and toggles reported from the platform API. Note however that the platform API doesn't return any useful state for anything other than the light color and power state at the time of writing, so this is a bit meh.
1 parent af181de commit cde6fe8

File tree

3 files changed

+324
-100
lines changed

3 files changed

+324
-100
lines changed

src/platform_api.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,22 +281,31 @@ impl GoveeApiClient {
281281
anyhow::bail!("Scene '{scene}' is not available for this device");
282282
}
283283

284-
pub async fn set_power_state(
284+
pub async fn set_toggle_state(
285285
&self,
286286
device: &HttpDeviceInfo,
287+
instance: &str,
287288
on: bool,
288289
) -> anyhow::Result<ControlDeviceResponseCapability> {
289290
let cap = device
290-
.capability_by_instance("powerSwitch")
291-
.ok_or_else(|| anyhow::anyhow!("device has no powerSwitch"))?;
291+
.capability_by_instance(instance)
292+
.ok_or_else(|| anyhow::anyhow!("device has no {instance}"))?;
292293

293294
let value = cap
294295
.enum_parameter_by_name(if on { "on" } else { "off" })
295-
.ok_or_else(|| anyhow::anyhow!("powerSwitch has no on/off!?"))?;
296+
.ok_or_else(|| anyhow::anyhow!("{instance} has no on/off!?"))?;
296297

297298
self.control_device(&device, &cap, value).await
298299
}
299300

301+
pub async fn set_power_state(
302+
&self,
303+
device: &HttpDeviceInfo,
304+
on: bool,
305+
) -> anyhow::Result<ControlDeviceResponseCapability> {
306+
self.set_toggle_state(device, "powerSwitch", on).await
307+
}
308+
300309
pub async fn set_brightness(
301310
&self,
302311
device: &HttpDeviceInfo,
@@ -511,7 +520,7 @@ impl HttpDeviceInfo {
511520
}
512521
}
513522

514-
#[derive(Deserialize, Serialize, Debug, Default, Clone, Copy)]
523+
#[derive(Deserialize, Serialize, Debug, Default, Clone, Copy, PartialEq, Eq)]
515524
pub enum DeviceType {
516525
#[serde(rename = "devices.types.light")]
517526
#[default]
@@ -538,7 +547,7 @@ pub enum DeviceType {
538547
Other,
539548
}
540549

541-
#[derive(Deserialize, Serialize, Debug, Clone, Copy)]
550+
#[derive(Deserialize, Serialize, Debug, Clone, Copy, PartialEq, Eq)]
542551
pub enum DeviceCapabilityKind {
543552
#[serde(rename = "devices.capabilities.on_off")]
544553
OnOff,

0 commit comments

Comments
 (0)