1 """Camera platform that has a Raspberry Pi camera."""
3 from __future__
import annotations
9 from tempfile
import NamedTemporaryFile
23 CONF_OVERLAY_METADATA,
24 CONF_OVERLAY_TIMESTAMP,
30 _LOGGER = logging.getLogger(__name__)
34 """Kill any previously running raspistill process.."""
35 with subprocess.Popen(
36 [
"killall",
"raspistill"],
37 stdout=subprocess.DEVNULL,
38 stderr=subprocess.STDOUT,
47 add_entities: AddEntitiesCallback,
48 discovery_info: DiscoveryInfoType |
None =
None,
50 """Set up the Raspberry Camera."""
53 if discovery_info
is None:
56 if shutil.which(
"raspistill")
is None:
57 _LOGGER.error(
"'raspistill' was not found")
60 hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, kill_raspistill)
62 setup_config = hass.data[DOMAIN]
63 file_path = setup_config[CONF_FILE_PATH]
65 def delete_temp_file(*args):
66 """Delete the temporary file to prevent saving multiple temp images.
68 Only used when no path is defined
74 with NamedTemporaryFile(suffix=
".jpg", delete=
False)
as temp_file:
75 file_path = temp_file.name
76 setup_config[CONF_FILE_PATH] = file_path
77 hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, delete_temp_file)
80 elif not hass.config.is_allowed_path(file_path):
81 _LOGGER.error(
"'%s' is not a whitelisted directory", file_path)
88 """Representation of a Raspberry Pi camera."""
91 """Initialize Raspberry Pi camera component."""
94 self.
_name_name = device_info[CONF_NAME]
104 device_info[CONF_FILE_PATH],
108 str(device_info[CONF_IMAGE_WIDTH]),
110 str(device_info[CONF_IMAGE_HEIGHT]),
112 str(device_info[CONF_TIMELAPSE]),
114 str(device_info[CONF_IMAGE_QUALITY]),
116 str(device_info[CONF_IMAGE_ROTATION]),
118 if device_info[CONF_HORIZONTAL_FLIP]:
119 cmd_args.append(
"-hf")
121 if device_info[CONF_VERTICAL_FLIP]:
122 cmd_args.append(
"-vf")
124 if device_info[CONF_OVERLAY_METADATA]:
125 cmd_args.append(
"-a")
126 cmd_args.append(
str(device_info[CONF_OVERLAY_METADATA]))
128 if device_info[CONF_OVERLAY_TIMESTAMP]:
129 cmd_args.append(
"-a")
131 cmd_args.append(
"-a")
132 cmd_args.append(
str(device_info[CONF_OVERLAY_TIMESTAMP]))
140 stdout=subprocess.DEVNULL,
141 stderr=subprocess.STDOUT,
146 self, width: int |
None =
None, height: int |
None =
None
148 """Return raspistill image response."""
149 with open(self.
_config_config[CONF_FILE_PATH],
"rb")
as file:
154 """Return the name of this camera."""
155 return self.
_name_name
159 """Return the interval between frames of the stream."""
160 return self.
_config_config[CONF_TIMELAPSE] / 1000
def __init__(self, device_info)
bytes|None camera_image(self, int|None width=None, int|None height=None)
None add_entities(HomeAssistant hass, FreeboxRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None open(self, **Any kwargs)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
def kill_raspistill(*args)