Skip to content

Commit

Permalink
Improve Beoremote One paired / unpaired checks for config entry reloa…
Browse files Browse the repository at this point in the history
…ding

Restructure code to resemble core version of integration closer
Cleanup
Small improvements
  • Loading branch information
mj23000 committed Dec 19, 2024
1 parent e2b8bb5 commit 1d5be91
Show file tree
Hide file tree
Showing 11 changed files with 270 additions and 256 deletions.
2 changes: 1 addition & 1 deletion custom_components/bang_olufsen/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Binary Sensor entities from config entry."""
entities: list[BangOlufsenEntity] = []
entities: list[BangOlufsenBinarySensor] = []

# Check if device has a battery
battery_state = await config_entry.runtime_data.client.get_battery_state()
Expand Down
62 changes: 25 additions & 37 deletions custom_components/bang_olufsen/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,11 @@ class WebsocketNotification(StrEnum):
ALL = "all"


# Range for bass and treble entities
BASS_TREBLE_RANGE = range(-6, 6, 1)

DOMAIN: Final[str] = "bang_olufsen"

# Default values for configuration.
DEFAULT_MODEL: Final[str] = BangOlufsenModel.BEOSOUND_BALANCE

MANUFACTURER: Final[str] = "Bang & Olufsen"

# Configuration.
CONF_BEOLINK_JID: Final = "jid"
CONF_SERIAL_NUMBER: Final = "serial_number"
Expand All @@ -138,6 +133,8 @@ class WebsocketNotification(StrEnum):
model for model in BangOlufsenModel if model != BangOlufsenModel.BEOREMOTE_ONE
]

MANUFACTURER: Final[str] = "Bang & Olufsen"

# Attribute names for zeroconf discovery.
ATTR_TYPE_NUMBER: Final[str] = "tn"
ATTR_SERIAL_NUMBER: Final[str] = "sn"
Expand All @@ -159,17 +156,6 @@ class WebsocketNotification(StrEnum):
MediaType.CHANNEL,
)

# Playback states for playing and not playing
PLAYING: Final[tuple[str, ...]] = ("started", "buffering", BANG_OLUFSEN_ON)
NOT_PLAYING: Final[tuple[str, ...]] = (
"idle",
"paused",
"stopped",
"ended",
"unknown",
"error",
)

# Fallback sources to use in case of API failure.
FALLBACK_SOURCES: Final[SourceArray] = SourceArray(
items=[
Expand Down Expand Up @@ -274,22 +260,24 @@ class WebsocketNotification(StrEnum):
# Device events
BANG_OLUFSEN_WEBSOCKET_EVENT: Final[str] = f"{DOMAIN}_websocket_event"


CONNECTION_STATUS: Final[str] = "CONNECTION_STATUS"

# Dict used to translate native Bang & Olufsen event names to string.json compatible ones
EVENT_TRANSLATION_MAP: dict[str, str] = {
# Beoremote One
"KeyPress": "key_press",
"KeyRelease": "key_release",
# Physical "buttons"
"shortPress (Release)": "short_press_release",
"longPress (Timeout)": "long_press_timeout",
"longPress (Release)": "long_press_release",
"veryLongPress (Timeout)": "very_long_press_timeout",
"veryLongPress (Release)": "very_long_press_release",
# Proximity sensor
"proximityPresenceDetected": "proximity_presence_detected",
"proximityPresenceNotDetected": "proximity_presence_not_detected",
}

CONNECTION_STATUS: Final[str] = "CONNECTION_STATUS"

DEVICE_BUTTONS: Final[list[str]] = [
"Bluetooth",
"Microphone",
Expand Down Expand Up @@ -360,7 +348,7 @@ class WebsocketNotification(StrEnum):
"Func17",
)

# "keys" that are unique to the Control and Light submenus
# "keys" that are unique to the Control submenu
BEO_REMOTE_CONTROL_KEYS: Final[tuple[str, ...]] = (
"Func18",
"Func19",
Expand All @@ -382,6 +370,23 @@ class WebsocketNotification(StrEnum):
"proximity_presence_not_detected",
]

# Beolink Converter NL/ML sources need to be transformed to upper case
BEOLINK_JOIN_SOURCES_TO_UPPER = (
"aux_a",
"cd",
"ph",
"radio",
"tp1",
"tp2",
)
BEOLINK_JOIN_SOURCES = (
*BEOLINK_JOIN_SOURCES_TO_UPPER,
"beoradio",
"deezer",
"spotify",
"tidal",
)


BEOLINK_LEADER_COMMAND: Final[str] = "BEOLINK_LEADER_COMMAND"
BEOLINK_LISTENER_COMMAND: Final[str] = "BEOLINK_LISTENER_COMMAND"
Expand Down Expand Up @@ -430,20 +435,3 @@ class WebsocketNotification(StrEnum):
STR_PARAMETERS,
NONE_PARAMETERS,
)

# Beolink Converter NL/ML sources need to be transformed to upper case
BEOLINK_JOIN_SOURCES_TO_UPPER = (
"aux_a",
"cd",
"ph",
"radio",
"tp1",
"tp2",
)
BEOLINK_JOIN_SOURCES = (
*BEOLINK_JOIN_SOURCES_TO_UPPER,
"beoradio",
"deezer",
"spotify",
"tidal",
)
7 changes: 3 additions & 4 deletions custom_components/bang_olufsen/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ def __init__(
self._client = config_entry.runtime_data.client

# Get the input from the config entry.
# Use _entry instead of config_entry to avoid conflicts with Home Assistant classes such as DataUpdateCoordinator.
self._entry = config_entry
self.entry = config_entry

# Set the configuration variables.
self._host: str = self._entry.data[CONF_HOST]
self._unique_id: str = cast(str, self._entry.unique_id)
self._host: str = self.entry.data[CONF_HOST]
self._unique_id: str = cast(str, self.entry.unique_id)

# Objects that get directly updated by notifications.
self._active_listening_mode = ListeningModeProps()
Expand Down
2 changes: 1 addition & 1 deletion custom_components/bang_olufsen/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def async_setup_entry(
) -> None:
"""Set up Sensor entities from config entry."""

entities: list[EventEntity] = []
entities: list[BangOlufsenEvent] = []

# Add physical "buttons"
if config_entry.data[CONF_MODEL] in MODEL_SUPPORT_MAP[MODEL_SUPPORT_DEVICE_BUTTONS]:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/bang_olufsen/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"iot_class": "local_push",
"issue_tracker": "https://github.com/bang-olufsen/bang_olufsen-hacs/issues",
"requirements": ["mozart-api==4.1.1.116.4"],
"version": "3.2.0",
"version": "3.2.1",
"zeroconf": ["_bangolufsen._tcp.local."]
}
Loading

0 comments on commit 1d5be91

Please sign in to comment.