Skip to content

Commit

Permalink
few fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelveldt committed Sep 2, 2020
1 parent 7197529 commit d96a693
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
22 changes: 19 additions & 3 deletions hass_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def __init__(self, url: str = None, token: str = None):
Initialize the connection to HomeAssistant.
:param url: full url to the HomeAssistant instance.
:param token: a long lived token.
Note: Leave url and token empty for auto detection on supervisor addon's.
"""
self._loop = asyncio.get_event_loop()
self._states = {}
Expand All @@ -48,8 +47,17 @@ def __init__(self, url: str = None, token: str = None):
self._initial_state_received = False
self._connected_callback = None
self._event_listeners = []
self._ws_task = None

async def connect(self):
def connect(self):
"""Start the connection."""
self._loop.create_task(self.async_connect())

def close(self):
"""Close the connection."""
self._loop.create_task(self.async_close())

async def async_connect(self):
"""Start the connection."""
if not self._loop.is_running:
raise RuntimeError("A running eventloop is required!")
Expand All @@ -58,7 +66,15 @@ async def connect(self):
self._http_session = aiohttp.ClientSession(
loop=self._loop, connector=aiohttp.TCPConnector()
)
self._loop.create_task(self.__async_hass_websocket())
self._ws_task = self._loop.create_task(self.__async_hass_websocket())

async def async_close(self):
"""Close/stop the connection."""
if self._ws_task:
self._ws_task.cancel()
if self._http_session:
await self._http_session.close()
LOGGER.info("Disconnected from Home Assistant")

def register_event_callback(
self,
Expand Down
8 changes: 3 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@

PROJECT_DIR = Path(__file__).parent.resolve()
README_FILE = PROJECT_DIR / "README.md"
VERSION = "0.0.1"
VERSION = "0.0.2"

with open("requirements.txt") as f:
INSTALL_REQUIRES = f.read().splitlines()
if os.name != "nt":
INSTALL_REQUIRES.append("uvloop")

PACKAGE_FILES = []
for (path, directories, filenames) in os.walk('hass_client/'):
Expand All @@ -20,8 +18,8 @@
setup(
name="hass_client",
version=VERSION,
url="https://github.com/marcelveldt/hass-client",
download_url="https://github.com/marcelveldt/hass-client",
url="https://github.com/marcelveldt/python-hass-client",
download_url="https://github.com/marcelveldt/python-hass-client",
author="Marcel van der Veldt",
author_email="[email protected]",
description="Basic client for connecting to Home Assistant over websockets and REST.",
Expand Down
5 changes: 4 additions & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ async def hass_event(event, event_details):
hass.register_event_callback(hass_event, )

async def run():
await hass.connect()
await hass.async_connect()
await asyncio.sleep(10)
await hass.async_close()
loop.stop()

try:
loop.create_task(run())
Expand Down

0 comments on commit d96a693

Please sign in to comment.