From bd1f349c5cecf2ff53066f68a2d1bdb48545eb4d Mon Sep 17 00:00:00 2001 From: Kiran Kumar Date: Wed, 13 Sep 2023 11:01:28 +0530 Subject: [PATCH 1/2] Update sensor.py to add file URL sensor --- custom_components/google_photos/sensor.py | 51 +++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/custom_components/google_photos/sensor.py b/custom_components/google_photos/sensor.py index ec4560f..ed93cad 100644 --- a/custom_components/google_photos/sensor.py +++ b/custom_components/google_photos/sensor.py @@ -32,6 +32,7 @@ async def async_setup_entry( coordinator = await coordinator_manager.get_coordinator(album_id) entities.append(GooglePhotosMediaCount(coordinator)) entities.append(GooglePhotosFileName(coordinator)) + entities.append(GooglePhotosFileURL(coordinator)) entities.append(GooglePhotosCreationTimestamp(coordinator)) async_add_entities( @@ -89,6 +90,56 @@ def _handle_coordinator_update(self) -> None: """Handle updated data from the coordinator.""" self._read_value() +class GooglePhotosFileURL(SensorEntity): + """Sensor to display current file URL""" + + coordinator: Coordinator + _attr_has_entity_name = True + _attr_icon = "mdi:text-short" + + def __init__(self, coordinator: Coordinator) -> None: + """Initialize a sensor class.""" + super().__init__() + self.coordinator = coordinator + self.entity_description = SensorEntityDescription( + key="fileurl", name="File URL", icon=self._attr_icon + ) + album_id = self.coordinator.album["id"] + self._attr_device_info = self.coordinator.get_device_info() + self._attr_unique_id = f"{album_id}-fileurl" + + async def async_added_to_hass(self) -> None: + """When entity is added to hass.""" + await super().async_added_to_hass() + self.async_on_remove( + self.coordinator.async_add_listener(self._handle_coordinator_update) + ) + self._read_value() + + @property + def should_poll(self) -> bool: + """No need to poll. Coordinator notifies entity of updates.""" + return False + + @property + def available(self) -> bool: + """Return if entity is available.""" + return ( + self.coordinator.last_update_success + and self.coordinator.current_media is not None + ) + + def _read_value(self) -> None: + if self.coordinator.current_media is not None: + self._attr_native_value = self.coordinator.current_media.get("productUrl") + self.async_write_ha_state() + + @callback + def _handle_coordinator_update(self) -> None: + """Handle updated data from the coordinator.""" + self._read_value() + + class GooglePhotosCreationTimestamp(SensorEntity): """Sensor to display the current creation timestamp""" From d3506fb8d311a288e7c60df432c8f17e994d3f0d Mon Sep 17 00:00:00 2001 From: Daan Sieben Date: Fri, 6 Oct 2023 09:32:07 +0200 Subject: [PATCH 2/2] feat: expose productUrl as attribute --- custom_components/google_photos/camera.py | 3 ++ custom_components/google_photos/sensor.py | 51 ----------------------- 2 files changed, 3 insertions(+), 51 deletions(-) diff --git a/custom_components/google_photos/camera.py b/custom_components/google_photos/camera.py index e529db4..a6e0be7 100644 --- a/custom_components/google_photos/camera.py +++ b/custom_components/google_photos/camera.py @@ -125,6 +125,9 @@ def _handle_coordinator_update(self) -> None: self._attr_extra_state_attributes["media_contributor_info"] = ( media.get("contributorInfo") or {} ) + self._attr_extra_state_attributes["media_url"] = ( + media.get("productUrl") or {} + ) self.async_write_ha_state() def next_media(self, mode=None): diff --git a/custom_components/google_photos/sensor.py b/custom_components/google_photos/sensor.py index ed93cad..ec4560f 100644 --- a/custom_components/google_photos/sensor.py +++ b/custom_components/google_photos/sensor.py @@ -32,7 +32,6 @@ async def async_setup_entry( coordinator = await coordinator_manager.get_coordinator(album_id) entities.append(GooglePhotosMediaCount(coordinator)) entities.append(GooglePhotosFileName(coordinator)) - entities.append(GooglePhotosFileURL(coordinator)) entities.append(GooglePhotosCreationTimestamp(coordinator)) async_add_entities( @@ -90,56 +89,6 @@ def _handle_coordinator_update(self) -> None: """Handle updated data from the coordinator.""" self._read_value() -class GooglePhotosFileURL(SensorEntity): - """Sensor to display current file URL""" - - coordinator: Coordinator - _attr_has_entity_name = True - _attr_icon = "mdi:text-short" - - def __init__(self, coordinator: Coordinator) -> None: - """Initialize a sensor class.""" - super().__init__() - self.coordinator = coordinator - self.entity_description = SensorEntityDescription( - key="fileurl", name="File URL", icon=self._attr_icon - ) - album_id = self.coordinator.album["id"] - self._attr_device_info = self.coordinator.get_device_info() - self._attr_unique_id = f"{album_id}-fileurl" - - async def async_added_to_hass(self) -> None: - """When entity is added to hass.""" - await super().async_added_to_hass() - self.async_on_remove( - self.coordinator.async_add_listener(self._handle_coordinator_update) - ) - self._read_value() - - @property - def should_poll(self) -> bool: - """No need to poll. Coordinator notifies entity of updates.""" - return False - - @property - def available(self) -> bool: - """Return if entity is available.""" - return ( - self.coordinator.last_update_success - and self.coordinator.current_media is not None - ) - - def _read_value(self) -> None: - if self.coordinator.current_media is not None: - self._attr_native_value = self.coordinator.current_media.get("productUrl") - self.async_write_ha_state() - - @callback - def _handle_coordinator_update(self) -> None: - """Handle updated data from the coordinator.""" - self._read_value() - - class GooglePhotosCreationTimestamp(SensorEntity): """Sensor to display the current creation timestamp"""