Skip to content

Commit 74384b6

Browse files
authored
Merge pull request #275 from sca075/refactoring_camera
Releasing 2024.11.1
2 parents d5d0b80 + 15f3ffe commit 74384b6

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

custom_components/mqtt_vacuum_camera/__init__.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Version: 2024.11.1"""
33

44
import logging
5+
from functools import partial
56
import os
67

78
from homeassistant import config_entries, core
@@ -27,7 +28,7 @@
2728
DOMAIN,
2829
)
2930
from .coordinator import MQTTVacuumCoordinator
30-
from .utils.camera.camera_services import reset_trims, reload_config
31+
from .utils.camera.camera_services import reset_trims, reload_camera_config
3132
from .utils.files_operations import (
3233
async_clean_up_all_auto_crop_files,
3334
async_get_translations_vacuum_id,
@@ -83,10 +84,10 @@ async def async_setup_entry(hass: core.HomeAssistant, entry: ConfigEntry) -> boo
8384
# Register Services
8485
if not hass.services.has_service(DOMAIN, SERVICE_RELOAD):
8586
async_register_admin_service(
86-
hass, DOMAIN, SERVICE_RELOAD, lambda call: reload_config(hass, DOMAIN)
87+
hass, DOMAIN, SERVICE_RELOAD, partial(reload_camera_config, hass=hass)
8788
)
8889
hass.services.async_register(
89-
DOMAIN, "reset_trims", lambda call: reset_trims(hass, call, DOMAIN)
90+
DOMAIN, "reset_trims", partial(reset_trims, hass=hass)
9091
)
9192
await async_register_vacuums_services(hass, data_coordinator)
9293
# Registers update listener to update config entry when options are updated.
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
11
"""Camera-related services for the MQTT Vacuum Camera integration."""
22

3+
import logging
34
import asyncio
45
import async_timeout
5-
import logging
6-
76

87
from homeassistant.core import ServiceCall, HomeAssistant
98
from homeassistant.config_entries import ConfigEntryState
109
from homeassistant.const import SERVICE_RELOAD
1110

1211
from ...utils.files_operations import async_clean_up_all_auto_crop_files
12+
from ...const import DOMAIN
1313

1414
_LOGGER = logging.getLogger(__name__)
1515

16-
async def reset_trims(hass: HomeAssistant, call: ServiceCall, domain: str) -> None:
16+
17+
async def reset_trims(call: ServiceCall, hass: HomeAssistant) -> None:
1718
"""Action Reset Map Trims."""
18-
_LOGGER.debug(f"Resetting trims for {domain}")
19+
_LOGGER.debug(f"Resetting trims for {DOMAIN}")
1920
try:
2021
await async_clean_up_all_auto_crop_files(hass)
21-
await hass.services.async_call(domain, SERVICE_RELOAD)
22-
hass.bus.async_fire(f"event_{domain}_reset_trims", context=call.context)
22+
await hass.services.async_call(DOMAIN, SERVICE_RELOAD)
23+
hass.bus.async_fire(f"event_{DOMAIN}_reset_trims", context=call.context)
2324
except Exception as err:
2425
_LOGGER.error(f"Error resetting trims: {err}")
2526

2627

27-
async def reload_config(hass: HomeAssistant, domain: str) -> None:
28+
async def reload_camera_config(call: ServiceCall, hass: HomeAssistant) -> None:
2829
"""Reload the camera platform for all entities in the integration."""
29-
_LOGGER.debug(f"Reloading the config entry for all {domain} entities")
30-
camera_entries = hass.config_entries.async_entries(domain)
30+
31+
_LOGGER.debug(f"Reloading the config entry for all {DOMAIN} entities")
32+
camera_entries = hass.config_entries.async_entries(DOMAIN)
3133
total_entries = len(camera_entries)
3234
processed = 0
3335

3436
for camera_entry in camera_entries:
3537
processed += 1
3638
_LOGGER.info(f"Processing entry {processed}/{total_entries}")
3739
if camera_entry.state == ConfigEntryState.LOADED:
38-
_LOGGER.debug(f"Unloading entry: {camera_entry.entry_id}")
3940
try:
40-
async with async_timeout.timeout(30):
41-
await hass.config_entries.async_unload(camera_entry.entry_id)
42-
43-
_LOGGER.debug(f"Reloading entry: {camera_entry.entry_id}")
44-
await hass.config_entries.async_setup(camera_entry.entry_id)
41+
with async_timeout.timeout(10):
42+
_LOGGER.debug(f"Reloading entry: {camera_entry.entry_id}")
43+
hass.config_entries.async_schedule_reload(camera_entry.entry_id)
4544
except asyncio.TimeoutError:
46-
_LOGGER.error(f"Timeout while processing entry {camera_entry.entry_id}")
47-
continue
45+
_LOGGER.error(f"Timeout processing entry {camera_entry.entry_id}")
4846
except Exception as err:
4947
_LOGGER.error(f"Error processing entry {camera_entry.entry_id}: {err}")
5048
continue
5149
else:
52-
_LOGGER.debug(
53-
f"Skipping entry {camera_entry.entry_id} as it is NOT_LOADED"
54-
)
55-
56-
hass.bus.async_fire(f"event_{domain}_reloaded", {
57-
"processed": processed,
58-
"total": total_entries
59-
})
50+
_LOGGER.debug(f"Skipping entry {camera_entry.entry_id} as it is NOT_LOADED")
51+
52+
hass.bus.async_fire(
53+
f"event_{DOMAIN}_reloaded",
54+
event_data={
55+
"processed": processed,
56+
"total": total_entries,
57+
},
58+
context=call.context,
59+
)

custom_components/mqtt_vacuum_camera/utils/vacuum/mqtt_vacuum_services.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ async def vacuum_clean_segments(call: ServiceCall, coordinator) -> None:
5050
service_data["payload"],
5151
)
5252
except Exception as e:
53-
raise ServiceValidationError(f"Error sending command to vacuum: {e}") from e
53+
raise ServiceValidationError(
54+
f"Error sending command to vacuum: {e}"
55+
) from e
5456

5557
coordinator.hass.bus.async_fire(
5658
f"event_{DOMAIN}.vacuum_clean_zone",

0 commit comments

Comments
 (0)