Skip to content

Commit

Permalink
Merge pull request #76 from firstof9/dev
Browse files Browse the repository at this point in the history
Properly unload sensor entities
  • Loading branch information
firstof9 authored Mar 22, 2020
2 parents bec2918 + 9076067 commit 21e8f8a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
3 changes: 2 additions & 1 deletion custom_components/mail_and_packages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def async_setup_entry(hass, config_entry):
return True


async def async_remove_entry(hass, config_entry):
async def async_unload_entry(hass, config_entry):
"""Handle removal of an entry."""
try:
await hass.config_entries.async_forward_entry_unload(config_entry,
Expand All @@ -41,6 +41,7 @@ async def async_remove_entry(hass, config_entry):
)
except ValueError:
pass
return True


async def update_listener(hass, entry):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/mail_and_packages/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ async def _show_step_options_2(self, user_input):
gif_duration = user_input["gif_duration"]
if "scan_interval" in user_input:
scan_interval = user_input["scan_interval"]

data_schema = OrderedDict()
data_schema[vol.Required("folder",
default=folder)] = vol.In(mailboxes)
Expand Down
5 changes: 2 additions & 3 deletions custom_components/mail_and_packages/const.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DOMAIN = "mail_and_packages"
VERSION = "0.1.3-b7"
VERSION = "0.1.3-b8"
ISSUE_URL = "http://github.com/moralmunky/Home-Assistant-Mail-And-Packages"

CONF_FOLDER = "folder"
Expand Down Expand Up @@ -32,5 +32,4 @@

GIF_FILE_NAME = 'mail_today.gif'
GIF_DURATION = 5

SCAN_INTERVAL = 5
SCAN_INTERVAL = 5
42 changes: 24 additions & 18 deletions custom_components/mail_and_packages/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

_LOGGER = logging.getLogger(__name__)


async def async_setup_entry(hass, entry, async_add_entities):

config = {
Expand Down Expand Up @@ -105,7 +106,7 @@ def __init__(self, hass, config):
self._amazon_packages = None
self._amazon_items = None
_LOGGER.debug("Config scan interval: %s", self._scan_interval)

self.update = Throttle(self._scan_interval)(self.update)

def update(self):
Expand Down Expand Up @@ -160,7 +161,7 @@ def update(self):

else:
_LOGGER.debug("Host was left blank not attempting connection")

self._scan_time = update_time()
_LOGGER.debug("Updated scan time: %s", self._scan_time)

Expand Down Expand Up @@ -907,8 +908,10 @@ def selectfolder(account, folder):

def get_formatted_date():
today = datetime.datetime.today().strftime('%d-%b-%Y')
#for testing
#today = '18-Mar-2020'
"""
# for testing
# today = '18-Mar-2020'
"""
return today


Expand Down Expand Up @@ -938,6 +941,14 @@ def get_mails(account, image_output_path, gif_duration):
'(FROM "' + USPS_Mail_Email + '" SUBJECT "' +
USPS_Mail_Subject + '" ON "' + today + '")')

# Check to see if the path exists, if not make it
pathcheck = os.path.isdir(image_output_path)
if not pathcheck:
try:
os.makedirs(image_output_path)
except Exception as err:
_LOGGER.critical("Error creating directory: %s", str(err))

if rv == 'OK':
_LOGGER.debug("Informed Delivery email found processing...")
for num in data[0].split():
Expand All @@ -954,15 +965,6 @@ def get_mails(account, image_output_path, gif_duration):
_LOGGER.debug("Extracting image from email")
filepath = image_output_path + part.get_filename()

# Check to see if the path exists, if not make it
pathcheck = os.path.isdir(image_output_path)
if not pathcheck:
try:
os.mkdir(image_output_path)
except Exception as err:
_LOGGER.critical("Error creating directory: %s",
str(err))

# Log error message if we are unable to open the filepath for
# some reason
try:
Expand Down Expand Up @@ -1029,11 +1031,15 @@ def get_mails(account, image_output_path, gif_duration):

elif image_count == 0:
_LOGGER.info("No mail found.")
try:
_LOGGER.debug("Removing " + image_output_path + GIF_FILE_NAME)
os.remove(image_output_path + GIF_FILE_NAME)
except Exception as err:
_LOGGER.error("Error attempting to remove image: %s", str(err))
filecheck = os.path.isfile(image_output_path + GIF_FILE_NAME)
if filecheck:
try:
_LOGGER.debug("Removing " + image_output_path +
GIF_FILE_NAME)
os.remove(image_output_path + GIF_FILE_NAME)
except Exception as err:
_LOGGER.error("Error attempting to remove image: %s",
str(err))
try:
_LOGGER.debug("Copying nomail gif")
copyfile(os.path.dirname(__file__) + '/mail_none.gif',
Expand Down

0 comments on commit 21e8f8a

Please sign in to comment.