Disable Met.no hourly weather by default (#39867)

Co-authored-by: Daniel Hjelseth Høyer <mail@dahoiv.net>
This commit is contained in:
Franck Nijhof 2020-09-10 07:58:40 +02:00 committed by GitHub
parent 6e79d49c80
commit b70df4ab18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 8 deletions

View File

@ -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):

View File

@ -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"},

View File

@ -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