Add s7 demo subcommand: live server exposing real host metrics#704
Merged
Conversation
A runnable, demoable S7 PLC backed by your own machine. CPU %, memory %, disk/network throughput, temperature and fan RPM write into well-known DB1 offsets every couple of seconds; a writable DB2 block lets clients (e.g. the ha-s7 Home Assistant integration) flip a lamp, set a brightness, push a text message, and the demo prints each write with a timestamp. Optional rich live display shows current sensor values and scrolling write history in a full-screen dashboard. Runs as `s7 demo --port 10102` after `pip install "python-snap7[cli,demo]"`. psutil lives behind a new `demo` extra so the core install stays thin. DB layout is documented in the module docstring (and mirrored in constants) so users can copy-paste PLC4X tag addresses straight into a Home Assistant config. Platform notes: CPU temperature and fan speed report 0 where psutil has no sensor backend (macOS, most containers) — intentional, not a bug. Tests exercise the collector, sensor encoder, threshold BOOL derivation, and control-write diffing without spinning up the server; end-to-end server coverage already lives in test_s7_unified and would duplicate work here. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Move rich and click into the demo extras so `pip install "python-snap7[demo]"` actually gets you a working demo — the previous split forced users to install [cli,demo] to unlock the rich live dashboard, which nobody would have guessed. rich is still listed under cli too; pip dedupes and it keeps the cli extras independent for anyone who wants the CLI without the demo. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A runnable, demoable S7 PLC backed by your own machine. Real CPU %, memory %, disk/network throughput, temperature and fan RPM write into well-known DB1 offsets every couple of seconds; a writable DB2 block lets clients flip a lamp, set a brightness, push a text message, and the demo prints each write with a timestamp.
Optional
richlive display shows a full-screen dashboard of current sensor values plus a scrolling write history. Falls back to plain periodic logging whenrichisn't installed.Why
Up to now there was no way to see python-snap7 do anything without wiring up a real PLC or writing test-harness code. This gives:
localhost:10102, watch CPU % change.ha-s7Home Assistant integration: CPU/temp sensors, lamp switch, brightness slider — all the entity classes on one device.DB layout
Documented in the module docstring (and mirrored in constants) so users can copy-paste PLC4X tag addresses straight into a Home Assistant config.
Platform notes
psutillives behind a newdemoextra so the core install stays thin.Test plan
MetricCollector,_encode_sensors, threshold BOOL derivation, andControlWatcher. 5 new tests, all pass.snap7.Client, read DB1 → got live values (CPU 34.3%, MEM 55.6%). Wrote DB2 → demo logged[WRITE] lamp_on = ON,brightness = 200.pytest.importorskip).🤖 Generated with Claude Code