Load ecobee notify platform via discovery (#78558)

* Fix ecobee notify platform KeyError

* set up notify platform via discovery

* address comments

* fix isort

* Apply suggestions from code review

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
kevdliu 2022-10-21 17:19:26 -04:00 committed by GitHub
parent f21fabba17
commit da099532fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 18 deletions

View File

@ -5,13 +5,21 @@ from pyecobee import ECOBEE_API_KEY, ECOBEE_REFRESH_TOKEN, Ecobee, ExpiredTokenE
import voluptuous as vol
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import CONF_API_KEY
from homeassistant.const import CONF_API_KEY, CONF_NAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import config_validation as cv, discovery
from homeassistant.helpers.typing import ConfigType
from homeassistant.util import Throttle
from .const import _LOGGER, CONF_REFRESH_TOKEN, DATA_ECOBEE_CONFIG, DOMAIN, PLATFORMS
from .const import (
_LOGGER,
ATTR_CONFIG_ENTRY_ID,
CONF_REFRESH_TOKEN,
DATA_ECOBEE_CONFIG,
DATA_HASS_CONFIG,
DOMAIN,
PLATFORMS,
)
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=180)
@ -30,7 +38,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
migrating from the old ecobee integration. Otherwise, the user will have to
continue setting up the integration via the config flow.
"""
hass.data[DATA_ECOBEE_CONFIG] = config.get(DOMAIN, {})
hass.data[DATA_HASS_CONFIG] = config
if not hass.config_entries.async_entries(DOMAIN) and hass.data[DATA_ECOBEE_CONFIG]:
# No config entry exists and configuration.yaml config exists, trigger the import flow.
@ -63,6 +73,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
hass.async_create_task(
discovery.async_load_platform(
hass,
Platform.NOTIFY,
DOMAIN,
{CONF_NAME: entry.title, ATTR_CONFIG_ENTRY_ID: entry.entry_id},
hass.data[DATA_HASS_CONFIG],
)
)
return True

View File

@ -20,8 +20,9 @@ _LOGGER = logging.getLogger(__package__)
DOMAIN = "ecobee"
DATA_ECOBEE_CONFIG = "ecobee_config"
DATA_HASS_CONFIG = "ecobee_hass_config"
ATTR_CONFIG_ENTRY_ID = "entry_id"
CONF_INDEX = "index"
CONF_REFRESH_TOKEN = "refresh_token"
ECOBEE_MODEL_TO_NAME = {

View File

@ -1,31 +1,33 @@
"""Support for Ecobee Send Message service."""
import voluptuous as vol
from homeassistant.components.notify import PLATFORM_SCHEMA, BaseNotificationService
import homeassistant.helpers.config_validation as cv
from homeassistant.components.notify import ATTR_TARGET, BaseNotificationService
from .const import CONF_INDEX, DOMAIN
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Optional(CONF_INDEX, default=0): cv.positive_int}
)
from .const import DOMAIN
def get_service(hass, config, discovery_info=None):
"""Get the Ecobee notification service."""
if discovery_info is None:
return None
data = hass.data[DOMAIN]
index = config.get(CONF_INDEX)
return EcobeeNotificationService(data, index)
return EcobeeNotificationService(data.ecobee)
class EcobeeNotificationService(BaseNotificationService):
"""Implement the notification service for the Ecobee thermostat."""
def __init__(self, data, thermostat_index):
def __init__(self, ecobee):
"""Initialize the service."""
self.data = data
self.thermostat_index = thermostat_index
self.ecobee = ecobee
def send_message(self, message="", **kwargs):
"""Send a message."""
self.data.ecobee.send_message(self.thermostat_index, message)
targets = kwargs.get(ATTR_TARGET)
if not targets:
raise ValueError("Missing required argument: target")
for target in targets:
thermostat_index = int(target)
self.ecobee.send_message(thermostat_index, message)