Skip to content

Commit

Permalink
Adds fixes done on mainline
Browse files Browse the repository at this point in the history
  • Loading branch information
mikewoudenberg committed Jul 4, 2023
1 parent ebc0df8 commit 18e2b37
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 32 deletions.
9 changes: 6 additions & 3 deletions custom_components/loqed/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ async def validate_input(
)
cloud_client = cloud_loqed.LoqedCloudAPI(cloud_api_client)
lock_data = await cloud_client.async_get_locks()
except aiohttp.ClientError:
except aiohttp.ClientError as err:
_LOGGER.error("HTTP Connection error to loqed API")
raise CannotConnect from aiohttp.ClientError
raise CannotConnect from err

try:
selected_lock = next(
Expand Down Expand Up @@ -137,7 +137,10 @@ async def async_step_user(
errors["base"] = "invalid_auth"
else:
await self.async_set_unique_id(
re.sub(r"LOQED-([a-f0-9]+)\.local", r"\1", info["bridge_mdns_hostname"])
re.sub(
r"LOQED-([a-f0-9]+)\.local", r"\1", info["bridge_mdns_hostname"]
),
raise_on_progress=False,
)
self._abort_if_unique_id_configured()

Expand Down
2 changes: 0 additions & 2 deletions custom_components/loqed/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@


DOMAIN = "loqed"
OAUTH2_AUTHORIZE = "https://app.loqed.com/API/integration_oauth3/login.php"
OAUTH2_TOKEN = "https://app.loqed.com/API/integration_oauth3/token.php"
7 changes: 3 additions & 4 deletions custom_components/loqed/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

from homeassistant.components import webhook
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_WEBHOOK_ID
from homeassistant.core import HomeAssistant, callback
from homeassistant.const import CONF_NAME, CONF_WEBHOOK_ID
from homeassistant.core import HomeAssistant
from homeassistant.helpers.network import get_url
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

Expand Down Expand Up @@ -80,17 +80,16 @@ def __init__(
) -> None:
"""Initialize the Loqed Data Update coordinator."""
super().__init__(hass, _LOGGER, name="Loqed sensors")
self._hass = hass
self._api = api
self._entry = entry
self.lock = lock
self.device_name = self._entry.data[CONF_NAME]

async def _async_update_data(self) -> StatusMessage:
"""Fetch data from API endpoint."""
async with async_timeout.timeout(10):
return await self._api.async_get_lock_details()

@callback
async def _handle_webhook(
self, hass: HomeAssistant, webhook_id: str, request: Request
) -> None:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/loqed/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, coordinator: LoqedDataCoordinator) -> None:
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, lock_id)},
manufacturer="LOQED",
name="LOQED Lock",
name=coordinator.device_name,
model="Touch Smart Lock",
connections={(CONNECTION_NETWORK_MAC, lock_id)},
)
8 changes: 4 additions & 4 deletions custom_components/loqed/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,25 @@ async def async_setup_entry(
"""Set up the Loqed lock platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]

async_add_entities([LoqedLock(coordinator, entry.data["name"])])
async_add_entities([LoqedLock(coordinator)])


class LoqedLock(LoqedEntity, LockEntity):
"""Representation of a loqed lock."""

_attr_supported_features = LockEntityFeature.OPEN

def __init__(self, coordinator: LoqedDataCoordinator, name: str) -> None:
def __init__(self, coordinator: LoqedDataCoordinator) -> None:
"""Initialize the lock."""
super().__init__(coordinator)
self._lock = coordinator.lock
self._attr_unique_id = self._lock.id
self._attr_name = name
self._attr_name = None

@property
def changed_by(self) -> str:
"""Return internal ID of last used key."""
return "KeyID " + str(self._lock.last_key_id)
return f"KeyID {self._lock.last_key_id}"

@property
def is_locking(self) -> bool | None:
Expand Down
29 changes: 13 additions & 16 deletions custom_components/loqed/sensor.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
"""Creates LOQED sensors."""
from typing import Final

from .coordinator import LoqedDataCoordinator, StatusMessage
from .entity import LoqedEntity

from homeassistant.components.sensor import (
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
SensorDeviceClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, SIGNAL_STRENGTH_DECIBELS_MILLIWATT
from homeassistant.const import (
PERCENTAGE,
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
EntityCategory,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .const import DOMAIN
from .coordinator import LoqedDataCoordinator, StatusMessage
from .entity import LoqedEntity

SENSORS: Final[tuple[SensorEntityDescription, ...]] = (
SensorEntityDescription(
key="ble_strength",
name="Bluetooth signal",
translation_key="ble_strength",
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
state_class=SensorStateClass.MEASUREMENT,
Expand All @@ -30,9 +32,9 @@
),
SensorEntityDescription(
key="battery_percentage",
name="Battery level",
device_class=SensorDeviceClass.BATTERY,
state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC,
native_unit_of_measurement=PERCENTAGE,
),
)
Expand All @@ -44,24 +46,19 @@ async def async_setup_entry(
"""Set up the Loqed lock platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]

async_add_entities(LoqedSensor(coordinator, sensor, entry) for sensor in SENSORS)
async_add_entities(LoqedSensor(coordinator, sensor) for sensor in SENSORS)


class LoqedSensor(LoqedEntity, SensorEntity):
"""Representation of Sensor state."""

def __init__(
self,
coordinator: LoqedDataCoordinator,
description: SensorEntityDescription,
entry: ConfigEntry,
self, coordinator: LoqedDataCoordinator, description: SensorEntityDescription
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
self.entity_description = description
self.entry = entry

self._attr_unique_id = f"{entry.unique_id}_{description.key}"
self._attr_unique_id = f"{self.coordinator.lock.id}_{description.key}"

@property
def data(self) -> StatusMessage:
Expand Down
10 changes: 8 additions & 2 deletions custom_components/loqed/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@
},
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]",
"unknown": "[%key:common::config_flow::error::unknown%]"
"invalid_auth": "[%key:common::config_flow::error::invalid_auth%]"
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
}
},
"entity": {
"sensor": {
"ble_strength": {
"name": "Bluetooth signal"
}
}
}
}

0 comments on commit 18e2b37

Please sign in to comment.