Skip to content

Commit

Permalink
✨ add config media path
Browse files Browse the repository at this point in the history
  • Loading branch information
Jezza34000 committed Jan 23, 2025
1 parent 987f5b8 commit 4befc16
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
9 changes: 8 additions & 1 deletion custom_components/petkit/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
DEFAULT_SMART_POLLING,
DOMAIN,
LOGGER,
MEDIA_SECTION,
MEDIA_SECTION, CONF_MEDIA_PATH, DEFAULT_MEDIA_PATH,
)


Expand Down Expand Up @@ -92,6 +92,12 @@ async def async_step_init(
vol.Required(MEDIA_SECTION): section(
vol.Schema(
{
vol.Required(
CONF_MEDIA_PATH,
default=self.config_entry.options.get(
MEDIA_SECTION, {}
).get(CONF_MEDIA_PATH, DEFAULT_MEDIA_PATH),
): vol.All(str),
vol.Required(
CONF_SCAN_INTERVAL_MEDIA,
default=self.config_entry.options.get(
Expand Down Expand Up @@ -236,6 +242,7 @@ async def async_step_user(
CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL,
CONF_SMART_POLLING: DEFAULT_SMART_POLLING,
MEDIA_SECTION: {
CONF_MEDIA_PATH: DEFAULT_MEDIA_PATH,
CONF_SCAN_INTERVAL_MEDIA: DEFAULT_SCAN_INTERVAL_MEDIA,
CONF_MEDIA_DL_IMAGE: DEFAULT_DL_IMAGE,
CONF_MEDIA_DL_VIDEO: DEFAULT_DL_VIDEO,
Expand Down
4 changes: 2 additions & 2 deletions custom_components/petkit/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
LOGGER: Logger = getLogger(__package__)
DOMAIN = "petkit"

MEDIA_PATH = "media"

# Configuration
CONF_SCAN_INTERVAL_MEDIA = "scan_interval_media"
CONF_SMART_POLLING = "smart_polling"
Expand All @@ -20,6 +18,7 @@
CONF_MEDIA_DL_IMAGE = "media_dl_image"
CONF_MEDIA_EV_TYPE = "media_ev_type"
CONF_DELETE_AFTER = "delete_media_after"
CONF_MEDIA_PATH = "media_path"

# Default configuration values
DEFAULT_SCAN_INTERVAL = 60
Expand All @@ -31,6 +30,7 @@
DEFAULT_SMART_POLLING = True
DEFAULT_BLUETOOTH_RELAY = True
DEFAULT_DELETE_AFTER = 3
DEFAULT_MEDIA_PATH = "/media"

# Update interval
MAX_SCAN_INTERVAL = 120
Expand Down
15 changes: 7 additions & 8 deletions custom_components/petkit/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@
DEFAULT_SMART_POLLING,
DOMAIN,
LOGGER,
MEDIA_PATH,
MEDIA_SECTION,
MIN_SCAN_INTERVAL,
MIN_SCAN_INTERVAL, DEFAULT_MEDIA_PATH, CONF_MEDIA_PATH,
)


Expand Down Expand Up @@ -144,6 +143,7 @@ def __init__(
self.previous_devices = set()
self.media_table = {}
self.delete_after = 0
self.media_path = Path()
# Load configuration
self._get_media_config(config_entry.options)

Expand All @@ -153,6 +153,7 @@ def _get_media_config(self, options) -> None:
event_type_config = media_options.get(CONF_MEDIA_EV_TYPE, DEFAULT_EVENTS)
dl_image = media_options.get(CONF_MEDIA_DL_IMAGE, DEFAULT_DL_IMAGE)
dl_video = media_options.get(CONF_MEDIA_DL_VIDEO, DEFAULT_DL_VIDEO)
self.media_path = Path(media_options.get(CONF_MEDIA_PATH, DEFAULT_MEDIA_PATH))
self.delete_after = media_options.get(CONF_DELETE_AFTER, DEFAULT_DELETE_AFTER)

self.event_type = [RecordType(element.lower()) for element in event_type_config]
Expand All @@ -175,7 +176,6 @@ async def _async_update_data(
async def _async_update_media_files(self, devices_lst: set) -> None:
"""Update media files."""
client = self.config_entry.runtime_data.client
media_path = Path(MEDIA_PATH)

for device in devices_lst:
if not hasattr(client.petkit_entities[device], "medias"):
Expand All @@ -189,20 +189,20 @@ async def _async_update_media_files(self, devices_lst: set) -> None:
continue

LOGGER.debug(f"Gathering medias files onto disk for device id = {device}")
await client.media_manager.gather_all_media_from_disk(media_path, device)
await client.media_manager.gather_all_media_from_disk(self.media_path, device)
to_dl = await client.media_manager.list_missing_files(
media_lst, self.media_type, self.event_type
)

dl_mgt = DownloadDecryptMedia(media_path, client)
dl_mgt = DownloadDecryptMedia(self.media_path, client)
for media in to_dl:
await dl_mgt.download_file(media, self.media_type)
LOGGER.debug(
f"Downloaded all medias for device id = {device} is OK (got {len(to_dl)} files to download)"
)
self.media_table[device] = (
await client.media_manager.gather_all_media_from_disk(
media_path, device
self.media_path, device
)
)
LOGGER.debug("Update media files finished for all devices")
Expand All @@ -215,10 +215,9 @@ async def _async_delete_old_media(self) -> None:
return

retention_date = datetime.now() - timedelta(days=self.delete_after)
media_path = Path(MEDIA_PATH)

for device_id in self.data_coordinator.current_devices:
device_media_path = media_path / str(device_id)
device_media_path = self.media_path / str(device_id)
if not await aiofiles.os.path.exists(str(device_media_path)):
continue

Expand Down
2 changes: 2 additions & 0 deletions custom_components/petkit/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -602,13 +602,15 @@
},
"medias_options": {
"data": {
"media_path": "Media path",
"delete_media_after": "Delete media after (days)",
"media_dl_image": "Fetch images",
"media_dl_video": "Fetch videos",
"media_ev_type": "Event type for download",
"scan_interval_media": "Media refresh interval (minutes)"
},
"data_description": {
"media_path": "Path where the media will be stored. If not specified, the media will be stored in /media Home Assistant folder.",
"delete_media_after": "Delete downloaded media after the specified number of days. (0 = never delete)",
"media_dl_image": "Download all images from your devices, filtered by the selected events below (no active Care+ subscription required).",
"media_dl_video": "Download all videos from your devices, filtered by the selected events below. (required an active Care+ subscription)",
Expand Down

0 comments on commit 4befc16

Please sign in to comment.