Skip to content

Commit

Permalink
replase Avahi with ZeroConf for cross platform;add fake LED for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ihidchaos committed Oct 28, 2024
1 parent 734fd1d commit 1cdb70e
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 13 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ CircuitMatter is currently developed in CPython 3.12, the de facto implementatio

### Running on a Raspberry Pi SBC

CircuitMatter uses [avahi tools](https://avahi.org) to manage MDNS on Linux. It must therefore be installed for it to work properly.
```shell
sudo apt-get install avahi-utils
```

Now, install CircuitMatter:
Install CircuitMatter:

```shell
pip install circuitmatter
Expand Down Expand Up @@ -203,7 +198,7 @@ To run CircuitMatter against a live Matter commissioner run:
python examples/replay.py
```

This will start up MDNS via avahi for discovery by the commissioner and then reply to received UDP packets. CircuitMatter currently doesn't fully commission so it can't act as any specific type of device yet. When it can, there will be examples.
This will start up MDNS for discovery by the commissioner and then reply to received UDP packets. CircuitMatter currently doesn't fully commission so it can't act as any specific type of device yet. When it can, there will be examples.

## Running a Matter commissioner

Expand Down
4 changes: 2 additions & 2 deletions circuitmatter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def __init__(
self.socketpool = socketpool

if mdns_server is None:
from circuitmatter.utility.mdns.avahi import Avahi
from circuitmatter.utility.mdns.zeroconf import ZeroConf

mdns_server = Avahi()
mdns_server = ZeroConf()
self.mdns_server = mdns_server

if random_source is None:
Expand Down
47 changes: 47 additions & 0 deletions circuitmatter/utility/mdns/zeroconf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 Scott Shawcroft for Adafruit Industries
#
# SPDX-License-Identifier: MIT

import socket
from zeroconf import IPVersion, ServiceInfo, Zeroconf

class ZeroConf:
def __init__(self):
self.zeroconf = Zeroconf(ip_version=IPVersion.All)
self.service_infos = {}

def advertise_service(
self,
service_type,
protocol,
port,
txt_records={},
subtypes=[],
instance_name="",
):
txt_records = [f"{key}={value}" for key, value in txt_records.items()]
main_info = ServiceInfo(
f"{service_type}.{protocol}.local",
instance_name,
addresses=[socket.inet_aton("0.0.0.0")],
port=port,
properties=txt_records,
)

sub_info = ServiceInfo(
subtypes,
instance_name,
addresses=[socket.inet_aton("0.0.0.0")],
port=port,
properties=txt_records,
)

self.zeroconf.register_service(main_info)
self.zeroconf.register_service(sub_info)
self.service_infos[service_type + instance_name] = main_info
self.service_infos[subtypes + instance_name] = sub_info

def __del__(self):
for service_info in self.service_infos.values():
self.zeroconf.unregister_service(service_info)
self.zeroconf.close()
32 changes: 32 additions & 0 deletions examples/fake_onoff_led.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# SPDX-FileCopyrightText: Copyright (c) 2024 Scott Shawcroft for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""Simple fake LED on and off as a light."""

import circuitmatter as cm
from circuitmatter.device_types.lighting import on_off


class LED(on_off.OnOffLight):
def __init__(self, name, led):
super().__init__(name)
self._name = name
self._led = led

def on(self):
self._led.value = True
print("Led %s is On", self._name)

def off(self):
self._led.value = False
print("Led %s is Off", self._name)


matter = cm.CircuitMatter()
led = LED("led1")
matter.add_device(led)
led = LED("led2")
matter.add_device(led)
while True:
matter.process_packets()
4 changes: 2 additions & 2 deletions examples/replay.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from circuitmatter.device_types.lighting import on_off
from circuitmatter.utility import random
from circuitmatter.utility.mdns import DummyMDNS
from circuitmatter.utility.mdns.avahi import Avahi
from circuitmatter.utility.mdns.zeroconf import ZeroConf
from circuitmatter.utility.recording import RecordingRandom, RecordingSocketPool
from circuitmatter.utility.replay import ReplayRandom, ReplaySocketPool

Expand Down Expand Up @@ -51,7 +51,7 @@ def run(replay_file=None):
# No starting state.
record_file.write("none\n")
socketpool = RecordingSocketPool(record_file, socket)
mdns_server = Avahi()
mdns_server = ZeroConf()
random_source = RecordingRandom(record_file, random)

matter = cm.CircuitMatter(socketpool, mdns_server, random_source, device_state)
Expand Down
4 changes: 2 additions & 2 deletions examples/replay_rgb_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from circuitmatter.device_types.lighting import extended_color
from circuitmatter.utility import random
from circuitmatter.utility.mdns import DummyMDNS
from circuitmatter.utility.mdns.avahi import Avahi
from circuitmatter.utility.mdns.zeroconf import ZeroConf
from circuitmatter.utility.recording import RecordingRandom, RecordingSocketPool
from circuitmatter.utility.replay import ReplayRandom, ReplaySocketPool

Expand Down Expand Up @@ -73,7 +73,7 @@ def run(replay_file=None):
# No starting state.
record_file.write("none\n")
socketpool = RecordingSocketPool(record_file, socket)
mdns_server = Avahi()
mdns_server = ZeroConf()
random_source = RecordingRandom(record_file, random)

matter = cm.CircuitMatter(socketpool, mdns_server, random_source, device_state)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ requires = [
"setuptools",
"wheel",
"setuptools-scm",
"zeroconf",
]

[project]
Expand Down

0 comments on commit 1cdb70e

Please sign in to comment.