Skip to content

Commit 0c8e30d

Browse files
committed
Merging most recent changes to core repo into custom-component repo
1 parent e5f9e8a commit 0c8e30d

25 files changed

+649
-813
lines changed

custom_components/powersensor/AsyncSet.py

Lines changed: 0 additions & 55 deletions
This file was deleted.

custom_components/powersensor/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
RT_VHH,
2121
RT_ZEROCONF,
2222
)
23-
from .PowersensorDiscoveryService import PowersensorDiscoveryService
24-
from .PowersensorMessageDispatcher import PowersensorMessageDispatcher
23+
from .powersensor_discovery_service import PowersensorDiscoveryService
24+
from .powersensor_message_dispatcher import PowersensorMessageDispatcher
2525

2626
_LOGGER = logging.getLogger(__name__)
2727

@@ -55,7 +55,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
5555
manifest = integration.manifest
5656

5757
try:
58-
# Establish the zeroconf discovery service
58+
# Create the zeroconf discovery service
5959
zeroconf_domain: str = str(manifest["zeroconf"][0])
6060
zeroconf_service = PowersensorDiscoveryService(hass, zeroconf_domain)
6161
await zeroconf_service.start()

custom_components/powersensor/config_flow.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class PowersensorConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
3232
"""Handle a config flow."""
3333

3434
VERSION = 2
35+
MINOR_VERSION = 0
3536

3637
def __init__(self) -> None:
3738
"""Initialize the config flow."""
@@ -42,15 +43,11 @@ async def async_step_reconfigure(
4243
"""Handle reconfigure step. The primary use case is adding missing roles to sensors."""
4344
entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
4445
if entry is None or not hasattr(entry, "runtime_data"):
45-
return self.async_abort(
46-
reason="Cannot reconfigure. Initial configuration incomplete or broken."
47-
)
46+
return self.async_abort(reason="cannot_reconfigure")
4847

4948
dispatcher = entry.runtime_data[RT_DISPATCHER]
5049
if dispatcher is None:
51-
return self.async_abort(
52-
reason="Cannot reconfigure. Initial configuration incomplete or broken."
53-
)
50+
return self.async_abort(reason="cannot_reconfigure")
5451

5552
mac2name = {mac: SENSOR_NAME_FORMAT % mac for mac in dispatcher.sensors}
5653

@@ -63,7 +60,7 @@ async def async_step_reconfigure(
6360
role = None
6461
_LOGGER.debug("Applying %s to %s", role, mac)
6562
async_dispatcher_send(self.hass, ROLE_UPDATE_SIGNAL, mac, role)
66-
return self.async_abort(reason="Roles successfully applied!")
63+
return self.async_abort(reason="roles_applied")
6764

6865
sensor_roles = {}
6966
description_placeholders = {}
@@ -131,14 +128,16 @@ async def async_step_user(
131128
self, user_input: dict[str, Any] | None = None
132129
) -> ConfigFlowResult:
133130
"""Handle zeroconf discovery."""
134-
await self._common_setup()
131+
if result := await self._common_setup():
132+
return result
135133
return await self.async_step_manual_confirm()
136134

137135
async def async_step_zeroconf(
138136
self, discovery_info: zeroconf.ZeroconfServiceInfo
139137
) -> ConfigFlowResult:
140138
"""Handle zeroconf discovery."""
141-
await self._common_setup()
139+
if result := await self._common_setup():
140+
return result
142141
discovered_plugs_key = "discovered_plugs"
143142
host = discovery_info.host
144143
port = discovery_info.port or DEFAULT_PORT
@@ -147,7 +146,7 @@ async def async_step_zeroconf(
147146
if "id" in properties:
148147
mac = properties["id"].strip()
149148
else:
150-
return self.async_abort(reason="Plug firmware not compatible")
149+
return self.async_abort(reason="firmware_not_compatible")
151150

152151
display_name = f"🔌 Mac({mac})"
153152
plug_data = {
@@ -168,9 +167,12 @@ async def async_step_zeroconf(
168167
async def async_step_confirm(
169168
self, step_id: str, user_input: dict[str, Any] | None = None
170169
) -> ConfigFlowResult:
171-
"""Confirm user wants to add the powersensor integration with the plugs stored in hass.data['powersensor']."""
170+
"Confirm user wants to add the powersensor integration with the plugs stored in hass.data['powersensor']."
172171
if user_input is not None:
173-
_LOGGER.debug(self.hass.data[DOMAIN]["discovered_plugs"])
172+
_LOGGER.debug(
173+
"Creating entry with discovered plugs: %s",
174+
self.hass.data[DOMAIN]["discovered_plugs"],
175+
)
174176
return self.async_create_entry(
175177
title="Powersensor",
176178
data={
@@ -183,15 +185,15 @@ async def async_step_confirm(
183185
async def async_step_discovery_confirm(
184186
self, user_input: dict[str, Any] | None = None
185187
) -> ConfigFlowResult:
186-
"""Confirm user wants to add the powersensor integration with the plugs discovered."""
188+
"Confirm user wants to add the powersensor integration with the plugs discovered."
187189
return await self.async_step_confirm(
188190
step_id="discovery_confirm", user_input=user_input
189191
)
190192

191193
async def async_step_manual_confirm(
192194
self, user_input: dict[str, Any] | None = None
193195
) -> ConfigFlowResult:
194-
"""Confirm user wants to add the powersensor integration with manual configuration (typically no plugs available)."""
196+
"Confirm user wants to add the powersensor integration with manual configuration (typically no plugs available)."
195197
return await self.async_step_confirm(
196198
step_id="manual_confirm", user_input=user_input
197199
)

custom_components/powersensor/const.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
DOMAIN = "powersensor"
44
DEFAULT_NAME = "Powersensor"
55
DEFAULT_PORT = 49476
6-
DEFAULT_SCAN_INTERVAL = 30
76

87
# Internal signals
98
CREATE_PLUG_SIGNAL = f"{DOMAIN}_create_plug"
@@ -18,7 +17,7 @@
1817
ZEROCONF_UPDATE_PLUG_SIGNAL = f"{DOMAIN}_zeroconf_update_plug"
1918

2019
# Formatting, would've liked to have been able to have this translatable
21-
SENSOR_NAME_FORMAT = "Powersensor Sensor (ID: %s)"
20+
SENSOR_NAME_FORMAT = "Powersensor Sensor (ID: %s)"
2221

2322
# Config entry keys
2423
CFG_DEVICES = "devices"

custom_components/powersensor/manifest.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"config_flow": true,
99
"dependencies": [],
1010
"documentation": "https://dius.github.io/homeassistant-powersensor/",
11-
"homekit": {},
1211
"integration_type" : "hub",
1312
"iot_class": "local_push",
1413
"issue_tracker": "https://github.com/DiUS/homeassistant-powersensor/issues",

custom_components/powersensor/PowersensorDiscoveryService.py renamed to custom_components/powersensor/powersensor_discovery_service.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ async def stop(self):
189189
with suppress(asyncio.CancelledError):
190190
await self._task
191191

192+
if self.browser is not None:
193+
self.browser.cancel()
194+
192195
if self.zc:
193196
# self.zc.close()
194197
self.zc = None

custom_components/powersensor/PowersensorMessageDispatcher.py renamed to custom_components/powersensor/powersensor_message_dispatcher.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
async_dispatcher_send,
1919
)
2020

21-
from .AsyncSet import AsyncSet
2221
from .const import (
2322
# Used config entry fields
2423
CFG_ROLES,
@@ -40,7 +39,9 @@
4039

4140
async def _handle_exception(event: str, exc: BaseException):
4241
"""Log errors when PlugApi throws an exception."""
43-
_LOGGER.error("On event %s Plug connection reported exception: %s", event, exc)
42+
_LOGGER.exception(
43+
"On event %s Plug connection reported exception: %s", event, exc
44+
)
4445

4546

4647
def _filter_unknown(role: str):
@@ -98,13 +99,13 @@ def __init__(
9899

99100
self._monitor_add_plug_queue = None
100101
self._stop_task = False
101-
self._plug_added_queue: AsyncSet = AsyncSet()
102+
self._plug_added_queue: set = set()
102103
self._safe_to_process_plug_queue = False
103104

104105
async def enqueue_plug_for_adding(self, network_info: dict):
105106
"""On receiving zeroconf data this info is added to processing buffer to await creation of entity and api."""
106107
_LOGGER.debug("Adding to plug processing queue: %s", network_info)
107-
await self._plug_added_queue.add(
108+
self._plug_added_queue.add(
108109
(
109110
network_info["mac"],
110111
network_info["host"],
@@ -134,7 +135,7 @@ async def _monitor_plug_queue(self):
134135
"""The actual background task loop."""
135136
try:
136137
while not self._stop_task and self._plug_added_queue:
137-
queue_snapshot = await self._plug_added_queue.copy()
138+
queue_snapshot = self._plug_added_queue.copy()
138139
for mac_address, host, port, name in queue_snapshot:
139140
# @todo: maybe better to query the entity registry?
140141
if not self._plug_has_been_seen(mac_address, name):
@@ -162,7 +163,7 @@ async def _monitor_plug_queue(self):
162163
" Skipping and flushing from queue. ",
163164
mac_address,
164165
)
165-
await self._plug_added_queue.remove(
166+
self._plug_added_queue.remove(
166167
(mac_address, host, port, name)
167168
)
168169

@@ -185,9 +186,10 @@ def _get_role_info(self, message):
185186
"""Retrieve the effective role and persisted role for this message."""
186187
# Filter in case older version stuck an "unknown" in there
187188
persisted_role = _filter_unknown(
188-
self._entry.data.get(CFG_ROLES, {}).get(message['mac'], None))
189+
self._entry.data.get(CFG_ROLES, {}).get(message["mac"], None)
190+
)
189191
# The sensor *does* send "unknown", not null/None, so filter it
190-
role = _filter_unknown(message.get('role', None))
192+
role = _filter_unknown(message.get("role", None))
191193
return role, persisted_role
192194

193195
async def stop_processing_plug_queue(self):
@@ -266,7 +268,9 @@ async def handle_relaying_for(self, event: str, message: dict):
266268
# We only apply a known persisted role, so we don't clobber a sensor's
267269
# actual knowledge.
268270
if persisted_role is not None and role != persisted_role:
269-
_LOGGER.debug("Restoring role for %s from %s to %s", mac, role, persisted_role)
271+
_LOGGER.debug(
272+
"Restoring role for %s from %s to %s", mac, role, persisted_role
273+
)
270274
async_dispatcher_send(self._hass, ROLE_UPDATE_SIGNAL, mac, persisted_role)
271275

272276
async def handle_message(self, event: str, message: dict):
@@ -279,9 +283,9 @@ async def handle_message(self, event: str, message: dict):
279283
role, persisted_role = self._get_role_info(message)
280284

281285
# Apply persisted role information if necessary
282-
message['role'] = persisted_role if role is None else role
286+
message["role"] = persisted_role if role is None else role
283287

284-
# Uknown roles from the sensor should not be allowed to overwrite
288+
# Unknown roles from the sensor should not be allowed to overwrite
285289
# any persisted roles
286290
if role is not None and role != persisted_role:
287291
self.sensors[mac] = role
@@ -327,7 +331,7 @@ async def _acknowledge_plug_added_to_homeassistant(
327331
self, mac_address, host, port, name
328332
):
329333
self._create_api(mac_address, host, port, name)
330-
await self._plug_added_queue.remove((mac_address, host, port, name))
334+
self._plug_added_queue.remove((mac_address, host, port, name))
331335

332336
async def _plug_added(self, info):
333337
_LOGGER.debug(" Request to add plug received: %s", info)

0 commit comments

Comments
 (0)