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 import voluptuous as vol
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry 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.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.helpers.typing import ConfigType
from homeassistant.util import Throttle 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) 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 migrating from the old ecobee integration. Otherwise, the user will have to
continue setting up the integration via the config flow. continue setting up the integration via the config flow.
""" """
hass.data[DATA_ECOBEE_CONFIG] = config.get(DOMAIN, {}) 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]: 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. # 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) 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 return True

View File

@ -20,8 +20,9 @@ _LOGGER = logging.getLogger(__package__)
DOMAIN = "ecobee" DOMAIN = "ecobee"
DATA_ECOBEE_CONFIG = "ecobee_config" 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" CONF_REFRESH_TOKEN = "refresh_token"
ECOBEE_MODEL_TO_NAME = { ECOBEE_MODEL_TO_NAME = {

View File

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