Skip to content

Commit

Permalink
added requirements_all
Browse files Browse the repository at this point in the history
reordered imports and flake8

attempt to pelase a very picky linter

also pleasing pylint now :)
  • Loading branch information
c7h committed Feb 11, 2018
1 parent 46c1339 commit 6db0fb2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
38 changes: 22 additions & 16 deletions homeassistant/components/lock/kiwi.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
https://home-assistant.io/components/lock.kiwi/
"""
import logging
import requests
import datetime
import dateutil.parser
import requests

import voluptuous as vol

import homeassistant.helpers.config_validation as cv
from homeassistant.components.lock import (LockDevice, PLATFORM_SCHEMA)
from homeassistant.const import (
CONF_PASSWORD, CONF_USERNAME, ATTR_ID, ATTR_LONGITUDE, ATTR_LATITUDE)
CONF_PASSWORD, CONF_USERNAME, ATTR_ID, ATTR_LONGITUDE, ATTR_LATITUDE)

REQUIREMENTS = ['python-dateutil==2.6.1']

Expand All @@ -36,13 +35,20 @@


def setup_platform(hass, config, add_devices, discovery_info=None):
"""set up the KIWI lock platform."""
"""Set up the KIWI lock platform."""
kiwi = KiwiClient(config.get(CONF_USERNAME), config.get(CONF_PASSWORD))
add_devices([KiwiLock(lock, kiwi) for lock in kiwi.get_locks()], True)


class KiwiClient:
"""Client for KIWI service."""

def __init__(self, username, password):
"""Initiale the client.
:param username: valid KIWI username. Hint: your signup email address.
:param password: your KIWI account password.
"""
self.__username = username
self.__password = password
self.__session_key = None
Expand All @@ -52,14 +58,15 @@ def __init__(self, username, password):
self._renew_sessionkey()

def _with_valid_session(self):
"""check if the session is valid; renew if necessary"""
"""Check if the session is valid; renew if necessary."""
now = datetime.datetime.now(tz=datetime.timezone.utc)
if not self.__session_expires or (now >= self.__session_expires):
_LOGGER.debug("no valid session found - renewing session key")
self._renew_sessionkey()

def _renew_sessionkey(self):
"""update the clients session key."""
"""Update the clients session key."""
import dateutil.parser
_LOGGER.info(
"authentication for user %s started.",
self.__username)
Expand All @@ -75,17 +82,17 @@ def _renew_sessionkey(self):

if not auth_response.ok:
_LOGGER.error(
"could not authenticate at KIWI:\n%s",
auth_response.json())
"could not authenticate at KIWI:\n%s",
auth_response.json())

raise ValueError("could not authenticate")

self.__session_key = auth_response.json()['result']['session_key']
self.__session_expires = dateutil.parser.parse(
auth_response.json()['result']['session']['expires'])
auth_response.json()['result']['session']['expires'])

def get_locks(self):
"""return a list of kiwi locks"""
"""Return a list of kiwi locks."""
self._with_valid_session()
sensor_list = requests.get(
API_LIST_DOOR_URL,
Expand All @@ -100,7 +107,7 @@ def get_locks(self):
return doors

def open_door(self, door_id):
"""open the kiwi door lock"""
"""Open the kiwi door lock."""
self._with_valid_session()
open_response = requests.post(
API_OPEN_DOOR_URL.format(door_id),
Expand All @@ -118,14 +125,14 @@ def __init__(self, kiwi_lock, client):
self._sensor = kiwi_lock
self._device_attrs = None
self._client = client
self.id = kiwi_lock['sensor_id']
self.lock_id = kiwi_lock['sensor_id']

address = kiwi_lock.get('address')
lat = address.pop('lat', None)
lng = address.pop('lng', None)

self._device_attrs = {
ATTR_ID: self.id,
ATTR_ID: self.lock_id,
ATTR_TYPE: kiwi_lock.get('hardware_type'),
ATTR_PERMISSION: kiwi_lock.get('highest_permission'),
ATTR_CAN_INVITE: kiwi_lock.get('can_invite')}
Expand Down Expand Up @@ -155,6 +162,5 @@ def device_state_attributes(self):

def unlock(self, **kwargs):
"""Unlock the device."""
if not self._client.open_door(self.id):
if not self._client.open_door(self.lock_id):
_LOGGER.error("failed to open door")

3 changes: 3 additions & 0 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,9 @@ python-blockchain-api==0.0.2
# homeassistant.components.media_player.clementine
python-clementine-remote==1.0.1

# homeassistant.components.lock.kiwi
python-dateutil==2.6.1

# homeassistant.components.digital_ocean
python-digitalocean==1.13.2

Expand Down

0 comments on commit 6db0fb2

Please sign in to comment.