๐Ÿ“ฆ barneyman / sunload

๐Ÿ“„ __init__.py ยท 89 lines
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89# inspired by https://github.com/home-assistant/example-custom-config/tree/master/custom_components/example_load_platform

import voluptuous as vol
import logging

_LOGGER = logging.getLogger(__name__)

# The domain of your component. Should be equal to the name of your component.
DOMAIN = "sunload"

import json
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType
import homeassistant.helpers.config_validation as cv

offSchema = vol.Schema({vol.Required("at"): cv.time})

# TODO enable checking
CONFIG_SCHEMA = vol.Schema(
    {
        DOMAIN: vol.Schema(
            {
                vol.Required("tempsensor"): cv.string,
                vol.Required("threshold"): vol.All(
                    vol.Coerce(float), vol.Range(min=0, max=100)
                ),
                vol.Required("jitter"): vol.All(
                    vol.Coerce(float), vol.Range(min=0, max=5)
                ),
                vol.Required("instances"): vol.All(
                    cv.ensure_list,
                    [
                        {
                            vol.Required("name"): cv.string,
                            vol.Required("azimuth"): vol.Schema(
                                {
                                    vol.Required("min"): vol.All(
                                        vol.Coerce(float), vol.Range(min=0, max=359)
                                    ),
                                    vol.Required("max"): vol.All(
                                        vol.Coerce(float), vol.Range(min=0, max=359)
                                    ),
                                }
                            ),
                            vol.Optional("elevation"): vol.Schema(
                                {
                                    vol.Optional("min"): vol.All(
                                        vol.Coerce(float), vol.Range(min=0, max=90)
                                    ),
                                    vol.Optional("max"): vol.All(
                                        vol.Coerce(float), vol.Range(min=0, max=90)
                                    ),
                                }
                            ),
                        }
                    ],
                ),
            }
        )
    },
    extra=vol.ALLOW_EXTRA,
)

suninstances = []


async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
    """Your controller/hub specific code."""

    _LOGGER.info("%s async_setup %s", DOMAIN, config)

    if DOMAIN in config:

        myConfig = config.get(DOMAIN)

        hass.data[DOMAIN] = myConfig

        _LOGGER.info("%s config %s", DOMAIN, myConfig)

        # see hass.helpers.discovery.async_load_platform for the create task
        hass.async_create_task(
            hass.helpers.discovery.async_load_platform("sensor", DOMAIN, None, config)
        )

    else:
        _LOGGER.error("No config for %s", (DOMAIN))

    return True