Skip to content

Commit

Permalink
update tests for disabled entity
Browse files Browse the repository at this point in the history
  • Loading branch information
marcolivierarsenault committed Dec 6, 2023
1 parent f800681 commit da51770
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
62 changes: 62 additions & 0 deletions tests/test_binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
""" Binary_sensor Tests"""
from unittest.mock import patch

from homeassistant.helpers import entity_registry as er
import pytest
from pytest_homeassistant_custom_component.common import MockConfigEntry

Expand Down Expand Up @@ -81,6 +82,21 @@ async def test_update_available(hass):
assert await async_setup_entry(hass, config_entry)
await hass.async_block_till_done()

entity_registry = er.async_get(hass)
entity = entity_registry.async_get("binary_sensor.mainsail_update_available")
assert entity
assert entity.disabled
entity_registry.async_update_entity(
"binary_sensor.mainsail_update_available",
disabled_by=None,
)
await hass.config_entries.async_reload(config_entry.entry_id)
await hass.async_block_till_done()

entity = entity_registry.async_get("binary_sensor.mainsail_update_available")
assert entity
assert not entity.disabled

state = hass.states.get("binary_sensor.mainsail_update_available")
assert state.state == "on"

Expand All @@ -92,6 +108,21 @@ async def test_update_available_system(hass, get_machine_update_status):
assert await async_setup_entry(hass, config_entry)
await hass.async_block_till_done()

entity_registry = er.async_get(hass)
entity = entity_registry.async_get("binary_sensor.mainsail_update_available")
assert entity
assert entity.disabled
entity_registry.async_update_entity(
"binary_sensor.mainsail_update_available",
disabled_by=None,
)
await hass.config_entries.async_reload(config_entry.entry_id)
await hass.async_block_till_done()

entity = entity_registry.async_get("binary_sensor.mainsail_update_available")
assert entity
assert not entity.disabled

state = hass.states.get("binary_sensor.mainsail_update_available")
assert state.state == "on"

Expand All @@ -103,6 +134,21 @@ async def test_update_available_component(hass, get_machine_update_status):
assert await async_setup_entry(hass, config_entry)
await hass.async_block_till_done()

entity_registry = er.async_get(hass)
entity = entity_registry.async_get("binary_sensor.mainsail_update_available")
assert entity
assert entity.disabled
entity_registry.async_update_entity(
"binary_sensor.mainsail_update_available",
disabled_by=None,
)
await hass.config_entries.async_reload(config_entry.entry_id)
await hass.async_block_till_done()

entity = entity_registry.async_get("binary_sensor.mainsail_update_available")
assert entity
assert not entity.disabled

state = hass.states.get("binary_sensor.mainsail_update_available")
assert state.state == "on"

Expand All @@ -115,5 +161,21 @@ async def test_update_available_no_update(hass, get_machine_update_status):
assert await async_setup_entry(hass, config_entry)
await hass.async_block_till_done()

entity_registry = er.async_get(hass)
entity = entity_registry.async_get("binary_sensor.mainsail_update_available")
assert entity
assert entity.disabled
entity_registry.async_update_entity(
"binary_sensor.mainsail_update_available",
disabled_by=None,
)

await hass.config_entries.async_reload(config_entry.entry_id)
await hass.async_block_till_done()

entity = entity_registry.async_get("binary_sensor.mainsail_update_available")
assert entity
assert not entity.disabled

state = hass.states.get("binary_sensor.mainsail_update_available")
assert state.state == "off"
40 changes: 37 additions & 3 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime as dt
from unittest.mock import patch

from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity_registry import async_get as get_entity_registry
import pytest
from pytest_homeassistant_custom_component.common import (
Expand Down Expand Up @@ -106,9 +107,6 @@ async def test_sensor_services_update(hass, get_data):
("mainsail_slicer_print_duration_estimate", "8232.0"),
("mainsail_object_height", "62.6"),
("mainsail_speed_factor", "200.0"),
("mainsail_machine_update_system", "8 packages can be upgraded"),
("mainsail_crownest", "v4.0.4-6 > v4.1.1-1"),
("mainsail_mainsail", "v2.8.0"),
],
)
async def test_sensors(
Expand All @@ -124,6 +122,42 @@ async def test_sensors(
assert hass.states.get(f"sensor.{sensor}").state == value


# test all sensors
@pytest.mark.parametrize(
"sensor, value",
[
("mainsail_machine_update_system", "8 packages can be upgraded"),
],
)
async def test_disabled_sensors(
hass,
sensor,
value,
):
config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")
config_entry.add_to_hass(hass)
assert await async_setup_entry(hass, config_entry)
await hass.async_block_till_done()

entity_registry = er.async_get(hass)
entity = entity_registry.async_get(f"sensor.{sensor}")
assert entity
assert entity.disabled
entity_registry.async_update_entity(
f"sensor.{sensor}",
disabled_by=None,
)
await hass.config_entries.async_reload(config_entry.entry_id)
await hass.async_block_till_done()

entity = entity_registry.async_get(f"sensor.{sensor}")
assert entity
assert not entity.disabled

state = hass.states.get(f"sensor.{sensor}")
assert state.state == value


# test sensor affected by not printing state
@pytest.mark.parametrize(
"sensor_not_printing, value",
Expand Down

0 comments on commit da51770

Please sign in to comment.