Skip to content

Commit 95e344e

Browse files
authored
Cleanup homelink (home-assistant#158477)
1 parent 7ed8613 commit 95e344e

3 files changed

Lines changed: 23 additions & 37 deletions

File tree

homeassistant/components/gentex_homelink/coordinator.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from dataclasses import dataclass
77
from functools import partial
88
import logging
9-
from typing import TYPE_CHECKING, TypedDict
9+
from typing import TypedDict
1010

1111
from homelink.model.device import Device
1212
from homelink.mqtt_provider import MQTTProvider
@@ -15,9 +15,6 @@
1515
from homeassistant.core import HomeAssistant, callback
1616
from homeassistant.util.ssl import get_default_context
1717

18-
if TYPE_CHECKING:
19-
from .event import HomeLinkEventEntity
20-
2118
_LOGGER = logging.getLogger(__name__)
2219

2320
type HomeLinkConfigEntry = ConfigEntry[HomeLinkData]
@@ -61,7 +58,6 @@ def __init__(
6158
self.config_entry = config_entry
6259
self.provider = provider
6360
self.device_data: list[Device] = []
64-
self.buttons: list[HomeLinkEventEntity] = []
6561
self._listeners: dict[str, EventCallback] = {}
6662

6763
@callback
@@ -76,7 +72,7 @@ def __async_remove_listener_internal(self, listener_id: str):
7672
del self._listeners[listener_id]
7773

7874
@callback
79-
def async_handle_state_data(self, data: dict[str, HomeLinkEventData]):
75+
def async_handle_state_data(self, data: dict[str, HomeLinkEventData]) -> None:
8076
"""Notify listeners."""
8177
for button_id, event in data.items():
8278
if listener := self._listeners.get(button_id):
@@ -86,7 +82,7 @@ async def async_config_entry_first_refresh(self) -> None:
8682
"""Refresh data for the first time when a config entry is setup."""
8783
await self._async_setup()
8884

89-
async def async_on_unload(self, _event):
85+
async def async_on_unload(self, _event) -> None:
9086
"""Disconnect and unregister when unloaded."""
9187
await self.provider.disable()
9288

@@ -100,10 +96,8 @@ async def discover_devices(self):
10096
"""Discover devices and build the Entities."""
10197
self.device_data = await self.provider.discover()
10298

103-
def on_message(
104-
self: HomeLinkCoordinator, _topic: str, message: HomeLinkMQTTMessage
105-
):
106-
"MQTT Callback function."
99+
def on_message(self, _topic: str, message: HomeLinkMQTTMessage) -> None:
100+
"""MQTT Callback function."""
107101
if message["type"] == "state":
108102
self.hass.add_job(self.async_handle_state_data, message["data"])
109103
if message["type"] == "requestSync":

homeassistant/components/gentex_homelink/event.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ async def async_setup_entry(
1919
) -> None:
2020
"""Add the entities for the binary sensor."""
2121
coordinator = config_entry.runtime_data.coordinator
22-
for device in coordinator.device_data:
23-
buttons = [
24-
HomeLinkEventEntity(b.id, b.name, device.id, device.name, coordinator)
25-
for b in device.buttons
26-
]
27-
coordinator.buttons.extend(buttons)
2822

29-
async_add_entities(coordinator.buttons)
23+
async_add_entities(
24+
HomeLinkEventEntity(button.id, button.name, device.id, device.name, coordinator)
25+
for device in coordinator.device_data
26+
for button in device.buttons
27+
)
3028

3129

3230
# Updates are centralized by the coordinator.
@@ -42,17 +40,17 @@ class HomeLinkEventEntity(EventEntity):
4240

4341
def __init__(
4442
self,
45-
id: str,
43+
button_id: str,
4644
param_name: str,
4745
device_id: str,
4846
device_name: str,
4947
coordinator: HomeLinkCoordinator,
5048
) -> None:
5149
"""Initialize the event entity."""
5250

53-
self.id: str = id
54-
self._attr_name: str = param_name
55-
self._attr_unique_id: str = id
51+
self.button_id = button_id
52+
self._attr_name = param_name
53+
self._attr_unique_id = button_id
5654
self._attr_device_info = DeviceInfo(
5755
identifiers={(DOMAIN, device_id)},
5856
name=device_name,
@@ -65,7 +63,7 @@ async def async_added_to_hass(self) -> None:
6563
await super().async_added_to_hass()
6664
self.async_on_remove(
6765
self.coordinator.async_add_event_listener(
68-
self._handle_event_data_update, self.id
66+
self._handle_event_data_update, self.button_id
6967
)
7068
)
7169

@@ -78,6 +76,3 @@ def _handle_event_data_update(self, update_data: HomeLinkEventData) -> None:
7876
self.last_request_id = update_data["requestId"]
7977

8078
self.async_write_ha_state()
81-
82-
async def async_update(self):
83-
"""Request early polling. Left intentionally blank because it's not possible in this implementation."""

homeassistant/components/gentex_homelink/oauth2.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
class SRPAuthImplementation(config_entry_oauth2_flow.AbstractOAuth2Implementation):
2222
"""Base class to abstract OAuth2 authentication."""
2323

24-
def __init__(self, hass: HomeAssistant, domain) -> None:
24+
def __init__(self, hass: HomeAssistant, domain: str) -> None:
2525
"""Initialize the SRP Auth implementation."""
2626

2727
self.hass = hass
@@ -45,16 +45,13 @@ async def async_generate_authorize_url(self, flow_id: str) -> str:
4545
async def async_resolve_external_data(self, external_data) -> dict:
4646
"""Format the token from the source appropriately for HomeAssistant."""
4747
tokens = external_data["tokens"]
48-
new_token = {}
49-
new_token["access_token"] = tokens["AuthenticationResult"]["AccessToken"]
50-
new_token["refresh_token"] = tokens["AuthenticationResult"]["RefreshToken"]
51-
new_token["token_type"] = tokens["AuthenticationResult"]["TokenType"]
52-
new_token["expires_in"] = tokens["AuthenticationResult"]["ExpiresIn"]
53-
new_token["expires_at"] = (
54-
time.time() + tokens["AuthenticationResult"]["ExpiresIn"]
55-
)
56-
57-
return new_token
48+
return {
49+
"access_token": tokens["AuthenticationResult"]["AccessToken"],
50+
"refresh_token": tokens["AuthenticationResult"]["RefreshToken"],
51+
"token_type": tokens["AuthenticationResult"]["TokenType"],
52+
"expires_in": tokens["AuthenticationResult"]["ExpiresIn"],
53+
"expires_at": (time.time() + tokens["AuthenticationResult"]["ExpiresIn"]),
54+
}
5855

5956
async def _token_request(self, data: dict) -> dict:
6057
"""Make a token request."""

0 commit comments

Comments
 (0)