Skip to content

Commit 0110963

Browse files
authored
Merge pull request #13 from chrivers/chrivers/group-filters
Add support for group_filter option on z2m server blocks
2 parents 986fc1a + 89c6089 commit 0110963

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

doc/config-reference.md

+10
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ z2m:
5757
url: ws://10.00.0.100:8080
5858
other-server:
5959
url: ws://10.10.0.102:8080
60+
61+
# Group prefix [optional!]
62+
#
63+
# If you specify this parameter, *only* groups with this prefix
64+
# will be visible from this z2m server. The prefix will be removed.
65+
#
66+
# So with a group_prefix of "bifrost_", the group "bifrost_kitchen"
67+
# will be available as "kitchen", but the group "living_room" will
68+
# be hidden instead.
69+
group_prefix: bifrost_
6070
...
6171

6272
# Rooms section [optional!]

src/config.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub struct Z2mConfig {
3434
#[derive(Clone, Debug, Serialize, Deserialize)]
3535
pub struct Z2mServer {
3636
pub url: String,
37+
pub group_prefix: Option<String>,
3738
}
3839

3940
#[derive(Clone, Debug, Serialize, Deserialize, Default)]

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ async fn build_tasks(appstate: AppState) -> ApiResult<JoinSet<ApiResult<()>>> {
8585
for (name, server) in &appstate.config().z2m.servers {
8686
let client = z2m::Client::new(
8787
name.clone(),
88-
server.url.clone(),
88+
server.clone(),
8989
appstate.config(),
9090
appstate.res.clone(),
9191
)?;

src/z2m/mod.rs

+24-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use tokio::time::sleep;
1616
use tokio_tungstenite::{connect_async, tungstenite, MaybeTlsStream, WebSocketStream};
1717
use uuid::Uuid;
1818

19-
use crate::config::AppConfig;
19+
use crate::config::{AppConfig, Z2mServer};
2020
use crate::hue;
2121
use crate::hue::api::{
2222
Button, ButtonData, ButtonMetadata, ButtonReport, ColorTemperature, ColorTemperatureUpdate,
@@ -43,7 +43,7 @@ struct LearnScene {
4343

4444
pub struct Client {
4545
name: String,
46-
conn: String,
46+
server: Z2mServer,
4747
config: Arc<AppConfig>,
4848
state: Arc<Mutex<Resources>>,
4949
map: HashMap<String, Uuid>,
@@ -55,7 +55,7 @@ pub struct Client {
5555
impl Client {
5656
pub fn new(
5757
name: String,
58-
conn: String,
58+
server: Z2mServer,
5959
config: Arc<AppConfig>,
6060
state: Arc<Mutex<Resources>>,
6161
) -> ApiResult<Self> {
@@ -65,7 +65,7 @@ impl Client {
6565
let ignore = HashSet::new();
6666
Ok(Self {
6767
name,
68-
conn,
68+
server,
6969
config,
7070
state,
7171
map,
@@ -163,6 +163,23 @@ impl Client {
163163
}
164164

165165
pub async fn add_group(&mut self, grp: &crate::z2m::api::Group) -> ApiResult<()> {
166+
let room_name;
167+
168+
if let Some(ref prefix) = self.server.group_prefix {
169+
if !grp.friendly_name.starts_with(prefix) {
170+
log::debug!(
171+
"[{}] Ignoring room outside our prefix: {}",
172+
self.name,
173+
grp.friendly_name
174+
);
175+
return Ok(());
176+
} else {
177+
room_name = grp.friendly_name.strip_prefix(prefix).unwrap()
178+
}
179+
} else {
180+
room_name = &grp.friendly_name;
181+
}
182+
166183
let link_room = RType::Room.deterministic(&grp.friendly_name);
167184
let link_glight = RType::GroupedLight.deterministic((link_room.rid, grp.id));
168185

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

237-
let mut metadata = RoomMetadata::new(RoomArchetype::Home, &topic);
254+
let mut metadata = RoomMetadata::new(RoomArchetype::Home, room_name);
238255
if let Some(room_conf) = self.config.rooms.get(&topic) {
239256
if let Some(name) = &room_conf.name {
240257
metadata.name = name.to_string();
@@ -623,8 +640,8 @@ impl Client {
623640
pub async fn run_forever(mut self) -> ApiResult<()> {
624641
let mut chan = self.state.lock().await.z2m_channel();
625642
loop {
626-
log::info!("[{}] Connecting to {}", self.name, self.conn);
627-
match connect_async(&self.conn).await {
643+
log::info!("[{}] Connecting to {}", self.name, self.server.url);
644+
match connect_async(&self.server.url).await {
628645
Ok((socket, _)) => {
629646
let res = self.event_loop(&mut chan, socket).await;
630647
log::error!("[{}] Event loop broke: {res:?}", self.name);

0 commit comments

Comments
 (0)