Store Axis device with entry_id key rather than unique_id (#85673)

* Store Axis device with entry_id key rather than unique_id

* Fix review comments
This commit is contained in:
Robert Svensson 2023-01-13 22:21:40 +01:00 committed by GitHub
parent c191daedc3
commit be899b6ab6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 27 additions and 33 deletions

View file

@ -25,7 +25,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
raise ConfigEntryAuthFailed from err
device = AxisNetworkDevice(hass, config_entry, api)
hass.data[AXIS_DOMAIN][config_entry.unique_id] = device
hass.data[AXIS_DOMAIN][config_entry.entry_id] = device
await device.async_update_device_registry()
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
device.async_setup_events()
@ -40,7 +40,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Unload Axis device config entry."""
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN].pop(config_entry.unique_id)
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN].pop(config_entry.entry_id)
return await device.async_reset()

View file

@ -48,7 +48,7 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up a Axis binary sensor."""
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN][config_entry.unique_id]
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN][config_entry.entry_id]
@callback
def async_create_entity(event: Event) -> None:

View file

@ -22,7 +22,7 @@ async def async_setup_entry(
"""Set up the Axis camera video stream."""
filter_urllib3_logging()
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN][config_entry.unique_id]
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN][config_entry.entry_id]
if not device.api.vapix.params.image_format:
return

View file

@ -232,7 +232,7 @@ class AxisOptionsFlowHandler(config_entries.OptionsFlowWithConfigEntry):
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Manage the Axis device options."""
self.device = self.hass.data[AXIS_DOMAIN][self.config_entry.unique_id]
self.device = self.hass.data[AXIS_DOMAIN][self.config_entry.entry_id]
return await self.async_step_configure_stream()
async def async_step_configure_stream(

View file

@ -157,7 +157,7 @@ class AxisNetworkDevice:
This is a static method because a class method (bound method),
can not be used with weak references.
"""
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN][entry.unique_id]
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN][entry.entry_id]
device.api.config.host = device.host
async_dispatcher_send(hass, device.signal_new_address)

View file

@ -20,7 +20,7 @@ async def async_get_config_entry_diagnostics(
hass: HomeAssistant, config_entry: ConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN][config_entry.unique_id]
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN][config_entry.entry_id]
diag: dict[str, Any] = {}
diag["config"] = async_redact_data(config_entry.as_dict(), REDACT_CONFIG)

View file

@ -19,7 +19,7 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up a Axis light."""
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN][config_entry.unique_id]
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN][config_entry.entry_id]
if (
device.api.vapix.light_control is None

View file

@ -19,7 +19,7 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up a Axis switch."""
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN][config_entry.unique_id]
device: AxisNetworkDevice = hass.data[AXIS_DOMAIN][config_entry.entry_id]
@callback
def async_create_entity(event: Event) -> None:

View file

@ -82,7 +82,7 @@ async def test_flow_manual_configuration(hass):
async def test_manual_configuration_update_configuration(hass):
"""Test that config flow fails on already configured device."""
config_entry = await setup_axis_integration(hass)
device = hass.data[AXIS_DOMAIN][config_entry.unique_id]
device = hass.data[AXIS_DOMAIN][config_entry.entry_id]
result = await hass.config_entries.flow.async_init(
AXIS_DOMAIN, context={"source": SOURCE_USER}
@ -214,7 +214,7 @@ async def test_flow_create_entry_multiple_existing_entries_of_same_model(hass):
async def test_reauth_flow_update_configuration(hass):
"""Test that config flow fails on already configured device."""
config_entry = await setup_axis_integration(hass)
device = hass.data[AXIS_DOMAIN][config_entry.unique_id]
device = hass.data[AXIS_DOMAIN][config_entry.entry_id]
result = await hass.config_entries.flow.async_init(
AXIS_DOMAIN,
@ -576,15 +576,13 @@ async def test_discovery_flow_ignore_link_local_address(
async def test_option_flow(hass):
"""Test config flow options."""
config_entry = await setup_axis_integration(hass)
device = hass.data[AXIS_DOMAIN][config_entry.unique_id]
device = hass.data[AXIS_DOMAIN][config_entry.entry_id]
assert device.option_stream_profile == DEFAULT_STREAM_PROFILE
assert device.option_video_source == DEFAULT_VIDEO_SOURCE
with respx.mock:
mock_default_vapix_requests(respx)
result = await hass.config_entries.options.async_init(
device.config_entry.entry_id
)
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] == FlowResultType.FORM
assert result["step_id"] == "configure_stream"

View file

@ -302,20 +302,18 @@ async def test_device_setup(hass):
return_value=True,
) as forward_entry_setup:
config_entry = await setup_axis_integration(hass)
device = hass.data[AXIS_DOMAIN][config_entry.unique_id]
device = hass.data[AXIS_DOMAIN][config_entry.entry_id]
assert device.api.vapix.firmware_version == "9.10.1"
assert device.api.vapix.product_number == "M1065-LW"
assert device.api.vapix.product_type == "Network Camera"
assert device.api.vapix.serial_number == "00408C123456"
entry = device.config_entry
assert len(forward_entry_setup.mock_calls) == 4
assert forward_entry_setup.mock_calls[0][1] == (entry, "binary_sensor")
assert forward_entry_setup.mock_calls[1][1] == (entry, "camera")
assert forward_entry_setup.mock_calls[2][1] == (entry, "light")
assert forward_entry_setup.mock_calls[3][1] == (entry, "switch")
assert forward_entry_setup.mock_calls[0][1] == (config_entry, "binary_sensor")
assert forward_entry_setup.mock_calls[1][1] == (config_entry, "camera")
assert forward_entry_setup.mock_calls[2][1] == (config_entry, "light")
assert forward_entry_setup.mock_calls[3][1] == (config_entry, "switch")
assert device.host == ENTRY_CONFIG[CONF_HOST]
assert device.model == ENTRY_CONFIG[CONF_MODEL]
@ -337,7 +335,7 @@ async def test_device_info(hass):
with patch.dict(API_DISCOVERY_RESPONSE, api_discovery):
config_entry = await setup_axis_integration(hass)
device = hass.data[AXIS_DOMAIN][config_entry.unique_id]
device = hass.data[AXIS_DOMAIN][config_entry.entry_id]
assert device.api.vapix.firmware_version == "9.80.1"
assert device.api.vapix.product_number == "M1065-LW"
@ -371,7 +369,7 @@ async def test_device_support_mqtt(hass, mqtt_mock):
async def test_update_address(hass):
"""Test update address works."""
config_entry = await setup_axis_integration(hass)
device = hass.data[AXIS_DOMAIN][config_entry.unique_id]
device = hass.data[AXIS_DOMAIN][config_entry.entry_id]
assert device.api.config.host == "1.2.3.4"
with patch(
@ -435,7 +433,7 @@ async def test_device_unavailable(hass, mock_rtsp_event, mock_rtsp_signal_state)
async def test_device_reset(hass):
"""Successfully reset device."""
config_entry = await setup_axis_integration(hass)
device = hass.data[AXIS_DOMAIN][config_entry.unique_id]
device = hass.data[AXIS_DOMAIN][config_entry.entry_id]
result = await device.async_reset()
assert result is True

View file

@ -4,10 +4,9 @@ from unittest.mock import AsyncMock, Mock, patch
from homeassistant.components import axis
from homeassistant.components.axis.const import DOMAIN as AXIS_DOMAIN
from homeassistant.const import CONF_MAC
from homeassistant.helpers.device_registry import format_mac
from homeassistant.setup import async_setup_component
from .test_device import MAC, setup_axis_integration
from .test_device import setup_axis_integration
from tests.common import MockConfigEntry
@ -20,9 +19,9 @@ async def test_setup_no_config(hass):
async def test_setup_entry(hass):
"""Test successful setup of entry."""
await setup_axis_integration(hass)
config_entry = await setup_axis_integration(hass)
assert len(hass.data[AXIS_DOMAIN]) == 1
assert format_mac(MAC) in hass.data[AXIS_DOMAIN]
assert config_entry.entry_id in hass.data[AXIS_DOMAIN]
async def test_setup_entry_fails(hass):
@ -44,10 +43,9 @@ async def test_setup_entry_fails(hass):
async def test_unload_entry(hass):
"""Test successful unload of entry."""
config_entry = await setup_axis_integration(hass)
device = hass.data[AXIS_DOMAIN][config_entry.unique_id]
assert hass.data[AXIS_DOMAIN]
assert await hass.config_entries.async_unload(device.config_entry.entry_id)
assert await hass.config_entries.async_unload(config_entry.entry_id)
assert not hass.data[AXIS_DOMAIN]

View file

@ -41,7 +41,7 @@ async def test_no_switches(hass):
async def test_switches_with_port_cgi(hass, mock_rtsp_event):
"""Test that switches are loaded properly using port.cgi."""
config_entry = await setup_axis_integration(hass)
device = hass.data[AXIS_DOMAIN][config_entry.unique_id]
device = hass.data[AXIS_DOMAIN][config_entry.entry_id]
device.api.vapix.ports = {"0": AsyncMock(), "1": AsyncMock()}
device.api.vapix.ports["0"].name = "Doorbell"
@ -103,7 +103,7 @@ async def test_switches_with_port_management(
with patch.dict(API_DISCOVERY_RESPONSE, api_discovery):
config_entry = await setup_axis_integration(hass)
device = hass.data[AXIS_DOMAIN][config_entry.unique_id]
device = hass.data[AXIS_DOMAIN][config_entry.entry_id]
device.api.vapix.ports = {"0": AsyncMock(), "1": AsyncMock()}
device.api.vapix.ports["0"].name = "Doorbell"