From 928a609d4945a3787914e04bd0042fc2ae92977d Mon Sep 17 00:00:00 2001 From: firstof9 Date: Tue, 19 Jan 2021 12:32:42 -0700 Subject: [PATCH 1/2] Missed some config migration info * Fixed config migration for version 1 * Adjusted path to remove double // --- .../mail_and_packages/__init__.py | 22 ++++++++++++++----- custom_components/mail_and_packages/sensor.py | 4 ++-- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/custom_components/mail_and_packages/__init__.py b/custom_components/mail_and_packages/__init__.py index 7a302535..167c0710 100644 --- a/custom_components/mail_and_packages/__init__.py +++ b/custom_components/mail_and_packages/__init__.py @@ -112,14 +112,26 @@ async def async_migrate_entry(hass, config_entry): # 1 -> 3: Migrate format if version == 1: _LOGGER.debug("Migrating from version %s", version) - data = config_entry.data.copy() + updated_config = config_entry.data.copy() - if CONF_AMAZON_FWDS in data.keys(): - if not isinstance(data[CONF_AMAZON_FWDS], list): - data[CONF_AMAZON_FWDS] = data[CONF_AMAZON_FWDS].split(",") + if CONF_AMAZON_FWDS in updated_config.keys(): + if not isinstance(updated_config[CONF_AMAZON_FWDS], list): + updated_config[CONF_AMAZON_FWDS] = updated_config[ + CONF_AMAZON_FWDS + ].split(",") else: _LOGGER.warn("Missing configuration data: %s", CONF_AMAZON_FWDS) - hass.config_entries.async_update_entry(config_entry, data=data) + + # Force path change + updated_config[CONF_PATH] = "www/mail_and_packages/" + + # Always on image security + if not config_entry.data[CONF_IMAGE_SECURITY]: + updated_config[CONF_IMAGE_SECURITY] = True + + if updated_config != config_entry.data: + hass.config_entries.async_update_entry(config_entry, data=updated_config) + config_entry.version = 3 _LOGGER.debug("Migration to version %s complete", config_entry.version) diff --git a/custom_components/mail_and_packages/sensor.py b/custom_components/mail_and_packages/sensor.py index a6b04ba8..8d8d7861 100644 --- a/custom_components/mail_and_packages/sensor.py +++ b/custom_components/mail_and_packages/sensor.py @@ -142,9 +142,9 @@ def state(self): elif self.type == "usps_mail_image_url": if self.hass.config.external_url is None: _LOGGER.warn("External URL not set in configuration.") - return f"{self.hass.config.internal_url}/local/mail_and_packages/{self._image}" + return f"{self.hass.config.internal_url}local/mail_and_packages/{self._image}" return ( - f"{self.hass.config.external_url}/local/mail_and_packages/{self._image}" + f"{self.hass.config.external_url}local/mail_and_packages/{self._image}" ) else: return None From 829150d9b67ca32c3f7471411ce9526ed0529f25 Mon Sep 17 00:00:00 2001 From: firstof9 Date: Tue, 19 Jan 2021 14:27:25 -0700 Subject: [PATCH 2/2] Fix coordinator data update --- custom_components/mail_and_packages/const.py | 2 +- custom_components/mail_and_packages/sensor.py | 26 ++++++++++--------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/custom_components/mail_and_packages/const.py b/custom_components/mail_and_packages/const.py index 0ba15260..c2e3771f 100644 --- a/custom_components/mail_and_packages/const.py +++ b/custom_components/mail_and_packages/const.py @@ -1,6 +1,6 @@ DOMAIN = "mail_and_packages" DOMAIN_DATA = "{}_data".format(DOMAIN) -VERSION = "0.3.0-b34" +VERSION = "0.3.0-b35" ISSUE_URL = "http://github.com/moralmunky/Home-Assistant-Mail-And-Packages" PLATFORM = "sensor" DATA = "data" diff --git a/custom_components/mail_and_packages/sensor.py b/custom_components/mail_and_packages/sensor.py index 8d8d7861..cd873a74 100644 --- a/custom_components/mail_and_packages/sensor.py +++ b/custom_components/mail_and_packages/sensor.py @@ -7,7 +7,7 @@ import logging from homeassistant.const import CONF_HOST, CONF_RESOURCES -from homeassistant.helpers.entity import Entity +from homeassistant.helpers.update_coordinator import CoordinatorEntity from . import const @@ -29,7 +29,7 @@ async def async_setup_entry(hass, entry, async_add_entities): async_add_entities(sensors, False) -class PackagesSensor(Entity): +class PackagesSensor(CoordinatorEntity): """ Represntation of a sensor """ def __init__(self, config, sensor_type, coordinator, unique_id): @@ -85,13 +85,14 @@ def device_state_attributes(self): attr = {} attr[const.ATTR_SERVER] = self._host tracking = f"{self.type.split('_')[0]}_tracking" + data = self.coordinator.data if "Amazon" in self._name: - attr[const.ATTR_ORDER] = self.data[const.AMAZON_ORDER] + attr[const.ATTR_ORDER] = data[const.AMAZON_ORDER] elif "Mail USPS Mail" == self._name: - attr[const.ATTR_IMAGE] = self.data[const.ATTR_IMAGE_NAME] + attr[const.ATTR_IMAGE] = data[const.ATTR_IMAGE_NAME] elif "_delivering" in self.type and tracking in self.data.keys(): - attr[const.ATTR_TRACKING_NUM] = self.data[tracking] + attr[const.ATTR_TRACKING_NUM] = data[tracking] return attr async def async_update(self): @@ -108,7 +109,7 @@ async def async_added_to_hass(self): ) -class ImagePathSensors(Entity): +class ImagePathSensors(CoordinatorEntity): """ Represntation of a sensor """ def __init__(self, hass, config, sensor_type, coordinator, unique_id): @@ -137,15 +138,17 @@ def name(self): @property def state(self): """Return the state of the sensor.""" + image = self.coordinator.data[const.ATTR_IMAGE_NAME] + if self.type == "usps_mail_image_system_path": - return f"{self.hass.config.path()}/{self._config.data[const.CONF_PATH]}{self._image}" + return ( + f"{self.hass.config.path()}/{self._config.data[const.CONF_PATH]}{image}" + ) elif self.type == "usps_mail_image_url": if self.hass.config.external_url is None: _LOGGER.warn("External URL not set in configuration.") - return f"{self.hass.config.internal_url}local/mail_and_packages/{self._image}" - return ( - f"{self.hass.config.external_url}local/mail_and_packages/{self._image}" - ) + return f"{self.hass.config.internal_url}local/mail_and_packages/{image}" + return f"{self.hass.config.external_url}local/mail_and_packages/{image}" else: return None @@ -173,7 +176,6 @@ def available(self): def device_state_attributes(self): """Return device specific state attributes.""" attr = {} - return attr async def async_update(self):