Skip to content

Commit

Permalink
Fix test issue and nasty multiple inheritance bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
dermotduffy committed Aug 26, 2023
1 parent bb9907b commit 701daaf
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
4 changes: 3 additions & 1 deletion custom_components/frigate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def __init__(self, config_entry: ConfigEntry):
@property
def available(self) -> bool:
"""Return the availability of the entity."""
return self._available
return self._available and super().available

def _get_model(self) -> str:
"""Get the Frigate device model string."""
Expand Down Expand Up @@ -451,11 +451,13 @@ async def async_added_to_hass(self) -> None:
self._topic_map,
)
self._sub_state = await async_subscribe_topics(self.hass, state)
await super().async_added_to_hass()

async def async_will_remove_from_hass(self) -> None:
"""Cleanup prior to hass removal."""
async_unsubscribe_topics(self.hass, self._sub_state)
self._sub_state = None
await super().async_will_remove_from_hass()

@callback # type: ignore[misc]
def _availability_message_received(self, msg: ReceiveMessage) -> None:
Expand Down
8 changes: 3 additions & 5 deletions custom_components/frigate/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,9 @@ def _motion_message_received(self, msg: ReceiveMessage) -> None:
def available(self) -> bool:
"""Signal when frigate loses connection to camera."""
if self.coordinator.data:
data = self.coordinator.data.get(self._cam_name, {}).get("camera_fps", 0)

if data:
return True
return False
if self.coordinator.data.get(self._cam_name, {}).get("camera_fps", 0) == 0:
return False
return super().available

@property
def unique_id(self) -> str:
Expand Down
12 changes: 7 additions & 5 deletions tests/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,16 +363,18 @@ async def test_camera_disable_motion_detection(

async def test_camera_unavailable(hass: HomeAssistant) -> None:
"""Test that camera is marked as unavailable."""
await setup_mock_frigate_config_entry(hass)
client = create_mock_frigate_client()
stats: dict[str, Any] = copy.deepcopy(TEST_STATS)
client.async_get_stats = AsyncMock(return_value=stats)
await setup_mock_frigate_config_entry(hass, client=client)

entity_state = hass.states.get(TEST_CAMERA_FRONT_DOOR_ENTITY_ID)
assert entity_state
assert entity_state.state == "streaming"

client = create_mock_frigate_client()
stats = copy.deepcopy(TEST_STATS)
stats["front_door"]["camera_fps"] = 0.0
client.async_get_stats = AsyncMock(return_value=stats)

async_fire_time_changed(hass, dt_util.utcnow() + (SCAN_INTERVAL * 10))
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL)
await hass.async_block_till_done()

entity_state = hass.states.get(TEST_CAMERA_FRONT_DOOR_ENTITY_ID)
Expand Down
7 changes: 6 additions & 1 deletion tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,18 @@ async def test_status_sensor_error(hass: HomeAssistant) -> None:
await setup_mock_frigate_config_entry(hass, client=client)
await enable_and_load_entity(hass, client, TEST_SENSOR_FRIGATE_STATUS_ENTITY_ID)

async_fire_mqtt_message(hass, "frigate/available", "online")
await hass.async_block_till_done()

client.async_get_stats = AsyncMock(side_effect=FrigateApiClientError)
async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL)
await hass.async_block_till_done()

entity_state = hass.states.get(TEST_SENSOR_FRIGATE_STATUS_ENTITY_ID)
assert entity_state
assert entity_state.state == "error"

# The update coordinator will treat the error as unavailability.
assert entity_state.state == "unavailable"
assert entity_state.attributes["icon"] == ICON_SERVER


Expand Down

0 comments on commit 701daaf

Please sign in to comment.