Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions homeassistant/components/sensor/linky.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,27 @@ def setup_platform(hass, config, add_entities, discovery_info=None):

from pylinky.client import LinkyClient, PyLinkyError
client = LinkyClient(username, password, None, timeout)

try:
client.fetch_data()
except PyLinkyError as exp:
_LOGGER.error(exp)
return
finally:
client.close_session()

devices = [LinkySensor('Linky', client)]
devices = [LinkySensor('Linky', username, password, timeout)]
add_entities(devices, True)


class LinkySensor(Entity):
"""Representation of a sensor entity for Linky."""

def __init__(self, name, client):
def __init__(self, name, username, password, timeout):
"""Initialize the sensor."""
self._name = name
self._client = client
self._username = username
self._password = password
self._timeout = timeout
self._state = None

@property
Expand All @@ -75,17 +78,21 @@ def unit_of_measurement(self):
@Throttle(SCAN_INTERVAL)
def update(self):
"""Fetch new state data for the sensor."""
from pylinky.client import PyLinkyError
from pylinky.client import LinkyClient, PyLinkyError

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

'pylinky.client.LinkyClient' imported but unused


client = LinkyClient(self._username, self._password, None, self._timeout)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (81 > 79 characters)

try:
self._client.fetch_data()
client.fetch_data()
except PyLinkyError as exp:
_LOGGER.error(exp)
return
finally:
client.close_session()

_LOGGER.debug(json.dumps(self._client.get_data(), indent=2))
_LOGGER.debug(json.dumps(client.get_data(), indent=2))

if self._client.get_data():
if client.get_data():
# get the last past day data
self._state = self._client.get_data()['daily'][-2]['conso']
self._state = client.get_data()['daily'][-2]['conso']
else:
self._state = None