Compare commits

...
Sign in to create a new pull request.

9 commits

Author SHA1 Message Date
Jesse Hills
a8d3bf8be8
device is always present at this stage now 2024-11-05 07:08:22 +13:00
Jesse Hills
35c5e598cb
Add dummy device 2024-11-05 07:07:50 +13:00
Jesse Hills
ef17733a6a
Check data is not none too 2024-11-05 06:51:23 +13:00
Jesse Hills
72d7788cf6
Update existing tests to account for entity not being created now instead of being unavailable 2024-11-04 17:23:22 +13:00
Jesse Hills
0b07f574e9
Add new test 2024-11-04 17:22:43 +13:00
Jesse Hills
2d3004d3b6
Check for device config in different position to allow creating the entity if config comes later. 2024-11-04 17:21:41 +13:00
Jesse Hills
a7a4241906
Check device info for not fully connected yet 2024-11-04 09:57:42 +13:00
Jesse Hills
522d98574e
assert not none 2024-11-04 09:42:42 +13:00
Jesse Hills
ed1f3eb4ab
Do not create ESPHome Dashboard update entity if no configuration found 2024-11-04 08:20:04 +13:00
2 changed files with 50 additions and 16 deletions

View file

@ -61,6 +61,8 @@ async def async_setup_entry(
if (dashboard := async_get_dashboard(hass)) is None:
return
entry_data = DomainData.get(hass).get_entry_data(entry)
assert entry_data.device_info is not None
device_name = entry_data.device_info.name
unsubs: list[CALLBACK_TYPE] = []
@callback
@ -72,13 +74,22 @@ async def async_setup_entry(
if not entry_data.available or not dashboard.last_update_success:
return
# Do not add Dashboard Entity if this device is not known to the ESPHome dashboard.
if dashboard.data is None or dashboard.data.get(device_name) is None:
return
for unsub in unsubs:
unsub()
unsubs.clear()
async_add_entities([ESPHomeDashboardUpdateEntity(entry_data, dashboard)])
if entry_data.available and dashboard.last_update_success:
if (
entry_data.available
and dashboard.last_update_success
and dashboard.data is not None
and dashboard.data.get(device_name)
):
_async_setup_update_entity()
return
@ -133,10 +144,8 @@ class ESPHomeDashboardUpdateEntity(
self._attr_supported_features = NO_FEATURES
self._attr_installed_version = device_info.esphome_version
device = coordinator.data.get(device_info.name)
if device is None:
self._attr_latest_version = None
else:
self._attr_latest_version = device["current_version"]
assert device is not None
self._attr_latest_version = device["current_version"]
@callback
def _handle_coordinator_update(self) -> None:

View file

@ -31,7 +31,6 @@ from homeassistant.const import (
STATE_OFF,
STATE_ON,
STATE_UNAVAILABLE,
STATE_UNKNOWN,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
@ -83,11 +82,6 @@ def stub_reconnect():
"supported_features": 0,
},
),
(
[],
STATE_UNKNOWN, # dashboard is available but device is unknown
{"supported_features": 0},
),
],
)
async def test_update_entity(
@ -408,11 +402,7 @@ async def test_update_becomes_available_at_runtime(
)
await hass.async_block_till_done()
state = hass.states.get("update.test_firmware")
assert state is not None
features = state.attributes[ATTR_SUPPORTED_FEATURES]
# There are no devices on the dashboard so no
# way to tell the version so install is disabled
assert features is UpdateEntityFeature(0)
assert state is None
# A device gets added to the dashboard
mock_dashboard["configured"] = [
@ -433,6 +423,41 @@ async def test_update_becomes_available_at_runtime(
assert features is UpdateEntityFeature.INSTALL
async def test_update_entity_not_present_with_dashboard_but_unknown_device(
hass: HomeAssistant,
mock_client: APIClient,
mock_esphome_device: Callable[
[APIClient, list[EntityInfo], list[UserService], list[EntityState]],
Awaitable[MockESPHomeDevice],
],
mock_dashboard: dict[str, Any],
) -> None:
"""Test ESPHome update entity does not get created if the device is unknown to the dashboard."""
await mock_esphome_device(
mock_client=mock_client,
entity_info=[],
user_service=[],
states=[],
)
mock_dashboard["configured"] = [
{
"name": "other-test",
"current_version": "2023.2.0-dev",
"configuration": "other-test.yaml",
}
]
state = hass.states.get("update.test_firmware")
assert state is None
await async_get_dashboard(hass).async_refresh()
await hass.async_block_till_done()
state = hass.states.get("update.none_firmware")
assert state is None
async def test_generic_device_update_entity(
hass: HomeAssistant,
mock_client: APIClient,