Skip to content

Commit

Permalink
Add test_async_setup_platform test
Browse files Browse the repository at this point in the history
  • Loading branch information
Limych committed Mar 5, 2021
1 parent 4c809fb commit b18dad7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
2 changes: 1 addition & 1 deletion custom_components/average/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# Base component constants
NAME = "Average Sensor"
DOMAIN = "average"
VERSION = "1.7.2"
VERSION = "1.7.3.dev0"
ISSUE_URL = "https://github.com/Limych/ha-average/issues"

STARTUP_MESSAGE = f"""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/average/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "average",
"name": "Average Sensor",
"version": "1.7.2",
"version": "1.7.3.dev0",
"documentation": "https://github.com/Limych/ha-average",
"issue_tracker": "https://github.com/Limych/ha-average/issues",
"dependencies": [
Expand Down
2 changes: 1 addition & 1 deletion custom_components/average/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def icon(self) -> Optional[str]:
return self._icon

@property
def device_state_attributes(self) -> Optional[Dict[str, Any]]:
def state_attributes(self) -> Optional[Dict[str, Any]]:
"""Return the state attributes."""
state_attr = {
attr: getattr(self, attr)
Expand Down
53 changes: 36 additions & 17 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers.template import Template
from homeassistant.setup import async_setup_component
from pytest import raises
from pytest_homeassistant_custom_component.common import assert_setup_component
from voluptuous import Invalid

from custom_components.average.const import CONF_DURATION, CONF_END, CONF_START, DOMAIN
Expand Down Expand Up @@ -131,7 +133,7 @@ async def test_setup_platform(hass: HomeAssistant):
assert async_add_entities.called


async def test_sensor_initialization(default_sensor):
async def test_entity_initialization(default_sensor):
"""Test sensor initialization."""
expected_attributes = {
"available_sources": 0,
Expand All @@ -147,22 +149,39 @@ async def test_sensor_initialization(default_sensor):
assert default_sensor.state == STATE_UNAVAILABLE
assert default_sensor.unit_of_measurement is None
assert default_sensor.icon is None
assert default_sensor.device_state_attributes == expected_attributes


#
#
# async def test_async_added_to_hass(hass: HomeAssistant):
# """Test async added to hass."""
# config = {
# CONF_PLATFORM: DOMAIN,
# CONF_NAME: "test",
# CONF_ENTITIES: ["sensor.test_monitored"],
# CONF_DURATION: timedelta(minutes=5),
# }
#
# await async_setup_component(hass, SENSOR, {SENSOR: config})
# hass.block_till_done()
assert default_sensor.state_attributes == expected_attributes


async def test_async_setup_platform(hass: HomeAssistant):
"""Test platform setup."""
mock_sensor = {
"platform": "template",
"sensors": {"test_monitored": {"value_template": "{{ 2 }}"}},
}
config = {
CONF_PLATFORM: DOMAIN,
CONF_ENTITIES: ["sensor.test_monitored", "sensor.nonexistent"],
}

with assert_setup_component(2, "sensor"):
assert await async_setup_component(
hass,
"sensor",
{
"sensor": [
config,
mock_sensor,
]
},
)
await hass.async_block_till_done()

await hass.async_start()
await hass.async_block_till_done()

state = hass.states.get("sensor.average")
assert state is not None
assert state.state == "2.0"


# pylint: disable=protected-access
Expand Down

0 comments on commit b18dad7

Please sign in to comment.