-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
73 lines (60 loc) · 2.26 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""The Chicken Farm integration."""
from __future__ import annotations
from homeassistant.core import HomeAssistant
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME # Not used. Remove.
import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from .const import (
DOMAIN,
PLATFORMS,
CONF_FARM_NAME,
CONF_FARM_SIZE,
CONF_CHICKEN_TYPE,
DEFAULT_FARM_NAME,
DEFAULT_FARM_SIZE,
DEFAULT_CHICKEN_TYPE,
)
# Configuration schema for YAML (optional if using config flow)
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Optional(CONF_FARM_NAME, default=DEFAULT_FARM_NAME): cv.string,
vol.Optional(CONF_FARM_SIZE, default=DEFAULT_FARM_SIZE): cv.string,
vol.Optional(CONF_CHICKEN_TYPE, default=DEFAULT_CHICKEN_TYPE): cv.string,
}
),
},
extra=vol.ALLOW_EXTRA,
)
async def async_setup(hass: HomeAssistant, config: dict) -> bool:
"""Set up the Chicken Farm component from YAML."""
conf = config.get(DOMAIN)
hass.data.setdefault(DOMAIN, {})
if conf is not None:
# Trigger config flow for YAML configuration
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": "import"}, data=conf
)
)
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Chicken Farm from a config entry."""
hass.data.setdefault(DOMAIN, {})
hass.data[DOMAIN][entry.entry_id] = entry.data
# Forward the setup to the platforms (e.g., sensor, number)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
# The following line is not needed as there is no script platform
# from .script import async_setup_scripts
# await async_setup_scripts(hass)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
# Unload platforms (e.g., sensor, number)
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
# Remove the entry data from hass.data
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok