Skip to content

Commit 1682ced

Browse files
authored
Bump pylamarzocco to 2.2.0 (home-assistant#156667)
1 parent 80b316b commit 1682ced

File tree

6 files changed

+100
-26
lines changed

6 files changed

+100
-26
lines changed

homeassistant/components/lamarzocco/__init__.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@
1515
from pylamarzocco.exceptions import AuthFail, RequestNotSuccessful
1616
from pylamarzocco.util import InstallationKey, generate_installation_key
1717

18-
from homeassistant.components.bluetooth import async_discovered_service_info
18+
from homeassistant.components.bluetooth import (
19+
async_ble_device_from_address,
20+
async_discovered_service_info,
21+
)
1922
from homeassistant.const import (
2023
CONF_MAC,
2124
CONF_PASSWORD,
2225
CONF_TOKEN,
2326
CONF_USERNAME,
27+
EVENT_HOMEASSISTANT_STOP,
2428
Platform,
2529
__version__,
2630
)
27-
from homeassistant.core import HomeAssistant
31+
from homeassistant.core import Event, HomeAssistant
2832
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
2933
from homeassistant.helpers import issue_registry as ir
3034
from homeassistant.helpers.aiohttp_client import async_create_clientsession
@@ -99,7 +103,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: LaMarzoccoConfigEntry) -
99103
# initialize Bluetooth
100104
bluetooth_client: LaMarzoccoBluetoothClient | None = None
101105
if entry.options.get(CONF_USE_BLUETOOTH, True) and (
102-
token := settings.ble_auth_token
106+
token := (entry.data.get(CONF_TOKEN) or settings.ble_auth_token)
103107
):
104108
if CONF_MAC not in entry.data:
105109
for discovery_info in async_discovered_service_info(hass):
@@ -108,7 +112,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: LaMarzoccoConfigEntry) -
108112
and name.startswith(BT_MODEL_PREFIXES)
109113
and name.split("_")[1] == serial
110114
):
111-
_LOGGER.debug("Found Bluetooth device, configuring with Bluetooth")
115+
_LOGGER.info("Found lamarzocco Bluetooth device, adding to entry")
112116
# found a device, add MAC address to config entry
113117
hass.config_entries.async_update_entry(
114118
entry,
@@ -118,22 +122,29 @@ async def async_setup_entry(hass: HomeAssistant, entry: LaMarzoccoConfigEntry) -
118122
},
119123
)
120124

121-
if not entry.data[CONF_TOKEN]:
122-
# update the token in the config entry
123-
hass.config_entries.async_update_entry(
124-
entry,
125-
data={
126-
**entry.data,
127-
CONF_TOKEN: token,
128-
},
129-
)
130-
131125
if CONF_MAC in entry.data:
132-
_LOGGER.debug("Initializing Bluetooth device")
133-
bluetooth_client = LaMarzoccoBluetoothClient(
134-
address_or_ble_device=entry.data[CONF_MAC],
135-
ble_token=token,
136-
)
126+
ble_device = async_ble_device_from_address(hass, entry.data[CONF_MAC])
127+
if ble_device:
128+
_LOGGER.info("Setting up lamarzocco with Bluetooth")
129+
bluetooth_client = LaMarzoccoBluetoothClient(
130+
ble_device=ble_device,
131+
ble_token=token,
132+
)
133+
134+
async def disconnect_bluetooth(_: Event) -> None:
135+
"""Stop push updates when hass stops."""
136+
await bluetooth_client.disconnect()
137+
138+
entry.async_on_unload(
139+
hass.bus.async_listen_once(
140+
EVENT_HOMEASSISTANT_STOP, disconnect_bluetooth
141+
)
142+
)
143+
entry.async_on_unload(bluetooth_client.disconnect)
144+
else:
145+
_LOGGER.info(
146+
"Bluetooth device not found during lamarzocco setup, continuing with cloud only"
147+
)
137148

138149
device = LaMarzoccoMachine(
139150
serial_number=entry.unique_id,

homeassistant/components/lamarzocco/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@
3737
"iot_class": "cloud_push",
3838
"loggers": ["pylamarzocco"],
3939
"quality_scale": "platinum",
40-
"requirements": ["pylamarzocco==2.1.3"]
40+
"requirements": ["pylamarzocco==2.2.0"]
4141
}

requirements_all.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

requirements_test_all.txt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/components/lamarzocco/conftest.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ def mock_bluetooth(enable_bluetooth: None) -> None:
147147
@pytest.fixture
148148
def mock_ble_device() -> BLEDevice:
149149
"""Return a mock BLE device."""
150-
return BLEDevice(
151-
"00:00:00:00:00:00", "GS_GS012345", details={"path": "path"}, rssi=50
152-
)
150+
return BLEDevice("00:00:00:00:00:00", "GS_GS012345", details={"path": "path"})
153151

154152

155153
@pytest.fixture

tests/components/lamarzocco/test_init.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from datetime import timedelta
44
from unittest.mock import AsyncMock, MagicMock, patch
55

6+
from bleak.backends.device import BLEDevice
67
from freezegun.api import FrozenDateTimeFactory
78
from pylamarzocco.const import FirmwareType, ModelName
89
from pylamarzocco.exceptions import AuthFail, RequestNotSuccessful
@@ -196,11 +197,27 @@ async def test_config_flow_entry_migration_downgrade(
196197
assert not await hass.config_entries.async_setup(entry.entry_id)
197198

198199

200+
@pytest.mark.parametrize(
201+
("ble_device", "has_client"),
202+
[
203+
(None, False),
204+
(
205+
BLEDevice(
206+
address="aa:bb:cc:dd:ee:ff",
207+
name="name",
208+
details={},
209+
),
210+
True,
211+
),
212+
],
213+
)
199214
async def test_bluetooth_is_set_from_discovery(
200215
hass: HomeAssistant,
201216
mock_config_entry: MockConfigEntry,
202217
mock_lamarzocco: MagicMock,
203218
mock_cloud_client: MagicMock,
219+
ble_device: BLEDevice | None,
220+
has_client: bool,
204221
) -> None:
205222
"""Check we can fill a device from discovery info."""
206223

@@ -216,13 +233,17 @@ async def test_bluetooth_is_set_from_discovery(
216233
patch(
217234
"homeassistant.components.lamarzocco.LaMarzoccoMachine"
218235
) as mock_machine_class,
236+
patch(
237+
"homeassistant.components.lamarzocco.async_ble_device_from_address",
238+
return_value=ble_device,
239+
),
219240
):
220241
mock_machine_class.return_value = mock_lamarzocco
221242
await async_init_integration(hass, mock_config_entry)
222243
discovery.assert_called_once()
223244
assert mock_machine_class.call_count == 1
224245
_, kwargs = mock_machine_class.call_args
225-
assert kwargs["bluetooth_client"] is not None
246+
assert (kwargs["bluetooth_client"] is not None) == has_client
226247

227248
assert mock_config_entry.data[CONF_MAC] == service_info.address
228249
assert mock_config_entry.data[CONF_TOKEN] == "token"
@@ -314,6 +335,50 @@ async def test_device(
314335
assert device == snapshot
315336

316337

338+
async def test_disconnect_on_stop(
339+
hass: HomeAssistant,
340+
mock_lamarzocco: MagicMock,
341+
mock_ble_device: BLEDevice,
342+
mock_config_entry: MockConfigEntry,
343+
) -> None:
344+
"""Test we close the connection with the La Marzocco when Home Assistants stops."""
345+
mock_config_entry = MockConfigEntry(
346+
title="My LaMarzocco",
347+
domain=DOMAIN,
348+
version=4,
349+
data=USER_INPUT
350+
| {
351+
CONF_MAC: mock_ble_device.address,
352+
CONF_TOKEN: "token",
353+
CONF_INSTALLATION_KEY: MOCK_INSTALLATION_KEY,
354+
},
355+
unique_id=mock_lamarzocco.serial_number,
356+
)
357+
358+
with (
359+
patch(
360+
"homeassistant.components.lamarzocco.async_ble_device_from_address",
361+
return_value=mock_ble_device,
362+
),
363+
patch(
364+
"homeassistant.components.lamarzocco.LaMarzoccoBluetoothClient",
365+
autospec=True,
366+
) as mock_bt_client_cls,
367+
):
368+
mock_bt_client = mock_bt_client_cls.return_value
369+
mock_bt_client.disconnect = AsyncMock()
370+
371+
await async_init_integration(hass, mock_config_entry)
372+
await hass.async_block_till_done()
373+
374+
assert mock_config_entry.state is ConfigEntryState.LOADED
375+
376+
hass.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
377+
await hass.async_block_till_done()
378+
379+
mock_bt_client.disconnect.assert_awaited_once()
380+
381+
317382
async def test_websocket_reconnects_after_termination(
318383
hass: HomeAssistant,
319384
mock_config_entry: MockConfigEntry,

0 commit comments

Comments
 (0)