Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Melnor RainCloud sprinkler water timer."""
2 
3 from datetime import timedelta
4 import logging
5 
6 from raincloudy.core import RainCloudy
7 from requests.exceptions import ConnectTimeout, HTTPError
8 import voluptuous as vol
9 
10 from homeassistant.components import persistent_notification
11 from homeassistant.const import CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_USERNAME
12 from homeassistant.core import HomeAssistant
14 from homeassistant.helpers.dispatcher import dispatcher_send
15 from homeassistant.helpers.event import track_time_interval
16 from homeassistant.helpers.typing import ConfigType
17 
18 from .const import DATA_RAINCLOUD, SIGNAL_UPDATE_RAINCLOUD
19 
20 _LOGGER = logging.getLogger(__name__)
21 
22 NOTIFICATION_ID = "raincloud_notification"
23 NOTIFICATION_TITLE = "Rain Cloud Setup"
24 
25 DOMAIN = "raincloud"
26 
27 SCAN_INTERVAL = timedelta(seconds=20)
28 
29 CONFIG_SCHEMA = vol.Schema(
30  {
31  DOMAIN: vol.Schema(
32  {
33  vol.Required(CONF_USERNAME): cv.string,
34  vol.Required(CONF_PASSWORD): cv.string,
35  vol.Optional(CONF_SCAN_INTERVAL, default=SCAN_INTERVAL): cv.time_period,
36  }
37  )
38  },
39  extra=vol.ALLOW_EXTRA,
40 )
41 
42 
43 def setup(hass: HomeAssistant, config: ConfigType) -> bool:
44  """Set up the Melnor RainCloud component."""
45  conf = config[DOMAIN]
46  username = conf.get(CONF_USERNAME)
47  password = conf.get(CONF_PASSWORD)
48  scan_interval = conf.get(CONF_SCAN_INTERVAL)
49 
50  try:
51  raincloud = RainCloudy(username=username, password=password)
52  if not raincloud.is_connected:
53  raise HTTPError # noqa: TRY301
54  hass.data[DATA_RAINCLOUD] = RainCloudHub(raincloud)
55  except (ConnectTimeout, HTTPError) as ex:
56  _LOGGER.error("Unable to connect to Rain Cloud service: %s", str(ex))
57  persistent_notification.create(
58  hass,
59  f"Error: {ex}<br />You will need to restart hass after fixing.",
60  title=NOTIFICATION_TITLE,
61  notification_id=NOTIFICATION_ID,
62  )
63  return False
64 
65  def hub_refresh(event_time):
66  """Call Raincloud hub to refresh information."""
67  _LOGGER.debug("Updating RainCloud Hub component")
68  hass.data[DATA_RAINCLOUD].data.update()
69  dispatcher_send(hass, SIGNAL_UPDATE_RAINCLOUD)
70 
71  # Call the Raincloud API to refresh updates
72  track_time_interval(hass, hub_refresh, scan_interval)
73 
74  return True
75 
76 
78  """Representation of a base RainCloud device."""
79 
80  def __init__(self, data):
81  """Initialize the entity."""
82  self.datadata = data
bool setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:43
None dispatcher_send(HomeAssistant hass, str signal, *Any args)
Definition: dispatcher.py:137