Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace Avahi with ZeroConf for cross platform;add fake LED for testing #27

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
49 changes: 49 additions & 0 deletions circuitmatter/utility/mdns/zeroconf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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 = {key: value for key, value in txt_records.items()}
main_info = ServiceInfo(
f"{service_type}.{protocol}.local.",
f"{service_type}.{protocol}.local.",
addresses=[socket.inet_aton("0.0.0.0")],
port=port,
properties=txt_records,
)
self.zeroconf.register_service(main_info)
self.service_infos[service_type + instance_name] = main_info

for subtype in subtypes:
sub_info = ServiceInfo(
f"{subtype}.local.",
f"{subtype}.local.",
addresses=[socket.inet_aton("0.0.0.0")],
port=port,
properties=txt_records,
)
self.service_infos[subtype] = sub_info
self.zeroconf.register_service(sub_info)

def __del__(self):
for service_info in self.service_infos.values():
self.zeroconf.unregister_service(service_info)
self.zeroconf.close()
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
32 changes: 32 additions & 0 deletions 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):
super().__init__(name)
self._name = name
self.state = False

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

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


matter = cm.CircuitMatter()
led1 = LED("led1")
matter.add_device(led1)
led2 = LED("led2")
matter.add_device(led2)
while True:
matter.process_packets()
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
Loading