Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loglinefetch for frontend API call #10579

Merged
merged 9 commits into from Nov 17, 2017
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 25 additions & 3 deletions homeassistant/components/config/zwave.py
Expand Up @@ -2,6 +2,7 @@
import asyncio
import logging

from collections import deque
import homeassistant.core as ha
from homeassistant.const import HTTP_NOT_FOUND, HTTP_OK
from homeassistant.components.http import HomeAssistantView
Expand All @@ -12,7 +13,6 @@
_LOGGER = logging.getLogger(__name__)
CONFIG_PATH = 'zwave_device_config.yaml'
OZW_LOG_FILENAME = 'OZW_Log.txt'
URL_API_OZW_LOG = '/api/zwave/ozwlog'


@asyncio.coroutine
Expand All @@ -26,13 +26,35 @@ def async_setup(hass):
hass.http.register_view(ZWaveNodeGroupView)
hass.http.register_view(ZWaveNodeConfigView)
hass.http.register_view(ZWaveUserCodeView)
hass.http.register_static_path(
URL_API_OZW_LOG, hass.config.path(OZW_LOG_FILENAME), False)
hass.http.register_view(ZWaveLogView)
hass.http.register_view(ZWaveConfigWriteView)

return True


class ZWaveLogView(HomeAssistantView):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too many blank lines (3)

"""View to read the ZWave log file."""

url = r"/api/zwave/ozwlog/{lines:\d+}"
name = "api:zwave:ozwlog"

@ha.callback
def get(self, request, lines):
"""Retrieve the lines from ZWave log."""
lines = int(lines)
hass = request.app['hass']
logfilepath = hass.config.path(OZW_LOG_FILENAME)

with open(logfilepath, 'r') as logfile:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you're not allowed to do I/O in the event loop.

Change method signature to @asyncio.coroutine

Move the opening of file to a new method on this class and call it _get_log(self, hass, lines).

response = yield from hass.async_add_job(self._get_log, hass, lines)

Ignoring pylint for no self use = 👍

data = (line.rstrip() for line in logfile)
if lines == 0:
loglines = list(data)
else:
loglines = list(deque(data, lines))

return self.json('\n'.join(loglines))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't return json. Instead, return just plain text.

# At top of file
from aiohttp.web import Response

return Response(text='\n'.join(loglines))

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also means that you no longer have to convert your deque to a list.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method gives pylint: Method could be a function (no-self-use) Is that safe to ignore?



class ZWaveConfigWriteView(HomeAssistantView):
"""View to save the ZWave configuration to zwcfg_xxxxx.xml."""

Expand Down