1 """The La Marzocco integration."""
5 from packaging
import version
6 from pylamarzocco.client_bluetooth
import LaMarzoccoBluetoothClient
7 from pylamarzocco.client_cloud
import LaMarzoccoCloudClient
8 from pylamarzocco.client_local
import LaMarzoccoLocalClient
9 from pylamarzocco.const
import BT_MODEL_PREFIXES, FirmwareType
10 from pylamarzocco.exceptions
import AuthFail, RequestNotSuccessful
28 from .const
import CONF_USE_BLUETOOTH, DOMAIN
29 from .coordinator
import LaMarzoccoConfigEntry, LaMarzoccoUpdateCoordinator
32 Platform.BINARY_SENSOR,
42 _LOGGER = logging.getLogger(__name__)
46 """Set up La Marzocco as config entry."""
48 assert entry.unique_id
49 serial = entry.unique_id
51 cloud_client = LaMarzoccoCloudClient(
52 username=entry.data[CONF_USERNAME],
53 password=entry.data[CONF_PASSWORD],
58 local_client: LaMarzoccoLocalClient |
None =
None
59 if (host := entry.data.get(CONF_HOST))
is not None:
60 _LOGGER.debug(
"Initializing local API")
61 local_client = LaMarzoccoLocalClient(
63 local_bearer=entry.data[CONF_TOKEN],
68 bluetooth_client: LaMarzoccoBluetoothClient |
None =
None
69 if entry.options.get(CONF_USE_BLUETOOTH,
True):
71 def bluetooth_configured() -> bool:
72 return entry.data.get(CONF_MAC,
"")
and entry.data.get(CONF_NAME,
"")
74 if not bluetooth_configured():
77 (name := discovery_info.name)
78 and name.startswith(BT_MODEL_PREFIXES)
79 and name.split(
"_")[1] == serial
81 _LOGGER.debug(
"Found Bluetooth device, configuring with Bluetooth")
83 hass.config_entries.async_update_entry(
87 CONF_MAC: discovery_info.address,
88 CONF_NAME: discovery_info.name,
93 if bluetooth_configured():
94 _LOGGER.debug(
"Initializing Bluetooth device")
95 bluetooth_client = LaMarzoccoBluetoothClient(
96 username=entry.data[CONF_USERNAME],
98 token=entry.data[CONF_TOKEN],
99 address_or_ble_device=entry.data[CONF_MAC],
105 local_client=local_client,
106 cloud_client=cloud_client,
107 bluetooth_client=bluetooth_client,
110 await coordinator.async_config_entry_first_refresh()
111 entry.runtime_data = coordinator
113 gateway_version = coordinator.device.firmware[FirmwareType.GATEWAY].current_version
114 if version.parse(gateway_version) < version.parse(
"v3.4-rc5"):
116 ir.async_create_issue(
119 "unsupported_gateway_firmware",
121 severity=ir.IssueSeverity.ERROR,
122 translation_key=
"unsupported_gateway_firmware",
123 translation_placeholders={
"gateway_version": gateway_version},
126 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
128 async
def update_listener(hass: HomeAssistant, entry: ConfigEntry) ->
None:
129 await hass.config_entries.async_reload(entry.entry_id)
131 entry.async_on_unload(entry.add_update_listener(update_listener))
137 """Unload a config entry."""
138 return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
142 """Migrate config entry."""
143 if entry.version > 2:
147 if entry.version == 1:
148 cloud_client = LaMarzoccoCloudClient(
149 username=entry.data[CONF_USERNAME],
150 password=entry.data[CONF_PASSWORD],
153 fleet = await cloud_client.get_customer_fleet()
154 except (AuthFail, RequestNotSuccessful)
as exc:
155 _LOGGER.error(
"Migration failed with error %s", exc)
158 assert entry.unique_id
is not None
159 device = fleet[entry.unique_id]
161 CONF_USERNAME: entry.data[CONF_USERNAME],
162 CONF_PASSWORD: entry.data[CONF_PASSWORD],
163 CONF_MODEL: device.model,
164 CONF_NAME: device.name,
165 CONF_TOKEN: device.communication_key,
168 if CONF_HOST
in entry.data:
169 v2_data[CONF_HOST] = entry.data[CONF_HOST]
171 if CONF_MAC
in entry.data:
172 v2_data[CONF_MAC] = entry.data[CONF_MAC]
174 hass.config_entries.async_update_entry(
179 _LOGGER.debug(
"Migrated La Marzocco config entry to version 2")
Iterable[BluetoothServiceInfoBleak] async_discovered_service_info(HomeAssistant hass, bool connectable=True)
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, LaMarzoccoConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
None update_listener(HomeAssistant hass, ConfigEntry entry)
httpx.AsyncClient create_async_httpx_client(HomeAssistant hass, bool verify_ssl=True, bool auto_cleanup=True, SSLCipherList ssl_cipher_list=SSLCipherList.PYTHON_DEFAULT, **Any kwargs)