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 the worldclock component."""

View File

@@ -0,0 +1,66 @@
"""Fixtures for the Worldclock integration."""
from __future__ import annotations
from collections.abc import Generator
from typing import Any
from unittest.mock import AsyncMock, patch
import pytest
from homeassistant.components.worldclock.const import (
CONF_TIME_FORMAT,
DEFAULT_NAME,
DEFAULT_TIME_STR_FORMAT,
DOMAIN,
)
from homeassistant.config_entries import SOURCE_USER
from homeassistant.const import CONF_NAME, CONF_TIME_ZONE
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
@pytest.fixture
def mock_setup_entry() -> Generator[AsyncMock]:
"""Automatically patch setup."""
with patch(
"homeassistant.components.worldclock.async_setup_entry",
return_value=True,
) as mock_setup_entry:
yield mock_setup_entry
@pytest.fixture(name="get_config")
async def get_config_to_integration_load() -> dict[str, Any]:
"""Return configuration.
To override the config, tests can be marked with:
@pytest.mark.parametrize("get_config", [{...}])
"""
return {
CONF_NAME: DEFAULT_NAME,
CONF_TIME_ZONE: "America/New_York",
CONF_TIME_FORMAT: DEFAULT_TIME_STR_FORMAT,
}
@pytest.fixture(name="loaded_entry")
async def load_integration(
hass: HomeAssistant, get_config: dict[str, Any]
) -> MockConfigEntry:
"""Set up the Worldclock integration in Home Assistant."""
config_entry = MockConfigEntry(
domain=DOMAIN,
title=DEFAULT_NAME,
source=SOURCE_USER,
options=get_config,
entry_id="1",
)
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
return config_entry

View File

@@ -0,0 +1,104 @@
"""Test the Worldclock config flow."""
from __future__ import annotations
from unittest.mock import AsyncMock
from homeassistant import config_entries
from homeassistant.components.worldclock.const import (
CONF_TIME_FORMAT,
DEFAULT_NAME,
DEFAULT_TIME_STR_FORMAT,
DOMAIN,
)
from homeassistant.const import CONF_NAME, CONF_TIME_ZONE
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
"""Test we get the form."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["step_id"] == "user"
assert result["type"] is FlowResultType.FORM
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_NAME: DEFAULT_NAME,
CONF_TIME_ZONE: "America/New_York",
},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["version"] == 1
assert result["options"] == {
CONF_NAME: DEFAULT_NAME,
CONF_TIME_ZONE: "America/New_York",
CONF_TIME_FORMAT: DEFAULT_TIME_STR_FORMAT,
}
assert len(mock_setup_entry.mock_calls) == 1
async def test_options_flow(hass: HomeAssistant, loaded_entry: MockConfigEntry) -> None:
"""Test options flow."""
result = await hass.config_entries.options.async_init(loaded_entry.entry_id)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "init"
result = await hass.config_entries.options.async_configure(
result["flow_id"],
user_input={
CONF_TIME_FORMAT: "%a, %b %d, %Y %I:%M %p",
},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["data"] == {
CONF_NAME: DEFAULT_NAME,
CONF_TIME_ZONE: "America/New_York",
CONF_TIME_FORMAT: "%a, %b %d, %Y %I:%M %p",
}
await hass.async_block_till_done()
# Check the entity was updated, no new entity was created
assert len(hass.states.async_all()) == 1
state = hass.states.get("sensor.worldclock_sensor")
assert state is not None
async def test_entry_already_exist(
hass: HomeAssistant, loaded_entry: MockConfigEntry
) -> None:
"""Test abort when entry already exist."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["step_id"] == "user"
assert result["type"] is FlowResultType.FORM
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_NAME: DEFAULT_NAME,
CONF_TIME_ZONE: "America/New_York",
CONF_TIME_FORMAT: DEFAULT_TIME_STR_FORMAT,
},
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"

View File

@@ -0,0 +1,17 @@
"""Test Worldclock component setup process."""
from __future__ import annotations
from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry
async def test_unload_entry(hass: HomeAssistant, loaded_entry: MockConfigEntry) -> None:
"""Test unload an entry."""
assert loaded_entry.state is ConfigEntryState.LOADED
assert await hass.config_entries.async_unload(loaded_entry.entry_id)
await hass.async_block_till_done()
assert loaded_entry.state is ConfigEntryState.NOT_LOADED

View File

@@ -0,0 +1,83 @@
"""The test for the World clock sensor platform."""
from datetime import tzinfo
import pytest
from homeassistant.components.worldclock.const import (
CONF_TIME_FORMAT,
DEFAULT_NAME,
DOMAIN,
)
from homeassistant.const import CONF_NAME, CONF_TIME_ZONE
from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant
from homeassistant.helpers import issue_registry as ir
from homeassistant.setup import async_setup_component
import homeassistant.util.dt as dt_util
from tests.common import MockConfigEntry
@pytest.fixture
async def time_zone() -> tzinfo | None:
"""Fixture for time zone."""
return await dt_util.async_get_time_zone("America/New_York")
async def test_time_imported_from_yaml(
hass: HomeAssistant, time_zone: tzinfo | None, issue_registry: ir.IssueRegistry
) -> None:
"""Test the time at a different location."""
config = {"sensor": {"platform": "worldclock", "time_zone": "America/New_York"}}
assert await async_setup_component(
hass,
"sensor",
config,
)
await hass.async_block_till_done()
state = hass.states.get("sensor.worldclock_sensor")
assert state is not None
assert state.state == dt_util.now(time_zone=time_zone).strftime("%H:%M")
issue = issue_registry.async_get_issue(
HOMEASSISTANT_DOMAIN, f"deprecated_yaml_{DOMAIN}"
)
assert issue
assert issue.issue_domain == DOMAIN
async def test_time_from_config_entry(
hass: HomeAssistant, time_zone: tzinfo | None, loaded_entry: MockConfigEntry
) -> None:
"""Test the time at a different location."""
state = hass.states.get("sensor.worldclock_sensor")
assert state is not None
assert state.state == dt_util.now(time_zone=time_zone).strftime("%H:%M")
@pytest.mark.parametrize(
"get_config",
[
{
CONF_NAME: DEFAULT_NAME,
CONF_TIME_ZONE: "America/New_York",
CONF_TIME_FORMAT: "%a, %b %d, %Y %I:%M %p",
}
],
)
async def test_time_format(
hass: HomeAssistant, time_zone: tzinfo | None, loaded_entry: MockConfigEntry
) -> None:
"""Test time_format setting."""
state = hass.states.get("sensor.worldclock_sensor")
assert state is not None
assert state.state == dt_util.now(time_zone=time_zone).strftime(
"%a, %b %d, %Y %I:%M %p"
)