init home-assistant

This commit is contained in:
2026-11-19 15:15:32 -05:00
parent 2e81d9d300
commit feec6ef9fb
19230 changed files with 3335526 additions and 0 deletions

View File

@@ -0,0 +1 @@
"""Tests for random component."""

View File

@@ -0,0 +1,46 @@
"""The test for the Random binary sensor platform."""
from unittest.mock import patch
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
async def test_random_binary_sensor_on(hass: HomeAssistant) -> None:
"""Test the Random binary sensor."""
config = {"binary_sensor": {"platform": "random", "name": "test"}}
with patch(
"homeassistant.components.random.binary_sensor.getrandbits",
return_value=1,
):
assert await async_setup_component(
hass,
"binary_sensor",
config,
)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test")
assert state.state == "on"
async def test_random_binary_sensor_off(hass: HomeAssistant) -> None:
"""Test the Random binary sensor."""
config = {"binary_sensor": {"platform": "random", "name": "test"}}
with patch(
"homeassistant.components.random.binary_sensor.getrandbits",
return_value=False,
):
assert await async_setup_component(
hass,
"binary_sensor",
config,
)
await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test")
assert state.state == "off"

View File

@@ -0,0 +1,202 @@
"""Test the Random config flow."""
from typing import Any
from unittest.mock import patch
import pytest
from voluptuous import Invalid
from homeassistant import config_entries
from homeassistant.components.random import async_setup_entry
from homeassistant.components.random.const import DOMAIN
from homeassistant.components.sensor import SensorDeviceClass
from homeassistant.const import UnitOfEnergy, UnitOfPower
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
@pytest.mark.parametrize(
(
"entity_type",
"extra_input",
"extra_options",
),
[
(
"binary_sensor",
{},
{},
),
(
"sensor",
{
"device_class": SensorDeviceClass.POWER,
"unit_of_measurement": UnitOfPower.WATT,
},
{
"device_class": SensorDeviceClass.POWER,
"unit_of_measurement": UnitOfPower.WATT,
"minimum": 0,
"maximum": 20,
},
),
(
"sensor",
{},
{"minimum": 0, "maximum": 20},
),
],
)
async def test_config_flow(
hass: HomeAssistant,
entity_type: str,
extra_input: dict[str, Any],
extra_options: dict[str, Any],
) -> None:
"""Test the config flow."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.MENU
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"next_step_id": entity_type},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == entity_type
with patch(
"homeassistant.components.random.async_setup_entry", wraps=async_setup_entry
) as mock_setup_entry:
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"name": "My random entity",
**extra_input,
},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "My random entity"
assert result["data"] == {}
assert result["options"] == {
"name": "My random entity",
"entity_type": entity_type,
**extra_options,
}
assert len(mock_setup_entry.mock_calls) == 1
@pytest.mark.parametrize(
("device_class", "unit_of_measurement"),
[
(SensorDeviceClass.POWER, UnitOfEnergy.WATT_HOUR),
(SensorDeviceClass.ILLUMINANCE, UnitOfEnergy.WATT_HOUR),
],
)
async def test_wrong_uom(
hass: HomeAssistant, device_class: SensorDeviceClass, unit_of_measurement: str
) -> None:
"""Test entering a wrong unit of measurement."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.MENU
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{"next_step_id": "sensor"},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "sensor"
with pytest.raises(Invalid, match="is not a valid unit for device class"):
await hass.config_entries.flow.async_configure(
result["flow_id"],
{
"name": "My random entity",
"device_class": device_class,
"unit_of_measurement": unit_of_measurement,
},
)
@pytest.mark.parametrize(
(
"entity_type",
"extra_options",
"options_options",
),
[
(
"sensor",
{
"device_class": SensorDeviceClass.ENERGY,
"unit_of_measurement": UnitOfEnergy.WATT_HOUR,
"minimum": 0,
"maximum": 20,
},
{
"minimum": 10,
"maximum": 20,
"device_class": SensorDeviceClass.POWER,
"unit_of_measurement": UnitOfPower.WATT,
},
),
],
)
async def test_options(
hass: HomeAssistant,
entity_type: str,
extra_options,
options_options,
) -> None:
"""Test reconfiguring."""
random_config_entry = MockConfigEntry(
data={},
domain=DOMAIN,
options={
"name": "My random",
"entity_type": entity_type,
**extra_options,
},
title="My random",
)
random_config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(random_config_entry.entry_id)
await hass.async_block_till_done()
config_entry = hass.config_entries.async_entries(DOMAIN)[0]
result = await hass.config_entries.options.async_init(config_entry.entry_id)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == entity_type
assert "name" not in result["data_schema"].schema
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input=options_options,
)
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"] == {
"name": "My random",
"entity_type": entity_type,
**options_options,
}
assert config_entry.data == {}
assert config_entry.options == {
"name": "My random",
"entity_type": entity_type,
**options_options,
}
assert config_entry.title == "My random"

View File

@@ -0,0 +1,28 @@
"""The test for the random number sensor platform."""
from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
async def test_random_sensor(hass: HomeAssistant) -> None:
"""Test the Random number sensor."""
config = {
"sensor": {
"platform": "random",
"name": "test",
"minimum": 10,
"maximum": 20,
}
}
assert await async_setup_component(
hass,
"sensor",
config,
)
await hass.async_block_till_done()
state = hass.states.get("sensor.test")
assert int(state.state) <= config["sensor"]["maximum"]
assert int(state.state) >= config["sensor"]["minimum"]