33
33
from .camera_shared import CameraSharedManager
34
34
from .common import get_vacuum_unique_id_from_mqtt_topic
35
35
from .const import (
36
- ATTR_MARGINS ,
37
- ATTR_ROTATE ,
38
- ATTR_VACUUM_TOPIC ,
36
+ ATTR_FRIENDLY_NAME ,
39
37
ATTR_JSON_DATA ,
38
+ ATTR_ROTATE ,
40
39
ATTR_SNAPSHOT_PATH ,
41
- ATTR_FRIENDLY_NAME ,
40
+ ATTR_VACUUM_TOPIC ,
42
41
CAMERA_STORAGE ,
43
- CONF_ASPECT_RATIO ,
44
- CONF_AUTO_ZOOM ,
45
- CONF_OFFSET_BOTTOM ,
46
- CONF_OFFSET_LEFT ,
47
- CONF_OFFSET_RIGHT ,
48
- CONF_OFFSET_TOP ,
49
- CONF_SNAPSHOTS_ENABLE ,
50
- CONF_VAC_STAT ,
51
- CONF_VAC_STAT_FONT ,
52
- CONF_VAC_STAT_POS ,
53
- CONF_VAC_STAT_SIZE ,
54
42
CONF_VACUUM_CONNECTION_STRING ,
55
43
CONF_VACUUM_ENTITY_ID ,
56
44
CONF_VACUUM_IDENTIFIERS ,
57
- CONF_ZOOM_LOCK_RATIO ,
58
- DEFAULT_VALUES ,
59
45
DEFAULT_NAME ,
60
46
DOMAIN ,
61
47
NOT_STREAMING_STATES ,
@@ -128,15 +114,11 @@ def __init__(self, hass, device_info):
128
114
self ._directory_path = self .hass .config .path () # get Home Assistant path
129
115
self ._mqtt_listen_topic = str (device_info .get (CONF_VACUUM_CONNECTION_STRING ))
130
116
self ._shared , self ._file_name = self ._handle_init_shared_data (
131
- self ._mqtt_listen_topic
117
+ self ._mqtt_listen_topic ,
118
+ device_info ,
132
119
)
133
120
self ._start_up_logs ()
134
- self ._init_shared_data (device_info )
135
- self ._storage_path = f"{ self .hass .config .path (STORAGE_DIR )} /{ CAMERA_STORAGE } "
136
- if not os .path .exists (self ._storage_path ):
137
- self ._storage_path = f"{ self ._directory_path } /{ STORAGE_DIR } "
138
- self .snapshot_img = f"{ self ._storage_path } /{ self ._file_name } .png"
139
- self .log_file = f"{ self ._storage_path } /{ self ._file_name } .zip"
121
+ self ._storage_path , self .snapshot_img , self .log_file = self ._init_paths ()
140
122
self ._attr_unique_id = device_info .get (
141
123
CONF_UNIQUE_ID ,
142
124
get_vacuum_unique_id_from_mqtt_topic (self ._mqtt_listen_topic ),
@@ -153,9 +135,7 @@ def __init__(self, hass, device_info):
153
135
self ._attr_frame_interval = 6
154
136
self ._vac_json_available = None
155
137
self ._cpu_percent = None
156
- # If there is a log zip in www remove it
157
- if os .path .isfile (self .log_file ):
158
- os .remove (self .log_file )
138
+ self ._init_clear_www_folder ()
159
139
self ._last_image = None
160
140
self ._update_time = None
161
141
self ._rrm_data = False # Check for rrm data
@@ -167,11 +147,13 @@ def __init__(self, hass, device_info):
167
147
self ._attr_brand = "MQTT Vacuum Camera"
168
148
169
149
@staticmethod
170
- def _handle_init_shared_data (mqtt_listen_topic : str ):
150
+ def _handle_init_shared_data (mqtt_listen_topic : str , device_info ):
171
151
"""Handle the shared data initialization."""
172
152
manager , shared , file_name = None , None , None
173
153
if mqtt_listen_topic :
174
- manager = CameraSharedManager (mqtt_listen_topic .split ("/" )[1 ].lower ())
154
+ manager = CameraSharedManager (
155
+ mqtt_listen_topic .split ("/" )[1 ].lower (), device_info
156
+ )
175
157
shared = manager .get_instance ()
176
158
file_name = shared .file_name
177
159
_LOGGER .debug (f"Camera { file_name } Starting up.." )
@@ -189,80 +171,24 @@ def _start_up_logs():
189
171
f" and In Use: { round ((ProcInsp ().psutil .virtual_memory ().used / (1024 * 1024 )), 1 )} "
190
172
)
191
173
192
- def _init_shared_data (self , device_info ):
193
- if self ._shared :
194
- try :
195
- self ._shared .attr_calibration_points = None
196
-
197
- # Initialize shared data with defaults from DEFAULT_VALUES
198
- self ._shared .offset_top = device_info .get (
199
- CONF_OFFSET_TOP , DEFAULT_VALUES ["offset_top" ]
200
- )
201
- self ._shared .offset_down = device_info .get (
202
- CONF_OFFSET_BOTTOM , DEFAULT_VALUES ["offset_bottom" ]
203
- )
204
- self ._shared .offset_left = device_info .get (
205
- CONF_OFFSET_LEFT , DEFAULT_VALUES ["offset_left" ]
206
- )
207
- self ._shared .offset_right = device_info .get (
208
- CONF_OFFSET_RIGHT , DEFAULT_VALUES ["offset_right" ]
209
- )
210
- self ._shared .image_auto_zoom = device_info .get (
211
- CONF_AUTO_ZOOM , DEFAULT_VALUES ["auto_zoom" ]
212
- )
213
- self ._shared .image_zoom_lock_ratio = device_info .get (
214
- CONF_ZOOM_LOCK_RATIO , DEFAULT_VALUES ["zoom_lock_ratio" ]
215
- )
216
- self ._shared .image_aspect_ratio = device_info .get (
217
- CONF_ASPECT_RATIO , DEFAULT_VALUES ["aspect_ratio" ]
218
- )
219
- self ._shared .image_rotate = int (
220
- device_info .get (ATTR_ROTATE , DEFAULT_VALUES ["rotate_image" ])
221
- )
222
- self ._shared .margins = int (
223
- device_info .get (ATTR_MARGINS , DEFAULT_VALUES ["margins" ])
224
- )
225
- self ._shared .show_vacuum_state = device_info .get (
226
- CONF_VAC_STAT , DEFAULT_VALUES ["show_vac_status" ]
227
- )
228
- self ._shared .vacuum_status_font = device_info .get (
229
- CONF_VAC_STAT_FONT , DEFAULT_VALUES ["vac_status_font" ]
230
- )
231
- self ._shared .vacuum_status_size = device_info .get (
232
- CONF_VAC_STAT_SIZE , DEFAULT_VALUES ["vac_status_size" ]
233
- )
234
- self ._shared .vacuum_status_position = device_info .get (
235
- CONF_VAC_STAT_POS , DEFAULT_VALUES ["vac_status_position" ]
236
- )
237
-
238
- # If enable_snapshots check if for png in www.
239
- self ._shared .enable_snapshots = device_info .get (
240
- CONF_SNAPSHOTS_ENABLE , DEFAULT_VALUES ["enable_www_snapshots" ]
241
- )
242
-
243
- if not self ._shared .enable_snapshots and os .path .isfile (
244
- f"{ self ._directory_path } /www/snapshot_{ self ._file_name } .png"
245
- ):
246
- os .remove (
247
- f"{ self ._directory_path } /www/snapshot_{ self ._file_name } .png"
248
- )
174
+ def _init_clear_www_folder (self ):
175
+ # If enable_snapshots check if for png in www
176
+ if not self ._shared .enable_snapshots and os .path .isfile (
177
+ f"{ self ._directory_path } /www/snapshot_{ self ._file_name } .png"
178
+ ):
179
+ os .remove (f"{ self ._directory_path } /www/snapshot_{ self ._file_name } .png" )
180
+ # If there is a log zip in www remove it
181
+ if os .path .isfile (self .log_file ):
182
+ os .remove (self .log_file )
249
183
250
- except TypeError as ex :
251
- _LOGGER .error (
252
- f"Shared data can't be initialized due to a TypeError! { ex } "
253
- )
254
- except AttributeError as ex :
255
- _LOGGER .error (
256
- f"Shared data can't be initialized due to an AttributeError! Possibly _shared is not properly initialized: { ex } "
257
- )
258
- except Exception as ex :
259
- _LOGGER .error (
260
- f"An unexpected error occurred while initializing shared data: { ex } "
261
- )
262
- else :
263
- _LOGGER .error (
264
- "Shared data initialization failed because _shared is not defined."
265
- )
184
+ def _init_paths (self ):
185
+ """Initialize Camera Paths"""
186
+ storage_path = f"{ self .hass .config .path (STORAGE_DIR )} /{ CAMERA_STORAGE } "
187
+ if not os .path .exists (storage_path ):
188
+ storage_path = f"{ self ._directory_path } /{ STORAGE_DIR } "
189
+ snapshot_img = f"{ storage_path } /{ self ._file_name } .png"
190
+ log_file = f"{ storage_path } /{ self ._file_name } .zip"
191
+ return storage_path , snapshot_img , log_file
266
192
267
193
async def async_added_to_hass (self ) -> None :
268
194
"""Handle entity added to Home Assistant."""
0 commit comments