Skip to content

Commit

Permalink
Force entity_id to be <model_serialnumber>
Browse files Browse the repository at this point in the history
tweaks
  • Loading branch information
mj23000 committed Dec 21, 2022
1 parent 671a7ee commit 608560a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
25 changes: 19 additions & 6 deletions custom_components/bangolufsen/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import logging
from typing import Any, TypedDict

from inflection import underscore
from mozart_api.exceptions import ApiException
from mozart_api.mozart_client import MozartClient
from urllib3.exceptions import MaxRetryError, NewConnectionError
import voluptuous as vol

from homeassistant.components.zeroconf import ZeroconfServiceInfo
from homeassistant.config_entries import ConfigEntry, ConfigFlow, OptionsFlow
from homeassistant.const import CONF_HOST, CONF_MODEL, CONF_NAME
from homeassistant.const import CONF_FRIENDLY_NAME, CONF_HOST, CONF_MODEL, CONF_NAME
from homeassistant.core import callback
from homeassistant.data_entry_flow import AbortFlow, FlowResult
from homeassistant.helpers import config_validation as cv, selector
Expand Down Expand Up @@ -114,6 +115,7 @@ class UserInput(TypedDict):
"""TypedDict for user_input."""

name: str
friendly_name: str
volume_step: int
default_volume: int
max_volume: int
Expand Down Expand Up @@ -186,15 +188,26 @@ async def async_step_zeroconf(
async def async_step_confirm(
self, user_input: UserInput | None = None
) -> FlowResult:
"""Confirm the configuration of the discovered device."""
"""Confirm the configuration of the device."""
if user_input is not None:
# Make sure that information from the zeroconf discovery is included
# Get the desired friendly name before changing it for generating entity_id
self._name = user_input[CONF_NAME]

# Make sure that all information is included
data = user_input
data[CONF_HOST] = self._host
data[CONF_MODEL] = self._model
data[CONF_BEOLINK_JID] = self._beolink_jid
data[CONF_FRIENDLY_NAME] = self._name

return self.async_create_entry(title=data[CONF_NAME], data=data)
# Manually define the entity_id
model_name = underscore(self._model.replace(" ", "_"))
data[CONF_NAME] = f"{model_name}_{self._serial_number}"

return self.async_create_entry(
title=self._name,
data=data,
)

client = MozartClient(self._host)
volume_settings = client.get_volume_settings(async_req=True).get()
Expand Down Expand Up @@ -240,17 +253,17 @@ async def async_step_init(self, user_input: UserInput | None = None) -> FlowResu

data[CONF_MODEL] = self._config_entry.data[CONF_MODEL]
data[CONF_BEOLINK_JID] = self._config_entry.data[CONF_BEOLINK_JID]
data[CONF_FRIENDLY_NAME] = user_input[CONF_NAME]
if not self.show_advanced_options:
data[CONF_HOST] = self._config_entry.data[CONF_HOST]

# Check connection
await _validate_host(data[CONF_HOST])

return self.async_create_entry(title=data[CONF_NAME], data=data)

# Create data schema with the last configuration as default values.
data_schema = _config_schema(
name=self._config_entry.data[CONF_NAME],
name=self._config_entry.data[CONF_FRIENDLY_NAME],
volume_step=self._config_entry.data[CONF_VOLUME_STEP],
default_volume=self._config_entry.data[CONF_DEFAULT_VOLUME],
max_volume=self._config_entry.data[CONF_MAX_VOLUME],
Expand Down
2 changes: 1 addition & 1 deletion custom_components/bangolufsen/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"issue_tracker": "https://github.com/bang-olufsen/bangolufsen-hacs/issues",
"requirements": ["mozart-api==2.3.4.15123.6"],
"zeroconf": ["_bangolufsen._tcp.local."],
"version": "0.5.2",
"version": "0.5.3",
"codeowners": ["@mj23000"],
"iot_class": "local_push",
"config_flow": true
Expand Down

0 comments on commit 608560a

Please sign in to comment.