2019-03-17 03:44:05 +00:00
|
|
|
"""The mill component."""
|
2021-08-18 19:30:37 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
2021-07-21 05:41:08 +00:00
|
|
|
from mill import Mill
|
2020-05-10 13:44:05 +00:00
|
|
|
|
2021-07-21 05:41:08 +00:00
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
2021-08-18 19:30:37 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-07-21 05:41:08 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2021-08-18 19:30:37 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
2021-07-21 05:41:08 +00:00
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
2021-08-18 19:30:37 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-07-21 05:41:08 +00:00
|
|
|
PLATFORMS = ["climate", "sensor"]
|
2021-04-27 16:49:13 +00:00
|
|
|
|
2020-05-10 13:44:05 +00:00
|
|
|
|
2021-08-18 19:30:37 +00:00
|
|
|
class MillDataUpdateCoordinator(DataUpdateCoordinator):
|
|
|
|
"""Class to manage fetching Mill data."""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass: HomeAssistant,
|
|
|
|
*,
|
|
|
|
mill_data_connection: Mill,
|
|
|
|
) -> None:
|
|
|
|
"""Initialize global Mill data updater."""
|
|
|
|
self.mill_data_connection = mill_data_connection
|
|
|
|
|
|
|
|
super().__init__(
|
|
|
|
hass,
|
|
|
|
_LOGGER,
|
|
|
|
name=DOMAIN,
|
2021-10-26 19:15:33 +00:00
|
|
|
update_method=mill_data_connection.fetch_heater_and_sensor_data,
|
2021-08-18 19:30:37 +00:00
|
|
|
update_interval=timedelta(seconds=30),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-05-10 13:44:05 +00:00
|
|
|
async def async_setup_entry(hass, entry):
|
|
|
|
"""Set up the Mill heater."""
|
2021-07-21 05:41:08 +00:00
|
|
|
mill_data_connection = Mill(
|
|
|
|
entry.data[CONF_USERNAME],
|
|
|
|
entry.data[CONF_PASSWORD],
|
|
|
|
websession=async_get_clientsession(hass),
|
|
|
|
)
|
|
|
|
if not await mill_data_connection.connect():
|
|
|
|
raise ConfigEntryNotReady
|
|
|
|
|
2021-08-18 19:30:37 +00:00
|
|
|
hass.data[DOMAIN] = MillDataUpdateCoordinator(
|
|
|
|
hass,
|
|
|
|
mill_data_connection=mill_data_connection,
|
|
|
|
)
|
2021-07-21 05:41:08 +00:00
|
|
|
|
2021-08-18 19:30:37 +00:00
|
|
|
await hass.data[DOMAIN].async_config_entry_first_refresh()
|
2021-07-21 05:41:08 +00:00
|
|
|
|
2021-04-27 16:49:13 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2020-05-10 13:44:05 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-04-27 16:49:13 +00:00
|
|
|
async def async_unload_entry(hass, entry):
|
2020-05-10 13:44:05 +00:00
|
|
|
"""Unload a config entry."""
|
2021-04-27 16:49:13 +00:00
|
|
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|