From b70df4ab18f2f1279925cb62b247baecfe961ce3 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Thu, 10 Sep 2020 07:58:40 +0200 Subject: [PATCH] Disable Met.no hourly weather by default (#39867) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Daniel Hjelseth Høyer --- homeassistant/components/met/weather.py | 22 +++++++++++++----- tests/components/met/test_weather.py | 27 ++++++++++++++++++++++- tests/components/onboarding/test_views.py | 2 +- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/met/weather.py b/homeassistant/components/met/weather.py index a53f66ab1dc..3355c497aab 100644 --- a/homeassistant/components/met/weather.py +++ b/homeassistant/components/met/weather.py @@ -104,7 +104,6 @@ class MetWeather(CoordinatorEntity, WeatherEntity): self._config = config self._is_metric = is_metric self._hourly = hourly - self._name_appendix = "-hourly" if hourly else "" @property def track_home(self): @@ -114,23 +113,34 @@ class MetWeather(CoordinatorEntity, WeatherEntity): @property def unique_id(self): """Return unique ID.""" + name_appendix = "" + if self._hourly: + name_appendix = "-hourly" if self.track_home: - return f"home{self._name_appendix}" + return f"home{name_appendix}" - return f"{self._config[CONF_LATITUDE]}-{self._config[CONF_LONGITUDE]}{self._name_appendix}" + return f"{self._config[CONF_LATITUDE]}-{self._config[CONF_LONGITUDE]}{name_appendix}" @property def name(self): """Return the name of the sensor.""" name = self._config.get(CONF_NAME) + name_appendix = "" + if self._hourly: + name_appendix = " Hourly" if name is not None: - return f"{name}{self._name_appendix}" + return f"{name}{name_appendix}" if self.track_home: - return f"{self.hass.config.location_name}{self._name_appendix}" + return f"{self.hass.config.location_name}{name_appendix}" - return f"{DEFAULT_NAME}{self._name_appendix}" + return f"{DEFAULT_NAME}{name_appendix}" + + @property + def entity_registry_enabled_default(self) -> bool: + """Return if the entity should be enabled when first added to the entity registry.""" + return not self._hourly @property def condition(self): diff --git a/tests/components/met/test_weather.py b/tests/components/met/test_weather.py index 064335a998c..242352c2498 100644 --- a/tests/components/met/test_weather.py +++ b/tests/components/met/test_weather.py @@ -1,13 +1,27 @@ """Test Met weather entity.""" +from homeassistant.components.met import DOMAIN +from homeassistant.components.weather import DOMAIN as WEATHER_DOMAIN + async def test_tracking_home(hass, mock_weather): """Test we track home.""" await hass.config_entries.flow.async_init("met", context={"source": "onboarding"}) await hass.async_block_till_done() - assert len(hass.states.async_entity_ids("weather")) == 2 + assert len(hass.states.async_entity_ids("weather")) == 1 assert len(mock_weather.mock_calls) == 4 + # Test the hourly sensor is disabled by default + registry = await hass.helpers.entity_registry.async_get_registry() + + state = hass.states.get("weather.test_home_hourly") + assert state is None + + entry = registry.async_get("weather.test_home_hourly") + assert entry + assert entry.disabled + assert entry.disabled_by == "integration" + # Test we track config await hass.config.async_update(latitude=10, longitude=20) await hass.async_block_till_done() @@ -21,6 +35,17 @@ async def test_tracking_home(hass, mock_weather): async def test_not_tracking_home(hass, mock_weather): """Test when we not track home.""" + + # Pre-create registry entry for disabled by default hourly weather + registry = await hass.helpers.entity_registry.async_get_registry() + registry.async_get_or_create( + WEATHER_DOMAIN, + DOMAIN, + "10-20-hourly", + suggested_object_id="somewhere_hourly", + disabled_by=None, + ) + await hass.config_entries.flow.async_init( "met", context={"source": "user"}, diff --git a/tests/components/onboarding/test_views.py b/tests/components/onboarding/test_views.py index 8b8f9761bdb..0d425642622 100644 --- a/tests/components/onboarding/test_views.py +++ b/tests/components/onboarding/test_views.py @@ -276,4 +276,4 @@ async def test_onboarding_core_sets_up_met(hass, hass_storage, hass_client): assert resp.status == 200 await hass.async_block_till_done() - assert len(hass.states.async_entity_ids("weather")) == 2 + assert len(hass.states.async_entity_ids("weather")) == 1