Skip to content

Commit

Permalink
Don't log warning for core integrations on new feature flags in Clima…
Browse files Browse the repository at this point in the history
…te (#109250)

* Don't log for core integration on Climate new feature flags

* Add test

* Fix test
  • Loading branch information
gjohansson-ST authored and frenck committed Feb 1, 2024
1 parent 647ac10 commit c31dfd6
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 20 deletions.
3 changes: 3 additions & 0 deletions homeassistant/components/climate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ def add_to_platform_start(

def _report_turn_on_off(feature: str, method: str) -> None:
"""Log warning not implemented turn on/off feature."""
module = type(self).__module__
if module and "custom_components" not in module:
return
report_issue = self._suggest_report_issue()
if feature.startswith("TURN"):
message = (
Expand Down
114 changes: 94 additions & 20 deletions tests/components/climate/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from enum import Enum
from types import ModuleType
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch

import pytest
import voluptuous as vol
Expand Down Expand Up @@ -415,23 +415,26 @@ async def async_setup_entry_climate_platform(
MockPlatform(async_setup_entry=async_setup_entry_climate_platform),
)

config_entry = MockConfigEntry(domain="test")
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
with patch.object(
MockClimateEntityTest, "__module__", "tests.custom_components.climate.test_init"
):
config_entry = MockConfigEntry(domain="test")
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

state = hass.states.get("climate.test")
assert state is not None

assert (
"Entity climate.test (<class 'tests.components.climate.test_init."
"Entity climate.test (<class 'tests.custom_components.climate.test_init."
"test_warning_not_implemented_turn_on_off_feature.<locals>.MockClimateEntityTest'>)"
" does not set ClimateEntityFeature.TURN_OFF but implements the turn_off method."
" Please report it to the author of the 'test' custom integration"
in caplog.text
)
assert (
"Entity climate.test (<class 'tests.components.climate.test_init."
"Entity climate.test (<class 'tests.custom_components.climate.test_init."
"test_warning_not_implemented_turn_on_off_feature.<locals>.MockClimateEntityTest'>)"
" does not set ClimateEntityFeature.TURN_ON but implements the turn_on method."
" Please report it to the author of the 'test' custom integration"
Expand Down Expand Up @@ -520,16 +523,19 @@ async def async_setup_entry_climate_platform(
MockPlatform(async_setup_entry=async_setup_entry_climate_platform),
)

config_entry = MockConfigEntry(domain="test")
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
with patch.object(
MockClimateEntityTest, "__module__", "tests.custom_components.climate.test_init"
):
config_entry = MockConfigEntry(domain="test")
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

state = hass.states.get("climate.test")
assert state is not None

assert (
"Entity climate.test (<class 'tests.components.climate.test_init."
"Entity climate.test (<class 'tests.custom_components.climate.test_init."
"test_implicit_warning_not_implemented_turn_on_off_feature.<locals>.MockClimateEntityTest'>)"
" implements HVACMode(s): off, heat and therefore implicitly supports the turn_on/turn_off"
" methods without setting the proper ClimateEntityFeature. Please report it to the author"
Expand Down Expand Up @@ -584,10 +590,13 @@ async def async_setup_entry_climate_platform(
MockPlatform(async_setup_entry=async_setup_entry_climate_platform),
)

config_entry = MockConfigEntry(domain="test")
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
with patch.object(
MockClimateEntityTest, "__module__", "tests.custom_components.climate.test_init"
):
config_entry = MockConfigEntry(domain="test")
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

state = hass.states.get("climate.test")
assert state is not None
Expand Down Expand Up @@ -652,10 +661,13 @@ async def async_setup_entry_climate_platform(
MockPlatform(async_setup_entry=async_setup_entry_climate_platform),
)

config_entry = MockConfigEntry(domain="test")
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
with patch.object(
MockClimateEntityTest, "__module__", "tests.custom_components.climate.test_init"
):
config_entry = MockConfigEntry(domain="test")
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

state = hass.states.get("climate.test")
assert state is not None
Expand All @@ -672,3 +684,65 @@ async def async_setup_entry_climate_platform(
" implements HVACMode(s): off, heat and therefore implicitly supports the off, heat methods"
not in caplog.text
)


async def test_no_warning_on_core_integrations_for_on_off_feature_flags(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, config_flow_fixture: None
) -> None:
"""Test we don't warn on core integration on new turn_on/off feature flags."""

class MockClimateEntityTest(MockClimateEntity):
"""Mock Climate device."""

def turn_on(self) -> None:
"""Turn on."""

def turn_off(self) -> None:
"""Turn off."""

async def async_setup_entry_init(
hass: HomeAssistant, config_entry: ConfigEntry
) -> bool:
"""Set up test config entry."""
await hass.config_entries.async_forward_entry_setups(config_entry, [DOMAIN])
return True

async def async_setup_entry_climate_platform(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up test climate platform via config entry."""
async_add_entities(
[MockClimateEntityTest(name="test", entity_id="climate.test")]
)

mock_integration(
hass,
MockModule(
"test",
async_setup_entry=async_setup_entry_init,
),
built_in=False,
)
mock_platform(
hass,
"test.climate",
MockPlatform(async_setup_entry=async_setup_entry_climate_platform),
)

with patch.object(
MockClimateEntityTest, "__module__", "homeassistant.components.test.climate"
):
config_entry = MockConfigEntry(domain="test")
config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()

state = hass.states.get("climate.test")
assert state is not None

assert (
"does not set ClimateEntityFeature.TURN_OFF but implements the turn_off method."
not in caplog.text
)

0 comments on commit c31dfd6

Please sign in to comment.