Skip to content

Commit

Permalink
Fix coordinator data update (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
firstof9 authored Jan 19, 2021
2 parents a470244 + 829150d commit b209af4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
22 changes: 17 additions & 5 deletions custom_components/mail_and_packages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion custom_components/mail_and_packages/const.py
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
26 changes: 14 additions & 12 deletions custom_components/mail_and_packages/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit b209af4

Please sign in to comment.