Skip to content

Commit

Permalink
Merge pull request #13 from chrivers/chrivers/group-filters
Browse files Browse the repository at this point in the history
Add support for group_filter option on z2m server blocks
  • Loading branch information
chrivers authored Aug 24, 2024
2 parents 986fc1a + 89c6089 commit 0110963
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
10 changes: 10 additions & 0 deletions doc/config-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ z2m:
url: ws://10.00.0.100:8080
other-server:
url: ws://10.10.0.102:8080

# Group prefix [optional!]
#
# If you specify this parameter, *only* groups with this prefix
# will be visible from this z2m server. The prefix will be removed.
#
# So with a group_prefix of "bifrost_", the group "bifrost_kitchen"
# will be available as "kitchen", but the group "living_room" will
# be hidden instead.
group_prefix: bifrost_
...

# Rooms section [optional!]
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct Z2mConfig {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Z2mServer {
pub url: String,
pub group_prefix: Option<String>,
}

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async fn build_tasks(appstate: AppState) -> ApiResult<JoinSet<ApiResult<()>>> {
for (name, server) in &appstate.config().z2m.servers {
let client = z2m::Client::new(
name.clone(),
server.url.clone(),
server.clone(),
appstate.config(),
appstate.res.clone(),
)?;
Expand Down
31 changes: 24 additions & 7 deletions src/z2m/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use tokio::time::sleep;
use tokio_tungstenite::{connect_async, tungstenite, MaybeTlsStream, WebSocketStream};
use uuid::Uuid;

use crate::config::AppConfig;
use crate::config::{AppConfig, Z2mServer};
use crate::hue;
use crate::hue::api::{
Button, ButtonData, ButtonMetadata, ButtonReport, ColorTemperature, ColorTemperatureUpdate,
Expand All @@ -43,7 +43,7 @@ struct LearnScene {

pub struct Client {
name: String,
conn: String,
server: Z2mServer,
config: Arc<AppConfig>,
state: Arc<Mutex<Resources>>,
map: HashMap<String, Uuid>,
Expand All @@ -55,7 +55,7 @@ pub struct Client {
impl Client {
pub fn new(
name: String,
conn: String,
server: Z2mServer,
config: Arc<AppConfig>,
state: Arc<Mutex<Resources>>,
) -> ApiResult<Self> {
Expand All @@ -65,7 +65,7 @@ impl Client {
let ignore = HashSet::new();
Ok(Self {
name,
conn,
server,
config,
state,
map,
Expand Down Expand Up @@ -163,6 +163,23 @@ impl Client {
}

pub async fn add_group(&mut self, grp: &crate::z2m::api::Group) -> ApiResult<()> {
let room_name;

if let Some(ref prefix) = self.server.group_prefix {
if !grp.friendly_name.starts_with(prefix) {
log::debug!(
"[{}] Ignoring room outside our prefix: {}",
self.name,
grp.friendly_name
);
return Ok(());
} else {
room_name = grp.friendly_name.strip_prefix(prefix).unwrap()
}
} else {
room_name = &grp.friendly_name;
}

let link_room = RType::Room.deterministic(&grp.friendly_name);
let link_glight = RType::GroupedLight.deterministic((link_room.rid, grp.id));

Expand Down Expand Up @@ -234,7 +251,7 @@ impl Client {
log::debug!("[{}] {link_room:?} is new, adding..", self.name);
}

let mut metadata = RoomMetadata::new(RoomArchetype::Home, &topic);
let mut metadata = RoomMetadata::new(RoomArchetype::Home, room_name);
if let Some(room_conf) = self.config.rooms.get(&topic) {
if let Some(name) = &room_conf.name {
metadata.name = name.to_string();
Expand Down Expand Up @@ -623,8 +640,8 @@ impl Client {
pub async fn run_forever(mut self) -> ApiResult<()> {
let mut chan = self.state.lock().await.z2m_channel();
loop {
log::info!("[{}] Connecting to {}", self.name, self.conn);
match connect_async(&self.conn).await {
log::info!("[{}] Connecting to {}", self.name, self.server.url);
match connect_async(&self.server.url).await {
Ok((socket, _)) => {
let res = self.event_loop(&mut chan, socket).await;
log::error!("[{}] Event loop broke: {res:?}", self.name);
Expand Down

0 comments on commit 0110963

Please sign in to comment.