Skip to content

Commit

Permalink
Fix HA incompatibility: always try to decode MQTT payloads (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
dermotduffy authored May 4, 2024
1 parent 8c2e781 commit 610e86c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
7 changes: 6 additions & 1 deletion custom_components/frigate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ def get_zones(config: dict[str, Any]) -> set[str]:
return cameras_zones


def decode_if_necessary(data: str | bytes) -> str:
"""Decode a string if necessary."""
return data.decode("utf-8") if isinstance(data, bytes) else data


async def async_setup(hass: HomeAssistant, config: Config) -> bool:
"""Set up this integration using YAML is not supported."""
integration = await async_get_integration(hass, DOMAIN)
Expand Down Expand Up @@ -474,5 +479,5 @@ async def async_will_remove_from_hass(self) -> None:
@callback # type: ignore[misc]
def _availability_message_received(self, msg: ReceiveMessage) -> None:
"""Handle a new received MQTT availability message."""
self._available = msg.payload == "online"
self._available = decode_if_necessary(msg.payload) == "online"
self.async_write_ha_state()
5 changes: 3 additions & 2 deletions custom_components/frigate/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from . import (
FrigateMQTTEntity,
ReceiveMessage,
decode_if_necessary,
get_cameras,
get_cameras_and_audio,
get_cameras_zones_and_objects,
Expand Down Expand Up @@ -184,7 +185,7 @@ def __init__(
@callback # type: ignore[misc]
def _state_message_received(self, msg: ReceiveMessage) -> None:
"""Handle a new received MQTT state message."""
self._is_on = msg.payload == "ON"
self._is_on = decode_if_necessary(msg.payload) == "ON"
self.async_write_ha_state()

@property
Expand Down Expand Up @@ -265,7 +266,7 @@ def __init__(
@callback # type: ignore[misc]
def _state_message_received(self, msg: ReceiveMessage) -> None:
"""Handle a new received MQTT state message."""
self._is_on = msg.payload == "ON"
self._is_on = decode_if_necessary(msg.payload) == "ON"
self.async_write_ha_state()

@property
Expand Down
5 changes: 3 additions & 2 deletions custom_components/frigate/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
FrigateEntity,
FrigateMQTTEntity,
ReceiveMessage,
decode_if_necessary,
get_friendly_name,
get_frigate_device_identifier,
get_frigate_entity_unique_id,
Expand Down Expand Up @@ -238,13 +239,13 @@ def __init__(
@callback # type: ignore[misc]
def _state_message_received(self, msg: ReceiveMessage) -> None:
"""Handle a new received MQTT state message."""
self._attr_is_recording = msg.payload.decode("utf-8") == "ON"
self._attr_is_recording = decode_if_necessary(msg.payload) == "ON"
self.async_write_ha_state()

@callback # type: ignore[misc]
def _motion_message_received(self, msg: ReceiveMessage) -> None:
"""Handle a new received MQTT extra message."""
self._attr_motion_detection_enabled = msg.payload.decode("utf-8") == "ON"
self._attr_motion_detection_enabled = decode_if_necessary(msg.payload) == "ON"
self.async_write_ha_state()

@property
Expand Down
3 changes: 2 additions & 1 deletion custom_components/frigate/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from . import (
FrigateMQTTEntity,
ReceiveMessage,
decode_if_necessary,
get_friendly_name,
get_frigate_device_identifier,
get_frigate_entity_unique_id,
Expand Down Expand Up @@ -119,7 +120,7 @@ def __init__(
@callback # type: ignore[misc]
def _state_message_received(self, msg: ReceiveMessage) -> None:
"""Handle a new received MQTT state message."""
self._is_on = msg.payload == "ON"
self._is_on = decode_if_necessary(msg.payload) == "ON"
self.async_write_ha_state()

@property
Expand Down
12 changes: 12 additions & 0 deletions tests/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@ async def test_switch_state(hass: HomeAssistant) -> None:
assert entity_state
assert entity_state.state == "off"

async_fire_mqtt_message(hass, "frigate/front_door/detect/state", b"ON")
await hass.async_block_till_done()
entity_state = hass.states.get(TEST_SWITCH_FRONT_DOOR_DETECT_ENTITY_ID)
assert entity_state
assert entity_state.state == "on"

async_fire_mqtt_message(hass, "frigate/front_door/detect/state", b"OFF")
await hass.async_block_till_done()
entity_state = hass.states.get(TEST_SWITCH_FRONT_DOOR_DETECT_ENTITY_ID)
assert entity_state
assert entity_state.state == "off"

async_fire_mqtt_message(hass, "frigate/front_door/detect/state", "INVALID_VALUE")
await hass.async_block_till_done()
entity_state = hass.states.get(TEST_SWITCH_FRONT_DOOR_DETECT_ENTITY_ID)
Expand Down

0 comments on commit 610e86c

Please sign in to comment.