Move imports in imap + imap_email_content component (#27793)

This commit is contained in:
Quentame 2019-10-17 15:01:50 +02:00 committed by Pascal Vizeli
parent 88a78a4a18
commit 9dc0c05ee0
2 changed files with 6 additions and 14 deletions

View File

@ -2,6 +2,7 @@
import asyncio import asyncio
import logging import logging
from aioimaplib import IMAP4_SSL, AioImapException
import async_timeout import async_timeout
import voluptuous as vol import voluptuous as vol
@ -107,24 +108,20 @@ class ImapSensor(Entity):
async def connection(self): async def connection(self):
"""Return a connection to the server, establishing it if necessary.""" """Return a connection to the server, establishing it if necessary."""
import aioimaplib
if self._connection is None: if self._connection is None:
try: 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.wait_hello_from_server()
await self._connection.login(self._user, self._password) await self._connection.login(self._user, self._password)
await self._connection.select(self._folder) await self._connection.select(self._folder)
self._does_push = self._connection.has_capability("IDLE") self._does_push = self._connection.has_capability("IDLE")
except (aioimaplib.AioImapException, asyncio.TimeoutError): except (AioImapException, asyncio.TimeoutError):
self._connection = None self._connection = None
return self._connection return self._connection
async def idle_loop(self): async def idle_loop(self):
"""Wait for data pushed from server.""" """Wait for data pushed from server."""
import aioimaplib
while True: while True:
try: try:
if await self.connection(): if await self.connection():
@ -138,17 +135,15 @@ class ImapSensor(Entity):
await idle await idle
else: else:
await self.async_update_ha_state() await self.async_update_ha_state()
except (aioimaplib.AioImapException, asyncio.TimeoutError): except (AioImapException, asyncio.TimeoutError):
self.disconnected() self.disconnected()
async def async_update(self): async def async_update(self):
"""Periodic polling of state.""" """Periodic polling of state."""
import aioimaplib
try: try:
if await self.connection(): if await self.connection():
await self.refresh_email_count() await self.refresh_email_count()
except (aioimaplib.AioImapException, asyncio.TimeoutError): except (AioImapException, asyncio.TimeoutError):
self.disconnected() self.disconnected()
async def refresh_email_count(self): async def refresh_email_count(self):

View File

@ -4,6 +4,7 @@ import datetime
import email import email
from collections import deque from collections import deque
import imaplib
import voluptuous as vol import voluptuous as vol
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
@ -88,8 +89,6 @@ class EmailReader:
def connect(self): def connect(self):
"""Login and setup the connection.""" """Login and setup the connection."""
import imaplib
try: try:
self.connection = imaplib.IMAP4_SSL(self._server, self._port) self.connection = imaplib.IMAP4_SSL(self._server, self._port)
self.connection.login(self._user, self._password) self.connection.login(self._user, self._password)
@ -110,8 +109,6 @@ class EmailReader:
def read_next(self): def read_next(self):
"""Read the next email from the email server.""" """Read the next email from the email server."""
import imaplib
try: try:
self.connection.select(self._folder, readonly=True) self.connection.select(self._folder, readonly=True)