diff --git a/homeassistant/components/imap/sensor.py b/homeassistant/components/imap/sensor.py index 0ae79d34cf0..a10fefa1b16 100644 --- a/homeassistant/components/imap/sensor.py +++ b/homeassistant/components/imap/sensor.py @@ -2,6 +2,7 @@ import asyncio import logging +from aioimaplib import IMAP4_SSL, AioImapException import async_timeout import voluptuous as vol @@ -107,24 +108,20 @@ class ImapSensor(Entity): async def connection(self): """Return a connection to the server, establishing it if necessary.""" - import aioimaplib - if self._connection is None: try: - self._connection = aioimaplib.IMAP4_SSL(self._server, self._port) + self._connection = IMAP4_SSL(self._server, self._port) await self._connection.wait_hello_from_server() await self._connection.login(self._user, self._password) await self._connection.select(self._folder) self._does_push = self._connection.has_capability("IDLE") - except (aioimaplib.AioImapException, asyncio.TimeoutError): + except (AioImapException, asyncio.TimeoutError): self._connection = None return self._connection async def idle_loop(self): """Wait for data pushed from server.""" - import aioimaplib - while True: try: if await self.connection(): @@ -138,17 +135,15 @@ class ImapSensor(Entity): await idle else: await self.async_update_ha_state() - except (aioimaplib.AioImapException, asyncio.TimeoutError): + except (AioImapException, asyncio.TimeoutError): self.disconnected() async def async_update(self): """Periodic polling of state.""" - import aioimaplib - try: if await self.connection(): await self.refresh_email_count() - except (aioimaplib.AioImapException, asyncio.TimeoutError): + except (AioImapException, asyncio.TimeoutError): self.disconnected() async def refresh_email_count(self): diff --git a/homeassistant/components/imap_email_content/sensor.py b/homeassistant/components/imap_email_content/sensor.py index c5171cde646..62dceae0dad 100644 --- a/homeassistant/components/imap_email_content/sensor.py +++ b/homeassistant/components/imap_email_content/sensor.py @@ -4,6 +4,7 @@ import datetime import email from collections import deque +import imaplib import voluptuous as vol from homeassistant.helpers.entity import Entity @@ -88,8 +89,6 @@ class EmailReader: def connect(self): """Login and setup the connection.""" - import imaplib - try: self.connection = imaplib.IMAP4_SSL(self._server, self._port) self.connection.login(self._user, self._password) @@ -110,8 +109,6 @@ class EmailReader: def read_next(self): """Read the next email from the email server.""" - import imaplib - try: self.connection.select(self._folder, readonly=True)