Skip to content

Commit

Permalink
Remove not needed name from yale_smart_alarm (#132204)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjohansson-ST authored Dec 6, 2024
1 parent 9771998 commit 4023994
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 120 deletions.
12 changes: 8 additions & 4 deletions homeassistant/components/yale_smart_alarm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from homeassistant.components.lock import CONF_DEFAULT_CODE, DOMAIN as LOCK_DOMAIN
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_CODE
from homeassistant.const import CONF_CODE, CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er

Expand Down Expand Up @@ -42,6 +42,7 @@ async def async_migrate_entry(hass: HomeAssistant, entry: YaleConfigEntry) -> bo
LOGGER.debug("Migrating from version %s", entry.version)

if entry.version == 1:
new_options = entry.options.copy()
if config_entry_default_code := entry.options.get(CONF_CODE):
entity_reg = er.async_get(hass)
entries = er.async_entries_for_config_entry(entity_reg, entry.entry_id)
Expand All @@ -52,12 +53,15 @@ async def async_migrate_entry(hass: HomeAssistant, entry: YaleConfigEntry) -> bo
LOCK_DOMAIN,
{CONF_DEFAULT_CODE: config_entry_default_code},
)
new_options = entry.options.copy()
del new_options[CONF_CODE]

hass.config_entries.async_update_entry(entry, options=new_options)
hass.config_entries.async_update_entry(entry, options=new_options, version=2)

hass.config_entries.async_update_entry(entry, version=2)
if entry.version == 2 and entry.minor_version == 1:
# Removes name from entry data
new_data = entry.data.copy()
del new_data[CONF_NAME]
hass.config_entries.async_update_entry(entry, data=new_data, minor_version=2)

LOGGER.debug("Migration to version %s successful", entry.version)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
AlarmControlPanelEntityFeature,
AlarmControlPanelState,
)
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand Down Expand Up @@ -84,7 +83,7 @@ async def async_set_alarm(self, command: str, code: str | None = None) -> None:
translation_domain=DOMAIN,
translation_key="set_alarm",
translation_placeholders={
"name": self.coordinator.config_entry.data[CONF_NAME],
"name": self.coordinator.config_entry.title,
"error": str(error),
},
) from error
Expand Down
6 changes: 2 additions & 4 deletions homeassistant/components/yale_smart_alarm/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
ConfigFlowResult,
OptionsFlow,
)
from homeassistant.const import CONF_NAME, CONF_PASSWORD, CONF_USERNAME
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv

from .const import (
CONF_AREA_ID,
CONF_LOCK_CODE_DIGITS,
DEFAULT_AREA_ID,
DEFAULT_NAME,
DOMAIN,
YALE_BASE_ERRORS,
)
Expand Down Expand Up @@ -67,6 +66,7 @@ class YaleConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Yale integration."""

VERSION = 2
MINOR_VERSION = 2

@staticmethod
@callback
Expand Down Expand Up @@ -146,7 +146,6 @@ async def async_step_user(
if user_input is not None:
username = user_input[CONF_USERNAME]
password = user_input[CONF_PASSWORD]
name = DEFAULT_NAME
area = user_input.get(CONF_AREA_ID, DEFAULT_AREA_ID)

errors = await self.hass.async_add_executor_job(
Expand All @@ -161,7 +160,6 @@ async def async_step_user(
data={
CONF_USERNAME: username,
CONF_PASSWORD: password,
CONF_NAME: name,
CONF_AREA_ID: area,
},
)
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/yale_smart_alarm/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from yalesmartalarmclient import YaleLock

from homeassistant.const import CONF_NAME, CONF_USERNAME
from homeassistant.const import CONF_USERNAME
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import CoordinatorEntity
Expand Down Expand Up @@ -61,7 +61,7 @@ def __init__(self, coordinator: YaleDataUpdateCoordinator) -> None:
identifiers={(DOMAIN, coordinator.config_entry.data[CONF_USERNAME])},
manufacturer=MANUFACTURER,
model=MODEL,
name=coordinator.config_entry.data[CONF_NAME],
name=coordinator.config_entry.title,
connections={(CONNECTION_NETWORK_MAC, panel_info["mac"])},
sw_version=panel_info["version"],
)
66 changes: 39 additions & 27 deletions tests/components/yale_smart_alarm/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
ENTRY_CONFIG = {
"username": "test-username",
"password": "new-test-password",
"name": "Yale Smart Alarm",
"area_id": "1",
}
OPTIONS_CONFIG = {"lock_code_digits": 6}
Expand All @@ -35,51 +34,64 @@ async def patch_platform_constant() -> list[Platform]:
@pytest.fixture
async def load_config_entry(
hass: HomeAssistant,
get_data: YaleSmartAlarmData,
get_all_data: YaleSmartAlarmData,
get_client: Mock,
load_platforms: list[Platform],
) -> tuple[MockConfigEntry, Mock]:
"""Set up the Yale Smart Living integration in Home Assistant."""
with patch("homeassistant.components.yale_smart_alarm.PLATFORMS", load_platforms):
config_entry = MockConfigEntry(
title=ENTRY_CONFIG["username"],
domain=DOMAIN,
source=SOURCE_USER,
data=ENTRY_CONFIG,
options=OPTIONS_CONFIG,
entry_id="1",
unique_id="username",
version=1,
version=2,
minor_version=2,
)

config_entry.add_to_hass(hass)

cycle = get_data.cycle["data"]
data = {"data": cycle["device_status"]}

with patch(
"homeassistant.components.yale_smart_alarm.coordinator.YaleSmartAlarmClient",
autospec=True,
) as mock_client_class:
client = mock_client_class.return_value
client.auth = Mock()
client.auth.get_authenticated = Mock(return_value=data)
client.auth.post_authenticated = Mock(return_value={"code": "000"})
client.auth.put_authenticated = Mock(return_value={"code": "000"})
client.lock_api = YaleDoorManAPI(client.auth)
locks = [
YaleLock(device, lock_api=client.lock_api)
for device in cycle["device_status"]
if device["type"] == YaleLock.DEVICE_TYPE
]
client.get_locks.return_value = locks
client.get_all.return_value = get_all_data
client.get_information.return_value = get_data
client.get_armed_status.return_value = YALE_STATE_ARM_FULL

return_value=get_client,
):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

return (config_entry, client)
return (config_entry, get_client)


@pytest.fixture(name="get_client")
async def mock_client(
get_data: YaleSmartAlarmData,
get_all_data: YaleSmartAlarmData,
) -> Mock:
"""Mock the Yale client."""
cycle = get_data.cycle["data"]
data = {"data": cycle["device_status"]}

with patch(
"homeassistant.components.yale_smart_alarm.coordinator.YaleSmartAlarmClient",
autospec=True,
) as mock_client_class:
client = mock_client_class.return_value
client.auth = Mock()
client.auth.get_authenticated = Mock(return_value=data)
client.auth.post_authenticated = Mock(return_value={"code": "000"})
client.auth.put_authenticated = Mock(return_value={"code": "000"})
client.lock_api = YaleDoorManAPI(client.auth)
locks = [
YaleLock(device, lock_api=client.lock_api)
for device in cycle["device_status"]
if device["type"] == YaleLock.DEVICE_TYPE
]
client.get_locks.return_value = locks
client.get_all.return_value = get_all_data
client.get_information.return_value = get_data
client.get_armed_status.return_value = YALE_STATE_ARM_FULL

return client


@pytest.fixture(name="loaded_fixture", scope="package")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# serializer version: 1
# name: test_alarm_control_panel[load_platforms0][alarm_control_panel.yale_smart_alarm-entry]
# name: test_alarm_control_panel[load_platforms0][alarm_control_panel.test_username-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
Expand All @@ -11,7 +11,7 @@
'disabled_by': None,
'domain': 'alarm_control_panel',
'entity_category': None,
'entity_id': 'alarm_control_panel.yale_smart_alarm',
'entity_id': 'alarm_control_panel.test_username',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
Expand All @@ -32,17 +32,17 @@
'unit_of_measurement': None,
})
# ---
# name: test_alarm_control_panel[load_platforms0][alarm_control_panel.yale_smart_alarm-state]
# name: test_alarm_control_panel[load_platforms0][alarm_control_panel.test_username-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'changed_by': None,
'code_arm_required': False,
'code_format': None,
'friendly_name': 'Yale Smart Alarm',
'friendly_name': 'test-username',
'supported_features': <AlarmControlPanelEntityFeature: 3>,
}),
'context': <ANY>,
'entity_id': 'alarm_control_panel.yale_smart_alarm',
'entity_id': 'alarm_control_panel.test_username',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
Expand Down
40 changes: 20 additions & 20 deletions tests/components/yale_smart_alarm/snapshots/test_binary_sensor.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@
'state': 'off',
})
# ---
# name: test_binary_sensor[load_platforms0][binary_sensor.yale_smart_alarm_battery-entry]
# name: test_binary_sensor[load_platforms0][binary_sensor.test_username_battery-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
Expand All @@ -293,7 +293,7 @@
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'binary_sensor.yale_smart_alarm_battery',
'entity_id': 'binary_sensor.test_username_battery',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
Expand All @@ -314,21 +314,21 @@
'unit_of_measurement': None,
})
# ---
# name: test_binary_sensor[load_platforms0][binary_sensor.yale_smart_alarm_battery-state]
# name: test_binary_sensor[load_platforms0][binary_sensor.test_username_battery-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': 'Yale Smart Alarm Battery',
'friendly_name': 'test-username Battery',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.yale_smart_alarm_battery',
'entity_id': 'binary_sensor.test_username_battery',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_binary_sensor[load_platforms0][binary_sensor.yale_smart_alarm_jam-entry]
# name: test_binary_sensor[load_platforms0][binary_sensor.test_username_jam-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
Expand All @@ -340,7 +340,7 @@
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'binary_sensor.yale_smart_alarm_jam',
'entity_id': 'binary_sensor.test_username_jam',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
Expand All @@ -361,21 +361,21 @@
'unit_of_measurement': None,
})
# ---
# name: test_binary_sensor[load_platforms0][binary_sensor.yale_smart_alarm_jam-state]
# name: test_binary_sensor[load_platforms0][binary_sensor.test_username_jam-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': 'Yale Smart Alarm Jam',
'friendly_name': 'test-username Jam',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.yale_smart_alarm_jam',
'entity_id': 'binary_sensor.test_username_jam',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_binary_sensor[load_platforms0][binary_sensor.yale_smart_alarm_power_loss-entry]
# name: test_binary_sensor[load_platforms0][binary_sensor.test_username_power_loss-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
Expand All @@ -387,7 +387,7 @@
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'binary_sensor.yale_smart_alarm_power_loss',
'entity_id': 'binary_sensor.test_username_power_loss',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
Expand All @@ -408,21 +408,21 @@
'unit_of_measurement': None,
})
# ---
# name: test_binary_sensor[load_platforms0][binary_sensor.yale_smart_alarm_power_loss-state]
# name: test_binary_sensor[load_platforms0][binary_sensor.test_username_power_loss-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': 'Yale Smart Alarm Power loss',
'friendly_name': 'test-username Power loss',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.yale_smart_alarm_power_loss',
'entity_id': 'binary_sensor.test_username_power_loss',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'off',
})
# ---
# name: test_binary_sensor[load_platforms0][binary_sensor.yale_smart_alarm_tamper-entry]
# name: test_binary_sensor[load_platforms0][binary_sensor.test_username_tamper-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
Expand All @@ -434,7 +434,7 @@
'disabled_by': None,
'domain': 'binary_sensor',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'binary_sensor.yale_smart_alarm_tamper',
'entity_id': 'binary_sensor.test_username_tamper',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
Expand All @@ -455,14 +455,14 @@
'unit_of_measurement': None,
})
# ---
# name: test_binary_sensor[load_platforms0][binary_sensor.yale_smart_alarm_tamper-state]
# name: test_binary_sensor[load_platforms0][binary_sensor.test_username_tamper-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'problem',
'friendly_name': 'Yale Smart Alarm Tamper',
'friendly_name': 'test-username Tamper',
}),
'context': <ANY>,
'entity_id': 'binary_sensor.yale_smart_alarm_tamper',
'entity_id': 'binary_sensor.test_username_tamper',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
Expand Down
Loading

0 comments on commit 4023994

Please sign in to comment.