This is an early version of a local control and data extraction for the Sonic water shut of valve. I have previously written a python library for the web API and a Home Assistant integration for the same device.
This library will exclusively act as a websocket client that connects directly to the device and thus will be local only
It will have less control of device and account settings (than the web api library) and will focus on the ability to calculate and view the volume of water used in a period of time, thus will be ideally setup to supply information to the Home Assistant energy dashboard.
The water consumption information is not currently available in the web API.
It will also have device status information (battery levels, signal strength etc.), further it will have the ability to open and close the valve.
The device publishes a telemetry message approximately every 1 minute when there is no flow, and approx every second when there is water flowing.
This message contains the flow rate, water temperature, ambient_temp, the absolute water pressure, the battery level, the leak_status, device status and the probe time.
I have initially structured the library with the following classes.
-
SonicDevice
: This class represents the Sonic device and contains methods to interact with it. It has methods likerequest_state
,request_telemetry
,change_state
, andcalculate_daily_volume
. -
WebSocketClient
: This class handle the WebSocket connection to the Sonic device. It has methods likeconnect
,disconnect
,send
, andreceive
. -
Message
: This class handles json messages to be sent or received over the WebSocket connection. It has the methodsto_json
andfrom_json
. -
main
: An instance ofSonicDevice
is created, we connect to it, receive a stream of volume usage, Also able to perform operations like open and close the valve.
TO USE
To test. copy the config_demo.ini file and rename to config.ini and update the values with your device details.- You need to enable the
Local mode status
in theSignal Settings
within theSettings
in theSonic
App to get the device's local server username/password. - If the
Local mode status
is disabled and re-enabled, the username/password will change. - Then from the terminal run
python -m sonic_local --ip YOURIPADDRESS -u YOURUSERNAME -p YOURPASSWORD --c XXXXXX
where XXXXXX is eitheropen
,closed
,requestState
orrequestTelemetry
. - for help with the command line arguments run
python -m sonic_local --help
TODO
add method for storing/caching daily volume in case program crasheshandle the concurrency of receiving/returning volumes and also daily tasksusing asyncio.gather task to run concurrently- Handle functions like opening, closing valve alongside the telemetry data stream
- Can I automatically identify the devices ip on my local network? or is this something that will be better suited to the Home Assistant integration?
remove port number from config.ini and set default as 443 in code.DoneHandle crashes and reconnection to websocket better, device can become unreachable if clean disconnect is not performed.Working quite well nowAddimprove logging (print statements to logger)- Add tests
- average pressure? or max pressure?
- handle different timezones
- remove old code in sonic_device.py
- handle delay for valve to open and close then read state to confirm success
- rewrite readme to reflect current state of project (moving volume calculation to HA integration)
remove configparser from main.py as it has been replaced with argparse- ...