mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
Fix Nuki issues (#26689)
* Fix Nuki issues * remove stale code * Add comments * Fix lint
This commit is contained in:
parent
1b57ea51be
commit
6853f99e30
@ -195,7 +195,7 @@ homeassistant/components/notify/* @home-assistant/core
|
|||||||
homeassistant/components/notion/* @bachya
|
homeassistant/components/notion/* @bachya
|
||||||
homeassistant/components/nsw_fuel_station/* @nickw444
|
homeassistant/components/nsw_fuel_station/* @nickw444
|
||||||
homeassistant/components/nsw_rural_fire_service_feed/* @exxamalte
|
homeassistant/components/nsw_rural_fire_service_feed/* @exxamalte
|
||||||
homeassistant/components/nuki/* @pschmitt
|
homeassistant/components/nuki/* @pvizeli
|
||||||
homeassistant/components/nws/* @MatthewFlamm
|
homeassistant/components/nws/* @MatthewFlamm
|
||||||
homeassistant/components/obihai/* @dshokouhi
|
homeassistant/components/obihai/* @dshokouhi
|
||||||
homeassistant/components/ohmconnect/* @robbiet480
|
homeassistant/components/ohmconnect/* @robbiet480
|
||||||
|
@ -1 +1,3 @@
|
|||||||
"""The nuki component."""
|
"""The nuki component."""
|
||||||
|
|
||||||
|
DOMAIN = "nuki"
|
||||||
|
@ -1,20 +1,18 @@
|
|||||||
"""Nuki.io lock platform."""
|
"""Nuki.io lock platform."""
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
import requests
|
|
||||||
|
|
||||||
|
from pynuki import NukiBridge
|
||||||
|
from requests.exceptions import RequestException
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.lock import (
|
from homeassistant.components.lock import PLATFORM_SCHEMA, SUPPORT_OPEN, LockDevice
|
||||||
DOMAIN,
|
|
||||||
PLATFORM_SCHEMA,
|
|
||||||
LockDevice,
|
|
||||||
SUPPORT_OPEN,
|
|
||||||
)
|
|
||||||
from homeassistant.const import ATTR_ENTITY_ID, CONF_HOST, CONF_PORT, CONF_TOKEN
|
from homeassistant.const import ATTR_ENTITY_ID, CONF_HOST, CONF_PORT, CONF_TOKEN
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.service import extract_entity_ids
|
from homeassistant.helpers.service import extract_entity_ids
|
||||||
|
|
||||||
|
from . import DOMAIN
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEFAULT_PORT = 8080
|
DEFAULT_PORT = 8080
|
||||||
@ -30,7 +28,8 @@ MIN_TIME_BETWEEN_SCANS = timedelta(seconds=30)
|
|||||||
NUKI_DATA = "nuki"
|
NUKI_DATA = "nuki"
|
||||||
|
|
||||||
SERVICE_LOCK_N_GO = "lock_n_go"
|
SERVICE_LOCK_N_GO = "lock_n_go"
|
||||||
SERVICE_CHECK_CONNECTION = "check_connection"
|
|
||||||
|
ERROR_STATES = (0, 254, 255)
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
@ -47,48 +46,30 @@ LOCK_N_GO_SERVICE_SCHEMA = vol.Schema(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
CHECK_CONNECTION_SERVICE_SCHEMA = vol.Schema(
|
|
||||||
{vol.Optional(ATTR_ENTITY_ID): cv.entity_ids}
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the Nuki lock platform."""
|
"""Set up the Nuki lock platform."""
|
||||||
from pynuki import NukiBridge
|
|
||||||
|
|
||||||
bridge = NukiBridge(
|
bridge = NukiBridge(
|
||||||
config[CONF_HOST], config[CONF_TOKEN], config[CONF_PORT], DEFAULT_TIMEOUT
|
config[CONF_HOST], config[CONF_TOKEN], config[CONF_PORT], DEFAULT_TIMEOUT
|
||||||
)
|
)
|
||||||
add_entities([NukiLock(lock) for lock in bridge.locks])
|
devices = [NukiLock(lock) for lock in bridge.locks]
|
||||||
|
|
||||||
def service_handler(service):
|
def service_handler(service):
|
||||||
"""Service handler for nuki services."""
|
"""Service handler for nuki services."""
|
||||||
entity_ids = extract_entity_ids(hass, service)
|
entity_ids = extract_entity_ids(hass, service)
|
||||||
all_locks = hass.data[NUKI_DATA][DOMAIN]
|
unlatch = service.data[ATTR_UNLATCH]
|
||||||
target_locks = []
|
|
||||||
if not entity_ids:
|
for lock in devices:
|
||||||
target_locks = all_locks
|
if lock.entity_id not in entity_ids:
|
||||||
else:
|
continue
|
||||||
for lock in all_locks:
|
lock.lock_n_go(unlatch=unlatch)
|
||||||
if lock.entity_id in entity_ids:
|
|
||||||
target_locks.append(lock)
|
|
||||||
for lock in target_locks:
|
|
||||||
if service.service == SERVICE_LOCK_N_GO:
|
|
||||||
unlatch = service.data[ATTR_UNLATCH]
|
|
||||||
lock.lock_n_go(unlatch=unlatch)
|
|
||||||
elif service.service == SERVICE_CHECK_CONNECTION:
|
|
||||||
lock.check_connection()
|
|
||||||
|
|
||||||
hass.services.register(
|
hass.services.register(
|
||||||
"nuki", SERVICE_LOCK_N_GO, service_handler, schema=LOCK_N_GO_SERVICE_SCHEMA
|
DOMAIN, SERVICE_LOCK_N_GO, service_handler, schema=LOCK_N_GO_SERVICE_SCHEMA
|
||||||
)
|
|
||||||
hass.services.register(
|
|
||||||
"nuki",
|
|
||||||
SERVICE_CHECK_CONNECTION,
|
|
||||||
service_handler,
|
|
||||||
schema=CHECK_CONNECTION_SERVICE_SCHEMA,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_entities(devices)
|
||||||
|
|
||||||
|
|
||||||
class NukiLock(LockDevice):
|
class NukiLock(LockDevice):
|
||||||
"""Representation of a Nuki lock."""
|
"""Representation of a Nuki lock."""
|
||||||
@ -99,15 +80,7 @@ class NukiLock(LockDevice):
|
|||||||
self._locked = nuki_lock.is_locked
|
self._locked = nuki_lock.is_locked
|
||||||
self._name = nuki_lock.name
|
self._name = nuki_lock.name
|
||||||
self._battery_critical = nuki_lock.battery_critical
|
self._battery_critical = nuki_lock.battery_critical
|
||||||
self._available = nuki_lock.state != 255
|
self._available = nuki_lock.state not in ERROR_STATES
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
|
||||||
"""Call when entity is added to hass."""
|
|
||||||
if NUKI_DATA not in self.hass.data:
|
|
||||||
self.hass.data[NUKI_DATA] = {}
|
|
||||||
if DOMAIN not in self.hass.data[NUKI_DATA]:
|
|
||||||
self.hass.data[NUKI_DATA][DOMAIN] = []
|
|
||||||
self.hass.data[NUKI_DATA][DOMAIN].append(self)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -140,13 +113,19 @@ class NukiLock(LockDevice):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update the nuki lock properties."""
|
"""Update the nuki lock properties."""
|
||||||
try:
|
for level in (False, True):
|
||||||
self._nuki_lock.update(aggressive=False)
|
try:
|
||||||
except requests.exceptions.RequestException:
|
self._nuki_lock.update(aggressive=level)
|
||||||
self._available = False
|
except RequestException:
|
||||||
return
|
_LOGGER.warning("Network issues detect with %s", self.name)
|
||||||
|
self._available = False
|
||||||
|
return
|
||||||
|
|
||||||
|
# If in error state, we force an update and repoll data
|
||||||
|
self._available = self._nuki_lock.state not in ERROR_STATES
|
||||||
|
if self._available:
|
||||||
|
break
|
||||||
|
|
||||||
self._available = True
|
|
||||||
self._name = self._nuki_lock.name
|
self._name = self._nuki_lock.name
|
||||||
self._locked = self._nuki_lock.is_locked
|
self._locked = self._nuki_lock.is_locked
|
||||||
self._battery_critical = self._nuki_lock.battery_critical
|
self._battery_critical = self._nuki_lock.battery_critical
|
||||||
@ -170,12 +149,3 @@ class NukiLock(LockDevice):
|
|||||||
amount of time depending on the lock settings) and relock.
|
amount of time depending on the lock settings) and relock.
|
||||||
"""
|
"""
|
||||||
self._nuki_lock.lock_n_go(unlatch, kwargs)
|
self._nuki_lock.lock_n_go(unlatch, kwargs)
|
||||||
|
|
||||||
def check_connection(self, **kwargs):
|
|
||||||
"""Update the nuki lock properties."""
|
|
||||||
try:
|
|
||||||
self._nuki_lock.update(aggressive=True)
|
|
||||||
except requests.exceptions.RequestException:
|
|
||||||
self._available = False
|
|
||||||
else:
|
|
||||||
self._available = self._nuki_lock.state != 255
|
|
||||||
|
@ -2,11 +2,7 @@
|
|||||||
"domain": "nuki",
|
"domain": "nuki",
|
||||||
"name": "Nuki",
|
"name": "Nuki",
|
||||||
"documentation": "https://www.home-assistant.io/components/nuki",
|
"documentation": "https://www.home-assistant.io/components/nuki",
|
||||||
"requirements": [
|
"requirements": ["pynuki==1.3.3"],
|
||||||
"pynuki==1.3.3"
|
|
||||||
],
|
|
||||||
"dependencies": [],
|
"dependencies": [],
|
||||||
"codeowners": [
|
"codeowners": ["@pvizeli"]
|
||||||
"@pschmitt"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user