Skip to content

Commit

Permalink
Added an app to watch the traps.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Sep 14, 2024
1 parent 4e2553c commit c0fa540
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
26 changes: 26 additions & 0 deletions jaraco/home/Trap Watcher.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>
<key>Label</key>
<string>com.jaraco.home.trap-watcher</string>
<key>ProgramArguments</key>
<array>
<string>{sys.executable}</string>
<string>-m</string>
<string>jaraco.home.trap-watcher</string>
</array>
<key>StartInterval</key>
<integer>3600</integer>
<key>SessionCreate</key>
<false/>
<key>StandardErrorPath</key>
<string>{logs}/trap-watcher.err</string>
<key>StandardOutPath</key>
<string>{logs}/trap-watcher.out</string>
</dict>

</plist>
Binary file added jaraco/home/mouse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions jaraco/home/trap-watcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import asyncio
import pathlib
import subprocess
import sys

import keyring
import rumps
import typer
import victor_smart_kill as vsk

from . import contact
from .compat.py38 import resources


app = typer.Typer()


async def check_traps():
username = contact.load().email
password = keyring.get_password('https://www.victorpest.com', username)
async with vsk.VictorAsyncClient(username, password) as client:
api = vsk.VictorApi(client)
traps = await api.get_traps()
return any(trap.status for trap in traps)


class TrapWatch:
def __init__(self):
self.app = rumps.App("Traps", icon=self.icon)
rumps.Timer(self.check_traps, 3600).start()
self.app.run()

def check_traps(self, timer):
if asyncio.run(check_traps()):
rumps.notification(
"Smart Traps",
subtitle=None,
message="Check the traps.",
img=self.icon,
)

@property
def icon(self):
return str(resources.files() / 'mouse.png')


@app.command()
def install():
name = 'Trap Watcher.plist'
agents = pathlib.Path('~/Library/LaunchAgents').expanduser()
target = agents / name
tmpl = resources.files().joinpath(name).read_text()
logs = pathlib.Path(sys.executable).parent.parent / 'logs'
source = tmpl.format(sys=sys, logs=logs)
target.write_text(source)
subprocess.check_output(['launchctl', 'load', target])


@app.callback(invoke_without_command=True)
def main(ctx: typer.Context, update: bool = False):
if ctx.invoked_subcommand:
return
TrapWatch()


__name__ == '__main__' and app()
1 change: 1 addition & 0 deletions newsfragments/+be161cbc.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added an app to watch the traps.
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ dependencies = [
'importlib_resources; python_version < "3.9"',
"jaraco.compat>=4.1",
"tempora>=5.7",
"rumps",
"typer",
]
dynamic = ["version"]

Expand Down

0 comments on commit c0fa540

Please sign in to comment.