Skip to content

Commit

Permalink
Fix work area sensor in Husqvarna Automower (#121228)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas55555 authored and frenck committed Jul 5, 2024
1 parent 1bb4d62 commit ac668dc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
11 changes: 9 additions & 2 deletions homeassistant/components/husqvarna_automower/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,23 +184,30 @@
RestrictedReasons.WEEK_SCHEDULE.lower(),
]

STATE_NO_WORK_AREA_ACTIVE = "no_work_area_active"


@callback
def _get_work_area_names(data: MowerAttributes) -> list[str]:
"""Return a list with all work area names."""
if TYPE_CHECKING:
# Sensor does not get created if it is None
assert data.work_areas is not None
return [data.work_areas[work_area_id].name for work_area_id in data.work_areas]
work_area_list = [
data.work_areas[work_area_id].name for work_area_id in data.work_areas
]
work_area_list.append(STATE_NO_WORK_AREA_ACTIVE)
return work_area_list


@callback
def _get_current_work_area_name(data: MowerAttributes) -> str:
"""Return the name of the current work area."""
if data.mower.work_area_id is None:
return STATE_NO_WORK_AREA_ACTIVE
if TYPE_CHECKING:
# Sensor does not get created if values are None
assert data.work_areas is not None
assert data.mower.work_area_id is not None
return data.work_areas[data.mower.work_area_id].name


Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/husqvarna_automower/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@
"work_area": {
"name": "Work area",
"state": {
"my_lawn": "My lawn"
"my_lawn": "My lawn",
"no_work_area_active": "No work area active"
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,7 @@
'Front lawn',
'Back lawn',
'my_lawn',
'no_work_area_active',
]),
}),
'config_entry_id': <ANY>,
Expand Down Expand Up @@ -1097,6 +1098,7 @@
'Front lawn',
'Back lawn',
'my_lawn',
'no_work_area_active',
]),
}),
'context': <ANY>,
Expand Down
32 changes: 32 additions & 0 deletions tests/components/husqvarna_automower/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,38 @@ async def test_next_start_sensor(
assert state.state == STATE_UNKNOWN


async def test_work_area_sensor(
hass: HomeAssistant,
mock_automower_client: AsyncMock,
mock_config_entry: MockConfigEntry,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test the work area sensor."""
await setup_integration(hass, mock_config_entry)
state = hass.states.get("sensor.test_mower_1_work_area")
assert state is not None
assert state.state == "Front lawn"

values = mower_list_to_dictionary_dataclass(
load_json_value_fixture("mower.json", DOMAIN)
)
values[TEST_MOWER_ID].mower.work_area_id = None
mock_automower_client.get_status.return_value = values
freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
state = hass.states.get("sensor.test_mower_1_work_area")
assert state.state == "no_work_area_active"

values[TEST_MOWER_ID].mower.work_area_id = 0
mock_automower_client.get_status.return_value = values
freezer.tick(SCAN_INTERVAL)
async_fire_time_changed(hass)
await hass.async_block_till_done()
state = hass.states.get("sensor.test_mower_1_work_area")
assert state.state == "my_lawn"


@pytest.mark.parametrize(
("sensor_to_test"),
[
Expand Down

0 comments on commit ac668dc

Please sign in to comment.