Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@sweenu
Copy link

@sweenu sweenu commented Nov 3, 2025

Proposed change

This is the first step towards: #155651

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@abmantis abmantis changed the title bbox: add tests Add tests for bbox Nov 3, 2025
Copy link
Member

@joostlek joostlek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think mik's review already covers a lot of things that can be imporved

@home-assistant
Copy link

home-assistant bot commented Nov 4, 2025

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft November 4, 2025 06:08
@sweenu sweenu marked this pull request as ready for review November 4, 2025 08:18
@home-assistant home-assistant bot requested a review from joostlek November 4, 2025 08:18
@sweenu sweenu marked this pull request as draft November 4, 2025 08:18
@sweenu sweenu force-pushed the bbox_add_tests branch 3 times, most recently from 1b58642 to 5664b83 Compare November 4, 2025 13:24
@sweenu
Copy link
Author

sweenu commented Nov 4, 2025

Thanks a lot @mik-laj for your review!
I admit I didn't give as much effort as I should have into this first PR as most of it gets replaced eventually. But that makes your guys' job harder, so I will definitely be more careful in the next ones.

@sweenu sweenu marked this pull request as ready for review November 4, 2025 13:29
@mik-laj
Copy link
Contributor

mik-laj commented Nov 4, 2025

@sweenu I'm glad we've managed to get the code to this point now. I look forward to your next contributions!

box = pybbox.Bbox(ip=self.host)
result = box.get_all_connected_devices()
try:
box = pybbox.Bbox(ip=self.host)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can creating the Bbox object raise?

Comment on lines +16 to +20
with (
patch("homeassistant.components.bbox.device_tracker.pybbox") as mock_pybbox_dt,
patch("homeassistant.components.bbox.sensor.pybbox") as mock_pybbox_sensor,
):
mock_bbox = MagicMock()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
with (
patch("homeassistant.components.bbox.device_tracker.pybbox") as mock_pybbox_dt,
patch("homeassistant.components.bbox.sensor.pybbox") as mock_pybbox_sensor,
):
mock_bbox = MagicMock()
with (
patch("homeassistant.components.bbox.device_tracker.pybbox.Bbox", autospec=True) as mock_bbox_client,
patch("homeassistant.components.bbox.sensor.pybbox", new=mock_bbox_client),
):
mock_bbox = mock_bbox_client.return_value

This way you don't have to assign it to 2 separate ones

Comment on lines +86 to +92
@pytest.fixture
def mock_add_entities() -> Generator[MagicMock]:
"""Mock add_entities callback."""
with patch(
"homeassistant.components.bbox.sensor.AddEntitiesCallback", spec=True
) as mock_add_entities:
yield mock_add_entities
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not patch this one

Comment on lines +24 to +26
scanner = get_scanner(hass, device_tracker_config)
assert scanner is not None
assert scanner.success_init is True
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead, we should let HA setup the integration with the YAML config, and then we can check if the device tracker entities that we expect are registered

Comment on lines +35 to +40
mock_bbox_api.get_all_connected_devices.side_effect = requests.exceptions.HTTPError(
"Connection failed"
)

scanner = get_scanner(hass, device_tracker_config)
assert scanner is None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case we should check that there are no device tracker entities created after letting HA setting it up

(in other words, the tests shouldn't call get_scanner directly)

Comment on lines +57 to +76
async def test_get_device_name(
hass: HomeAssistant,
device_tracker_config: ConfigType,
mock_bbox_api: MagicMock,
) -> None:
"""Test getting device name by MAC address."""
scanner = get_scanner(hass, device_tracker_config)
assert scanner is not None

# Test existing device
name = scanner.get_device_name("aa:bb:cc:dd:ee:ff")
assert name == "test_device"

# Test another existing device
name = scanner.get_device_name("ff:ee:dd:cc:bb:aa")
assert name == "another_device"

# Test non-existing device
name = scanner.get_device_name("11:22:33:44:55:66")
assert name is None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could test this via snapshots

"""Test sensor values for different sensor types."""
# Setup platform with specific sensor type
sensor_config[SENSOR_DOMAIN][CONF_MONITORED_VARIABLES] = [sensor_type]
setup_platform(hass, sensor_config[SENSOR_DOMAIN], mock_add_entities)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't call setup platform ourselves. Instead let HA set the integration up, after which you can use snapshots

Comment on lines +71 to +74
assert entity._attr_native_value.replace(microsecond=0) == expected_time.replace(
microsecond=0
)
assert entity._attr_device_class == "timestamp"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we also shouldn't access entities directly

@home-assistant home-assistant bot marked this pull request as draft November 4, 2025 16:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants