Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ae6bc96

Browse files
authored
Shelly code quality (home-assistant#86733)
1 parent e4a7842 commit ae6bc96

File tree

9 files changed

+76
-306
lines changed

9 files changed

+76
-306
lines changed

‎homeassistant/components/shelly/button.py‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from .const import SHELLY_GAS_MODELS
2222
from .coordinator import ShellyBlockCoordinator, ShellyRpcCoordinator, get_entry_data
23-
from .utils import get_block_device_name, get_device_entry_gen, get_rpc_device_name
23+
from .utils import get_device_entry_gen
2424

2525
_ShellyCoordinatorT = TypeVar(
2626
"_ShellyCoordinatorT", bound=ShellyBlockCoordinator | ShellyRpcCoordinator
@@ -121,12 +121,7 @@ def __init__(
121121
super().__init__(coordinator)
122122
self.entity_description = description
123123

124-
if isinstance(coordinator, ShellyRpcCoordinator):
125-
device_name = get_rpc_device_name(coordinator.device)
126-
else:
127-
device_name = get_block_device_name(coordinator.device)
128-
129-
self._attr_name = f"{device_name} {description.name}"
124+
self._attr_name = f"{coordinator.device.name} {description.name}"
130125
self._attr_unique_id = slugify(self._attr_name)
131126
self._attr_device_info = DeviceInfo(
132127
connections={(CONNECTION_NETWORK_MAC, coordinator.mac)}

‎homeassistant/components/shelly/config_flow.py‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,11 @@
3333
)
3434
from .coordinator import async_reconnect_soon, get_entry_data
3535
from .utils import (
36-
get_block_device_name,
3736
get_block_device_sleep_period,
3837
get_coap_context,
3938
get_info_auth,
4039
get_info_gen,
4140
get_model_name,
42-
get_rpc_device_name,
4341
get_rpc_device_sleep_period,
4442
get_ws_context,
4543
mac_address_from_name,
@@ -80,7 +78,7 @@ async def validate_input(
8078
assert rpc_device.shelly
8179

8280
return {
83-
"title": get_rpc_device_name(rpc_device),
81+
"title": rpc_device.name,
8482
CONF_SLEEP_PERIOD: get_rpc_device_sleep_period(rpc_device.config),
8583
"model": rpc_device.shelly.get("model"),
8684
"gen": 2,
@@ -95,7 +93,7 @@ async def validate_input(
9593
)
9694
block_device.shutdown()
9795
return {
98-
"title": get_block_device_name(block_device),
96+
"title": block_device.name,
9997
CONF_SLEEP_PERIOD: get_block_device_sleep_period(block_device.settings),
10098
"model": block_device.model,
10199
"gen": 1,

‎homeassistant/components/shelly/coordinator.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
UPDATE_PERIOD_MULTIPLIER,
5252
BLEScannerMode,
5353
)
54-
from .utils import device_update_info, get_device_name, get_rpc_device_wakeup_period
54+
from .utils import device_update_info, get_rpc_device_wakeup_period
5555

5656
_DeviceT = TypeVar("_DeviceT", bound="BlockDevice|RpcDevice")
5757

@@ -86,7 +86,7 @@ def __init__(
8686
self.entry = entry
8787
self.device = device
8888
self.device_id: str | None = None
89-
device_name = get_device_name(device) if device.initialized else entry.title
89+
device_name = device.name if device.initialized else entry.title
9090
interval_td = timedelta(seconds=update_interval)
9191
super().__init__(hass, LOGGER, name=device_name, update_interval=interval_td)
9292

‎homeassistant/components/shelly/logbook.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
get_block_coordinator_by_device_id,
2222
get_rpc_coordinator_by_device_id,
2323
)
24-
from .utils import get_block_device_name, get_rpc_entity_name
24+
from .utils import get_rpc_entity_name
2525

2626

2727
@callback
@@ -48,8 +48,7 @@ def async_describe_shelly_click_event(event: EventType) -> dict[str, str]:
4848
elif click_type in BLOCK_INPUTS_EVENTS_TYPES:
4949
block_coordinator = get_block_coordinator_by_device_id(hass, device_id)
5050
if block_coordinator and block_coordinator.device.initialized:
51-
device_name = get_block_device_name(block_coordinator.device)
52-
input_name = f"{device_name} channel {channel}"
51+
input_name = f"{block_coordinator.device.name} channel {channel}"
5352

5453
return {
5554
LOGBOOK_ENTRY_NAME: "Shelly",

‎homeassistant/components/shelly/update.py‎

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
from homeassistant.exceptions import HomeAssistantError
2020
from homeassistant.helpers.entity import EntityCategory
2121
from homeassistant.helpers.entity_platform import AddEntitiesCallback
22-
from homeassistant.util import slugify
2322

2423
from .const import CONF_SLEEP_PERIOD
25-
from .coordinator import ShellyBlockCoordinator, ShellyRpcCoordinator, get_entry_data
24+
from .coordinator import ShellyBlockCoordinator, ShellyRpcCoordinator
2625
from .entity import (
2726
RestEntityDescription,
2827
RpcEntityDescription,
@@ -31,12 +30,7 @@
3130
async_setup_entry_rest,
3231
async_setup_entry_rpc,
3332
)
34-
from .utils import (
35-
async_remove_shelly_entity,
36-
get_block_device_name,
37-
get_device_entry_gen,
38-
get_rpc_device_name,
39-
)
33+
from .utils import get_device_entry_gen
4034

4135
LOGGER = logging.getLogger(__name__)
4236

@@ -123,28 +117,10 @@ async def async_setup_entry(
123117
) -> None:
124118
"""Set up update entities for Shelly component."""
125119
if get_device_entry_gen(config_entry) == 2:
126-
# Remove legacy update binary sensor & buttons, remove in 202320
127-
rpc_coordinator = get_entry_data(hass)[config_entry.entry_id].rpc
128-
assert rpc_coordinator
129-
mac = rpc_coordinator.mac
130-
async_remove_shelly_entity(hass, "binary_sensor", f"{mac}-sys-fwupdate")
131-
device_name = slugify(get_rpc_device_name(rpc_coordinator.device))
132-
async_remove_shelly_entity(hass, "button", f"{device_name}_ota_update")
133-
async_remove_shelly_entity(hass, "button", f"{device_name}_ota_update_beta")
134-
135120
return async_setup_entry_rpc(
136121
hass, config_entry, async_add_entities, RPC_UPDATES, RpcUpdateEntity
137122
)
138123

139-
# Remove legacy update binary sensor & buttons, remove in 202320
140-
block_coordinator = get_entry_data(hass)[config_entry.entry_id].block
141-
assert block_coordinator
142-
mac = block_coordinator.mac
143-
async_remove_shelly_entity(hass, "binary_sensor", f"{mac}-fwupdate")
144-
device_name = slugify(get_block_device_name(block_coordinator.device))
145-
async_remove_shelly_entity(hass, "button", f"{device_name}_ota_update")
146-
async_remove_shelly_entity(hass, "button", f"{device_name}_ota_update_beta")
147-
148124
if not config_entry.data[CONF_SLEEP_PERIOD]:
149125
async_setup_entry_rest(
150126
hass,

‎homeassistant/components/shelly/utils.py‎

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,6 @@ def async_remove_shelly_entity(
4949
entity_reg.async_remove(entity_id)
5050

5151

52-
def get_block_device_name(device: BlockDevice) -> str:
53-
"""Get Block device name."""
54-
return cast(str, device.settings["name"] or device.settings["device"]["hostname"])
55-
56-
57-
def get_rpc_device_name(device: RpcDevice) -> str:
58-
"""Get RPC device name."""
59-
return cast(str, device.config["sys"]["device"].get("name") or device.hostname)
60-
61-
62-
def get_device_name(device: BlockDevice | RpcDevice) -> str:
63-
"""Get device name."""
64-
if isinstance(device, BlockDevice):
65-
return get_block_device_name(device)
66-
67-
return get_rpc_device_name(device)
68-
69-
7052
def get_number_of_channels(device: BlockDevice, block: Block) -> int:
7153
"""Get number of channels for block type."""
7254
assert isinstance(device.shelly, dict)
@@ -105,7 +87,7 @@ def get_block_entity_name(
10587

10688
def get_block_channel_name(device: BlockDevice, block: Block | None) -> str:
10789
"""Get name based on device and channel name."""
108-
entity_name = get_block_device_name(device)
90+
entity_name = device.name
10991

11092
if (
11193
not block
@@ -306,7 +288,7 @@ def get_rpc_channel_name(device: RpcDevice, key: str) -> str:
306288
"""Get name based on device and channel name."""
307289
if device.config.get("switch:0"):
308290
key = key.replace("input", "switch")
309-
device_name = get_rpc_device_name(device)
291+
device_name = device.name
310292
entity_name: str | None = None
311293
if key in device.config:
312294
entity_name = device.config[key].get("name", device_name)

‎tests/components/shelly/conftest.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Test configuration for Shelly."""
22
from __future__ import annotations
33

4-
from unittest.mock import AsyncMock, Mock, patch
4+
from unittest.mock import AsyncMock, Mock, PropertyMock, patch
55

66
from aioshelly.block_device import BlockDevice
77
from aioshelly.rpc_device import RpcDevice, UpdateType
@@ -245,7 +245,9 @@ def update():
245245
status=MOCK_STATUS_COAP,
246246
firmware_version="some fw string",
247247
initialized=True,
248+
model="SHSW-1",
248249
)
250+
type(device).name = PropertyMock(return_value="Test name")
249251
block_device_mock.return_value = device
250252
block_device_mock.return_value.mock_update = Mock(side_effect=update)
251253

@@ -254,7 +256,7 @@ def update():
254256

255257
def _mock_rpc_device(version: str | None = None):
256258
"""Mock rpc (Gen2, Websocket) device."""
257-
return Mock(
259+
device= Mock(
258260
spec=RpcDevice,
259261
config=MOCK_CONFIG,
260262
event={},
@@ -265,6 +267,8 @@ def _mock_rpc_device(version: str | None = None):
265267
firmware_version="some fw string",
266268
initialized=True,
267269
)
270+
type(device).name = PropertyMock(return_value="Test name")
271+
return device
268272

269273

270274
@pytest.fixture

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /