Skip to content

Commit 10b186a

Browse files
zweckjfrenck
authored andcommitted
Bump pylamarzocco to 2.1.0 (home-assistant#152364)
1 parent 757aec1 commit 10b186a

File tree

10 files changed

+102
-63
lines changed

10 files changed

+102
-63
lines changed

homeassistant/components/lamarzocco/__init__.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import asyncio
44
import logging
5+
import uuid
56

67
from packaging import version
78
from pylamarzocco import (
@@ -11,6 +12,7 @@
1112
)
1213
from pylamarzocco.const import FirmwareType
1314
from pylamarzocco.exceptions import AuthFail, RequestNotSuccessful
15+
from pylamarzocco.util import InstallationKey, generate_installation_key
1416

1517
from homeassistant.components.bluetooth import async_discovered_service_info
1618
from homeassistant.const import (
@@ -25,7 +27,7 @@
2527
from homeassistant.helpers import issue_registry as ir
2628
from homeassistant.helpers.aiohttp_client import async_create_clientsession
2729

28-
from .const import CONF_USE_BLUETOOTH, DOMAIN
30+
from .const import CONF_INSTALLATION_KEY, CONF_USE_BLUETOOTH, DOMAIN
2931
from .coordinator import (
3032
LaMarzoccoConfigEntry,
3133
LaMarzoccoConfigUpdateCoordinator,
@@ -60,6 +62,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: LaMarzoccoConfigEntry) -
6062
cloud_client = LaMarzoccoCloudClient(
6163
username=entry.data[CONF_USERNAME],
6264
password=entry.data[CONF_PASSWORD],
65+
installation_key=InstallationKey.from_json(entry.data[CONF_INSTALLATION_KEY]),
6366
client=async_create_clientsession(hass),
6467
)
6568

@@ -166,45 +169,37 @@ async def async_migrate_entry(
166169
hass: HomeAssistant, entry: LaMarzoccoConfigEntry
167170
) -> bool:
168171
"""Migrate config entry."""
169-
if entry.version > 3:
172+
if entry.version > 4:
170173
# guard against downgrade from a future version
171174
return False
172175

173-
if entry.version == 1:
176+
if entry.version in (1, 2):
174177
_LOGGER.error(
175-
"Migration from version 1 is no longer supported, please remove and re-add the integration"
178+
"Migration from version 1 or 2 is no longer supported, please remove and re-add the integration"
176179
)
177180
return False
178181

179-
if entry.version == 2:
182+
if entry.version == 3:
183+
installation_key = generate_installation_key(str(uuid.uuid4()).lower())
180184
cloud_client = LaMarzoccoCloudClient(
181185
username=entry.data[CONF_USERNAME],
182186
password=entry.data[CONF_PASSWORD],
187+
installation_key=installation_key,
183188
)
184189
try:
185-
things = await cloud_client.list_things()
190+
await cloud_client.async_register_client()
186191
except (AuthFail, RequestNotSuccessful) as exc:
187192
_LOGGER.error("Migration failed with error %s", exc)
188193
return False
189-
v3_data = {
190-
CONF_USERNAME: entry.data[CONF_USERNAME],
191-
CONF_PASSWORD: entry.data[CONF_PASSWORD],
192-
CONF_TOKEN: next(
193-
(
194-
thing.ble_auth_token
195-
for thing in things
196-
if thing.serial_number == entry.unique_id
197-
),
198-
None,
199-
),
200-
}
201-
if CONF_MAC in entry.data:
202-
v3_data[CONF_MAC] = entry.data[CONF_MAC]
194+
203195
hass.config_entries.async_update_entry(
204196
entry,
205-
data=v3_data,
206-
version=3,
197+
data={
198+
**entry.data,
199+
CONF_INSTALLATION_KEY: installation_key.to_json(),
200+
},
201+
version=4,
207202
)
208-
_LOGGER.debug("Migrated La Marzocco config entry to version 2")
203+
_LOGGER.debug("Migrated La Marzocco config entry to version 4")
209204

210205
return True

homeassistant/components/lamarzocco/config_flow.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
from collections.abc import Mapping
66
import logging
77
from typing import Any
8+
import uuid
89

910
from aiohttp import ClientSession
1011
from pylamarzocco import LaMarzoccoCloudClient
1112
from pylamarzocco.exceptions import AuthFail, RequestNotSuccessful
1213
from pylamarzocco.models import Thing
14+
from pylamarzocco.util import InstallationKey, generate_installation_key
1315
import voluptuous as vol
1416

1517
from homeassistant.components.bluetooth import (
@@ -45,7 +47,7 @@
4547
)
4648
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
4749

48-
from .const import CONF_USE_BLUETOOTH, DOMAIN
50+
from .const import CONF_INSTALLATION_KEY, CONF_USE_BLUETOOTH, DOMAIN
4951
from .coordinator import LaMarzoccoConfigEntry
5052

5153
CONF_MACHINE = "machine"
@@ -57,9 +59,10 @@
5759
class LmConfigFlow(ConfigFlow, domain=DOMAIN):
5860
"""Handle a config flow for La Marzocco."""
5961

60-
VERSION = 3
62+
VERSION = 4
6163

6264
_client: ClientSession
65+
_installation_key: InstallationKey
6366

6467
def __init__(self) -> None:
6568
"""Initialize the config flow."""
@@ -84,12 +87,17 @@ async def async_step_user(
8487
}
8588

8689
self._client = async_create_clientsession(self.hass)
90+
self._installation_key = generate_installation_key(
91+
str(uuid.uuid4()).lower()
92+
)
8793
cloud_client = LaMarzoccoCloudClient(
8894
username=data[CONF_USERNAME],
8995
password=data[CONF_PASSWORD],
9096
client=self._client,
97+
installation_key=self._installation_key,
9198
)
9299
try:
100+
await cloud_client.async_register_client()
93101
things = await cloud_client.list_things()
94102
except AuthFail:
95103
_LOGGER.debug("Server rejected login credentials")
@@ -184,6 +192,7 @@ async def async_step_machine_selection(
184192
title=selected_device.name,
185193
data={
186194
**self._config,
195+
CONF_INSTALLATION_KEY: self._installation_key.to_json(),
187196
CONF_TOKEN: self._things[serial_number].ble_auth_token,
188197
},
189198
)

homeassistant/components/lamarzocco/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
DOMAIN: Final = "lamarzocco"
66

77
CONF_USE_BLUETOOTH: Final = "use_bluetooth"
8+
CONF_INSTALLATION_KEY: Final = "installation_key"

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.0.11"]
40+
"requirements": ["pylamarzocco==2.1.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/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,6 @@ def get_bluetooth_service_info(model: ModelName, serial: str) -> BluetoothServic
5454
service_uuids=[],
5555
source="local",
5656
)
57+
58+
59+
MOCK_INSTALLATION_KEY = '{"secret": "K9ZW2vlMSb3QXmhySx4pxAbTHujWj3VZ01Jn3D/sO98=", "private_key": "MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg8iotE8El786F6kHuEL8GyYhjDB7oo06vNhQwtewF37yhRANCAAQCLb9lHskiavvfkI4H2B+WsdkusfgBBFuFNRrGV8bqPMra1TK5myb/ecdZfHJBBJrcbdt90QMDmXQm5L3muXXe", "installation_id": "4e966f3f-2abc-49c4-a362-3cd3346f1a87"}'

tests/components/lamarzocco/conftest.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
ThingSettings,
1313
ThingStatistics,
1414
)
15+
from pylamarzocco.util import InstallationKey
1516
import pytest
1617

17-
from homeassistant.components.lamarzocco.const import DOMAIN
18+
from homeassistant.components.lamarzocco.const import CONF_INSTALLATION_KEY, DOMAIN
1819
from homeassistant.const import CONF_ADDRESS, CONF_TOKEN
1920
from homeassistant.core import HomeAssistant
2021

21-
from . import SERIAL_DICT, USER_INPUT, async_init_integration
22+
from . import MOCK_INSTALLATION_KEY, SERIAL_DICT, USER_INPUT, async_init_integration
2223

2324
from tests.common import MockConfigEntry, load_json_object_fixture
2425

@@ -31,11 +32,12 @@ def mock_config_entry(
3132
return MockConfigEntry(
3233
title="My LaMarzocco",
3334
domain=DOMAIN,
34-
version=3,
35+
version=4,
3536
data=USER_INPUT
3637
| {
3738
CONF_ADDRESS: "000000000000",
3839
CONF_TOKEN: "token",
40+
CONF_INSTALLATION_KEY: MOCK_INSTALLATION_KEY,
3941
},
4042
unique_id=mock_lamarzocco.serial_number,
4143
)
@@ -51,6 +53,22 @@ async def init_integration(
5153
return mock_config_entry
5254

5355

56+
@pytest.fixture(autouse=True)
57+
def mock_generate_installation_key() -> Generator[MagicMock]:
58+
"""Return a mocked generate_installation_key."""
59+
with (
60+
patch(
61+
"homeassistant.components.lamarzocco.generate_installation_key",
62+
return_value=InstallationKey.from_json(MOCK_INSTALLATION_KEY),
63+
) as mock_generate,
64+
patch(
65+
"homeassistant.components.lamarzocco.config_flow.generate_installation_key",
66+
new=mock_generate,
67+
),
68+
):
69+
yield mock_generate
70+
71+
5472
@pytest.fixture
5573
def device_fixture() -> ModelName:
5674
"""Return the device fixture for a specific device."""

tests/components/lamarzocco/test_config_flow.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
import pytest
1010

1111
from homeassistant.components.lamarzocco.config_flow import CONF_MACHINE
12-
from homeassistant.components.lamarzocco.const import CONF_USE_BLUETOOTH, DOMAIN
12+
from homeassistant.components.lamarzocco.const import (
13+
CONF_INSTALLATION_KEY,
14+
CONF_USE_BLUETOOTH,
15+
DOMAIN,
16+
)
1317
from homeassistant.config_entries import (
1418
SOURCE_BLUETOOTH,
1519
SOURCE_DHCP,
@@ -23,7 +27,12 @@
2327
from homeassistant.helpers.service_info.bluetooth import BluetoothServiceInfo
2428
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
2529

26-
from . import USER_INPUT, async_init_integration, get_bluetooth_service_info
30+
from . import (
31+
MOCK_INSTALLATION_KEY,
32+
USER_INPUT,
33+
async_init_integration,
34+
get_bluetooth_service_info,
35+
)
2736

2837
from tests.common import MockConfigEntry
2938

@@ -68,6 +77,7 @@ async def __do_sucessful_machine_selection_step(
6877
assert result["data"] == {
6978
**USER_INPUT,
7079
CONF_TOKEN: None,
80+
CONF_INSTALLATION_KEY: MOCK_INSTALLATION_KEY,
7181
}
7282
assert result["result"].unique_id == "GS012345"
7383

@@ -344,6 +354,7 @@ async def test_bluetooth_discovery(
344354
**USER_INPUT,
345355
CONF_MAC: "aa:bb:cc:dd:ee:ff",
346356
CONF_TOKEN: "dummyToken",
357+
CONF_INSTALLATION_KEY: MOCK_INSTALLATION_KEY,
347358
}
348359

349360

@@ -407,6 +418,7 @@ async def test_bluetooth_discovery_errors(
407418
**USER_INPUT,
408419
CONF_MAC: "aa:bb:cc:dd:ee:ff",
409420
CONF_TOKEN: None,
421+
CONF_INSTALLATION_KEY: MOCK_INSTALLATION_KEY,
410422
}
411423

412424

@@ -438,6 +450,7 @@ async def test_dhcp_discovery(
438450
**USER_INPUT,
439451
CONF_ADDRESS: "aabbccddeeff",
440452
CONF_TOKEN: None,
453+
CONF_INSTALLATION_KEY: MOCK_INSTALLATION_KEY,
441454
}
442455

443456

0 commit comments

Comments
 (0)