autocamlight 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dries007
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: autocamlight
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Automate your webcam light.
|
|
5
|
+
Author: Dries007
|
|
6
|
+
Author-email: Dries007 <admin@dries007.net>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Requires-Dist: ha-mqtt-discoverable>=0.22.1
|
|
10
|
+
Requires-Dist: inotify>=0.2.12
|
|
11
|
+
Requires-Dist: pydantic-settings>=2.11.0
|
|
12
|
+
Requires-Python: >=3.11
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
|
|
15
|
+
# AutoCamLight
|
|
16
|
+
|
|
17
|
+
╔═══╗ ╖ ╔═══╕ ╖ ╖ ╖
|
|
18
|
+
║ ║ ║ ║ ║ ° ║ ║
|
|
19
|
+
╠═══╣╖ ╓╠═╛╔══╗║ ╒══╗╔═╦═╗║ ╖╔══╗╠══╗╠═╛
|
|
20
|
+
║ ║║ ║║ ║ ║║ ╔══╣║ ║ ║║ ║║ ║║ ║║
|
|
21
|
+
╜ ╙╚══╝╚═╛╚══╝╚═══╛╚══╝╜ ╙ ╙╚═══╛╙╚══╣╜ ╙╚═╛
|
|
22
|
+
╓ ║
|
|
23
|
+
AutoCamLight ╚══╝
|
|
24
|
+
|
|
25
|
+
[](https://opensource.org/licenses/MIT)
|
|
26
|
+
[](https://badge.fury.io/py/autocamlight)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
A small linux utility to automate your webcam light via Home Assistant.
|
|
30
|
+
|
|
31
|
+
Monitors /dev/video* and sends a state update to HA via MQTT when it detects that video is being captured.
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
Install, configure and run in the background.
|
|
36
|
+
|
|
37
|
+
After the first time the webcam is used, the device & entity will be created in Home Assistant, so you can set up your automations.
|
|
38
|
+
|
|
39
|
+
### Install
|
|
40
|
+
|
|
41
|
+
Use of [uv](https://docs.astral.sh/uv/) is highly recommended:
|
|
42
|
+
|
|
43
|
+
```shell
|
|
44
|
+
uv tool install autocamlight
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Configure
|
|
48
|
+
|
|
49
|
+
Create a `autocamlight.toml` file in one of the following locations:
|
|
50
|
+
|
|
51
|
+
1. $XDG_CONFIG_HOME (or ~/.config if unset), or
|
|
52
|
+
2. $XDG_CONFIG_DIRS (or /etc/xdg if unset), read as a list of folder split by :, as per the XDG specification, or
|
|
53
|
+
3. the current working directory.
|
|
54
|
+
|
|
55
|
+
Only a single toml file is loaded.
|
|
56
|
+
|
|
57
|
+
Example configuration:
|
|
58
|
+
|
|
59
|
+
```toml
|
|
60
|
+
log_level = "DEBUG"
|
|
61
|
+
|
|
62
|
+
[mqtt]
|
|
63
|
+
host = "homeassistant.local"
|
|
64
|
+
username = "mqtt"
|
|
65
|
+
password = "Correct Horse Battery Staple"
|
|
66
|
+
|
|
67
|
+
[entity]
|
|
68
|
+
name = "Webcam"
|
|
69
|
+
unique_id = "fe4bd497-7c2f-4f35-899f-f147f9f84738-capturing"
|
|
70
|
+
|
|
71
|
+
[entity.device]
|
|
72
|
+
name = "Webcam"
|
|
73
|
+
identifiers = "fe4bd497-7c2f-4f35-899f-f147f9f84738"
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
The file structure is dictated mostly by [ha_mqtt_discoverable](https://github.com/unixorn/ha-mqtt-discoverable).
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
### Auto-run on login
|
|
80
|
+
|
|
81
|
+
Create a new (user) unit file:
|
|
82
|
+
|
|
83
|
+
```shell
|
|
84
|
+
systemctl --user --force --full edit autocamlight
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Add the following to the file, assuming uv is installed in /usr/bin:
|
|
88
|
+
|
|
89
|
+
```ini
|
|
90
|
+
[Unit]
|
|
91
|
+
Description=AutoCamLight
|
|
92
|
+
After=multi-user.target
|
|
93
|
+
|
|
94
|
+
[Service]
|
|
95
|
+
Type=simple
|
|
96
|
+
ExecStart=/usr/bin/uv tool run autocamlight
|
|
97
|
+
Restart=on-failure
|
|
98
|
+
RestartSec=5
|
|
99
|
+
Environment=PYTHONUNBUFFERED=1
|
|
100
|
+
|
|
101
|
+
[Install]
|
|
102
|
+
WantedBy=default.target
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
Now enable & start the unit:
|
|
106
|
+
|
|
107
|
+
```shell
|
|
108
|
+
systemctl --user enable autocamlight
|
|
109
|
+
systemctl --user start autocamlight
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
You can monitor the service status with:
|
|
113
|
+
|
|
114
|
+
```bash
|
|
115
|
+
systemctl --user status autocamlight
|
|
116
|
+
journalctl --user -u autocamlight -f
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Test
|
|
120
|
+
|
|
121
|
+
Open any program that uses the webcam, e.g. Cheese.
|
|
122
|
+
|
|
123
|
+
If you have the logs open, an INFO message should appear.
|
|
124
|
+
The entity should be created in Home Assistant.
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# AutoCamLight
|
|
2
|
+
|
|
3
|
+
╔═══╗ ╖ ╔═══╕ ╖ ╖ ╖
|
|
4
|
+
║ ║ ║ ║ ║ ° ║ ║
|
|
5
|
+
╠═══╣╖ ╓╠═╛╔══╗║ ╒══╗╔═╦═╗║ ╖╔══╗╠══╗╠═╛
|
|
6
|
+
║ ║║ ║║ ║ ║║ ╔══╣║ ║ ║║ ║║ ║║ ║║
|
|
7
|
+
╜ ╙╚══╝╚═╛╚══╝╚═══╛╚══╝╜ ╙ ╙╚═══╛╙╚══╣╜ ╙╚═╛
|
|
8
|
+
╓ ║
|
|
9
|
+
AutoCamLight ╚══╝
|
|
10
|
+
|
|
11
|
+
[](https://opensource.org/licenses/MIT)
|
|
12
|
+
[](https://badge.fury.io/py/autocamlight)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
A small linux utility to automate your webcam light via Home Assistant.
|
|
16
|
+
|
|
17
|
+
Monitors /dev/video* and sends a state update to HA via MQTT when it detects that video is being captured.
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
Install, configure and run in the background.
|
|
22
|
+
|
|
23
|
+
After the first time the webcam is used, the device & entity will be created in Home Assistant, so you can set up your automations.
|
|
24
|
+
|
|
25
|
+
### Install
|
|
26
|
+
|
|
27
|
+
Use of [uv](https://docs.astral.sh/uv/) is highly recommended:
|
|
28
|
+
|
|
29
|
+
```shell
|
|
30
|
+
uv tool install autocamlight
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Configure
|
|
34
|
+
|
|
35
|
+
Create a `autocamlight.toml` file in one of the following locations:
|
|
36
|
+
|
|
37
|
+
1. $XDG_CONFIG_HOME (or ~/.config if unset), or
|
|
38
|
+
2. $XDG_CONFIG_DIRS (or /etc/xdg if unset), read as a list of folder split by :, as per the XDG specification, or
|
|
39
|
+
3. the current working directory.
|
|
40
|
+
|
|
41
|
+
Only a single toml file is loaded.
|
|
42
|
+
|
|
43
|
+
Example configuration:
|
|
44
|
+
|
|
45
|
+
```toml
|
|
46
|
+
log_level = "DEBUG"
|
|
47
|
+
|
|
48
|
+
[mqtt]
|
|
49
|
+
host = "homeassistant.local"
|
|
50
|
+
username = "mqtt"
|
|
51
|
+
password = "Correct Horse Battery Staple"
|
|
52
|
+
|
|
53
|
+
[entity]
|
|
54
|
+
name = "Webcam"
|
|
55
|
+
unique_id = "fe4bd497-7c2f-4f35-899f-f147f9f84738-capturing"
|
|
56
|
+
|
|
57
|
+
[entity.device]
|
|
58
|
+
name = "Webcam"
|
|
59
|
+
identifiers = "fe4bd497-7c2f-4f35-899f-f147f9f84738"
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The file structure is dictated mostly by [ha_mqtt_discoverable](https://github.com/unixorn/ha-mqtt-discoverable).
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
### Auto-run on login
|
|
66
|
+
|
|
67
|
+
Create a new (user) unit file:
|
|
68
|
+
|
|
69
|
+
```shell
|
|
70
|
+
systemctl --user --force --full edit autocamlight
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Add the following to the file, assuming uv is installed in /usr/bin:
|
|
74
|
+
|
|
75
|
+
```ini
|
|
76
|
+
[Unit]
|
|
77
|
+
Description=AutoCamLight
|
|
78
|
+
After=multi-user.target
|
|
79
|
+
|
|
80
|
+
[Service]
|
|
81
|
+
Type=simple
|
|
82
|
+
ExecStart=/usr/bin/uv tool run autocamlight
|
|
83
|
+
Restart=on-failure
|
|
84
|
+
RestartSec=5
|
|
85
|
+
Environment=PYTHONUNBUFFERED=1
|
|
86
|
+
|
|
87
|
+
[Install]
|
|
88
|
+
WantedBy=default.target
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Now enable & start the unit:
|
|
92
|
+
|
|
93
|
+
```shell
|
|
94
|
+
systemctl --user enable autocamlight
|
|
95
|
+
systemctl --user start autocamlight
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
You can monitor the service status with:
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
systemctl --user status autocamlight
|
|
102
|
+
journalctl --user -u autocamlight -f
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Test
|
|
106
|
+
|
|
107
|
+
Open any program that uses the webcam, e.g. Cheese.
|
|
108
|
+
|
|
109
|
+
If you have the logs open, an INFO message should appear.
|
|
110
|
+
The entity should be created in Home Assistant.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["uv_build>=0.8.15,<0.9.0"]
|
|
3
|
+
build-backend = "uv_build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "autocamlight"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
readme = "README.md"
|
|
9
|
+
description = "Automate your webcam light."
|
|
10
|
+
authors = [
|
|
11
|
+
{name = "Dries007", email = "admin@dries007.net"},
|
|
12
|
+
]
|
|
13
|
+
license = "MIT"
|
|
14
|
+
license-files = ["LICENSE"]
|
|
15
|
+
requires-python = ">=3.11"
|
|
16
|
+
dependencies = [
|
|
17
|
+
"ha-mqtt-discoverable>=0.22.1",
|
|
18
|
+
"inotify>=0.2.12",
|
|
19
|
+
"pydantic-settings>=2.11.0",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.scripts]
|
|
23
|
+
autocamlight = "autocamlight.__main__:main"
|
|
24
|
+
|
|
25
|
+
[dependency-groups]
|
|
26
|
+
dev = [
|
|
27
|
+
"pre-commit>=4.3.0",
|
|
28
|
+
"ruff>=0.14.0",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
# ___ __ __
|
|
32
|
+
# | _ \_ _ / _|/ _|
|
|
33
|
+
# | / || | _| _|
|
|
34
|
+
# |_|_\\_,_|_| |_|
|
|
35
|
+
[tool.ruff]
|
|
36
|
+
line-length = 140
|
|
37
|
+
fix = true
|
|
38
|
+
|
|
39
|
+
[tool.ruff.lint]
|
|
40
|
+
# Add extra checks to default. See https://docs.astral.sh/ruff/rules/
|
|
41
|
+
extend-select = [
|
|
42
|
+
"F", # pyflakes
|
|
43
|
+
"E", # pycodestyle errors
|
|
44
|
+
"W", # pycodestyle warnings
|
|
45
|
+
"I", # isort
|
|
46
|
+
"B", # flake8-bugbear
|
|
47
|
+
"G", # flake8-logging
|
|
48
|
+
"UP", # pyupgrade
|
|
49
|
+
"PT", # flake8-pytest-style
|
|
50
|
+
"SIM", # flake8-simplify
|
|
51
|
+
"ISC", # flake8-implicit-str-concat
|
|
52
|
+
"PIE", # flake8-pie
|
|
53
|
+
"FLY", # flynt
|
|
54
|
+
"PERF", # perflint
|
|
55
|
+
"FURB", # refurb
|
|
56
|
+
"RUF", # ruff
|
|
57
|
+
]
|
|
58
|
+
ignore = [
|
|
59
|
+
"E501", # line-too-long, ignored because the formatter has some leeway this rule doesn't understand
|
|
60
|
+
]
|
|
61
|
+
|
|
62
|
+
[tool.ruff.format]
|
|
63
|
+
docstring-code-format = true
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os
|
|
3
|
+
import signal
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
from threading import Timer
|
|
6
|
+
from typing import Literal
|
|
7
|
+
|
|
8
|
+
from ha_mqtt_discoverable import Settings
|
|
9
|
+
from ha_mqtt_discoverable.sensors import BinarySensor, BinarySensorInfo
|
|
10
|
+
from inotify.adapters import Inotify
|
|
11
|
+
from inotify.constants import IN_CLOSE, IN_OPEN
|
|
12
|
+
from pydantic import ValidationError
|
|
13
|
+
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource, SettingsConfigDict, TomlConfigSettingsSource
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
LOGO = r'''
|
|
17
|
+
╔═══╗ ╖ ╔═══╕ ╖ ╖ ╖
|
|
18
|
+
║ ║ ║ ║ ║ ° ║ ║
|
|
19
|
+
╠═══╣╖ ╓╠═╛╔══╗║ ╒══╗╔═╦═╗║ ╖╔══╗╠══╗╠═╛
|
|
20
|
+
║ ║║ ║║ ║ ║║ ╔══╣║ ║ ║║ ║║ ║║ ║║
|
|
21
|
+
╜ ╙╚══╝╚═╛╚══╝╚═══╛╚══╝╜ ╙ ╙╚═══╛╙╚══╣╜ ╙╚═╛
|
|
22
|
+
╓ ║
|
|
23
|
+
AutoCamLight ╚══╝
|
|
24
|
+
'''
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def possible_config_files():
|
|
28
|
+
for folder in (
|
|
29
|
+
os.environ.get("XDG_CONFIG_HOME", os.environ["HOME"] + "/.config"),
|
|
30
|
+
*os.environ.get("XDG_CONFIG_DIRS", "/etc/xdg").split(':'),
|
|
31
|
+
'.'
|
|
32
|
+
):
|
|
33
|
+
yield Path(folder, "autocamlight.toml").resolve()
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class Config(BaseSettings):
|
|
37
|
+
model_config = SettingsConfigDict(toml_file=list(possible_config_files()))
|
|
38
|
+
|
|
39
|
+
log_level: Literal["CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG"] = "INFO"
|
|
40
|
+
|
|
41
|
+
mqtt: Settings.MQTT
|
|
42
|
+
entity: BinarySensorInfo
|
|
43
|
+
|
|
44
|
+
@classmethod
|
|
45
|
+
def settings_customise_sources(cls, settings_cls: type[BaseSettings], *args, **kwargs) -> tuple[PydanticBaseSettingsSource, ...]:
|
|
46
|
+
return (TomlConfigSettingsSource(settings_cls),)
|
|
47
|
+
|
|
48
|
+
@classmethod
|
|
49
|
+
def load(cls):
|
|
50
|
+
try:
|
|
51
|
+
# noinspection PyArgumentList
|
|
52
|
+
return cls()
|
|
53
|
+
except ValidationError as e:
|
|
54
|
+
print("Tried to load these files:", ', '.join(map(os.fspath, cls.model_config.get("toml_file"))))
|
|
55
|
+
print("Could not load config, please check your config file. Pydantic error:")
|
|
56
|
+
print(e)
|
|
57
|
+
exit(1)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class AutoCamLight:
|
|
62
|
+
def __init__(self, cfg: Config):
|
|
63
|
+
self._state = None
|
|
64
|
+
self._cfg = cfg
|
|
65
|
+
self.sensor = BinarySensor(Settings(mqtt=cfg.mqtt, entity=cfg.entity, manual_availability=True))
|
|
66
|
+
|
|
67
|
+
def run(self):
|
|
68
|
+
try:
|
|
69
|
+
self.sensor.set_availability(True)
|
|
70
|
+
inotify = Inotify()
|
|
71
|
+
|
|
72
|
+
inotify.add_watch('/dev', mask=IN_OPEN | IN_CLOSE)
|
|
73
|
+
|
|
74
|
+
debounce = Timer(0, lambda: None)
|
|
75
|
+
|
|
76
|
+
for header, _, _, filename in inotify.event_gen(yield_nones=False):
|
|
77
|
+
if not filename.startswith('video'):
|
|
78
|
+
continue
|
|
79
|
+
|
|
80
|
+
debounce.cancel()
|
|
81
|
+
debounce = Timer(0.1, self.set_state, (bool(header.mask & IN_OPEN), ))
|
|
82
|
+
debounce.start()
|
|
83
|
+
except KeyboardInterrupt:
|
|
84
|
+
pass
|
|
85
|
+
finally:
|
|
86
|
+
logger = logging.getLogger("autocamlight")
|
|
87
|
+
logger.info("Exiting...")
|
|
88
|
+
# noinspection PyBroadException
|
|
89
|
+
try:
|
|
90
|
+
self.sensor.set_availability(False)
|
|
91
|
+
except Exception:
|
|
92
|
+
logger.exception("Failed to set availability")
|
|
93
|
+
# noinspection PyBroadException
|
|
94
|
+
try:
|
|
95
|
+
self.sensor.mqtt_client.disconnect()
|
|
96
|
+
except Exception:
|
|
97
|
+
logger.exception("Failed to disconnect MQTT client")
|
|
98
|
+
pass
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def set_state(self, state: bool):
|
|
102
|
+
if state == self._state:
|
|
103
|
+
return
|
|
104
|
+
self._state = state
|
|
105
|
+
|
|
106
|
+
self.sensor.update_state(state)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def main():
|
|
110
|
+
signal.signal(signal.SIGTERM, lambda signal, frame: exit(0))
|
|
111
|
+
|
|
112
|
+
cfg = Config.load()
|
|
113
|
+
|
|
114
|
+
logging.basicConfig(level=cfg.log_level, format="%(asctime)s %(levelname)5s [%(name)s] %(message)s")
|
|
115
|
+
logger = logging.getLogger("autocamlight")
|
|
116
|
+
logger.info(LOGO)
|
|
117
|
+
logger.info("Loaded config:\n%s", cfg.model_dump_json(indent=2, exclude_defaults=True))
|
|
118
|
+
|
|
119
|
+
acl = AutoCamLight(cfg)
|
|
120
|
+
acl.run()
|