mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 16:57:53 +00:00
Remove YAML support from sense (#49935)
This commit is contained in:
parent
ef2b8bbca8
commit
002b068c0a
@ -1,4 +1,5 @@
|
||||
"""Support for monitoring a Sense energy sensor."""
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
@ -7,9 +8,8 @@ from sense_energy import (
|
||||
SenseAPITimeoutException,
|
||||
SenseAuthenticationException,
|
||||
)
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
CONF_EMAIL,
|
||||
CONF_PASSWORD,
|
||||
@ -19,42 +19,25 @@ from homeassistant.const import (
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.helpers.event import async_track_time_interval
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from .const import (
|
||||
ACTIVE_UPDATE_RATE,
|
||||
DEFAULT_TIMEOUT,
|
||||
DOMAIN,
|
||||
EVENT_STOP_REMOVE,
|
||||
SENSE_DATA,
|
||||
SENSE_DEVICE_UPDATE,
|
||||
SENSE_DEVICES_DATA,
|
||||
SENSE_DISCOVERED_DEVICES_DATA,
|
||||
SENSE_TIMEOUT_EXCEPTIONS,
|
||||
SENSE_TRENDS_COORDINATOR,
|
||||
TRACK_TIME_REMOVE,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORMS = ["binary_sensor", "sensor"]
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema(
|
||||
{
|
||||
DOMAIN: vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_EMAIL): cv.string,
|
||||
vol.Required(CONF_PASSWORD): cv.string,
|
||||
vol.Optional(CONF_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int,
|
||||
}
|
||||
)
|
||||
},
|
||||
extra=vol.ALLOW_EXTRA,
|
||||
)
|
||||
|
||||
|
||||
class SenseDevicesData:
|
||||
"""Data for each sense device."""
|
||||
@ -65,36 +48,13 @@ class SenseDevicesData:
|
||||
|
||||
def set_devices_data(self, devices):
|
||||
"""Store a device update."""
|
||||
self._data_by_device = {}
|
||||
for device in devices:
|
||||
self._data_by_device[device["id"]] = device
|
||||
self._data_by_device = {device["id"]: device for device in devices}
|
||||
|
||||
def get_device_by_id(self, sense_device_id):
|
||||
"""Get the latest device data."""
|
||||
return self._data_by_device.get(sense_device_id)
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: dict):
|
||||
"""Set up the Sense component."""
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
conf = config.get(DOMAIN)
|
||||
if not conf:
|
||||
return True
|
||||
|
||||
hass.async_create_task(
|
||||
hass.config_entries.flow.async_init(
|
||||
DOMAIN,
|
||||
context={"source": SOURCE_IMPORT},
|
||||
data={
|
||||
CONF_EMAIL: conf[CONF_EMAIL],
|
||||
CONF_PASSWORD: conf[CONF_PASSWORD],
|
||||
CONF_TIMEOUT: conf[CONF_TIMEOUT],
|
||||
},
|
||||
)
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||
"""Set up Sense from a config entry."""
|
||||
|
||||
@ -136,9 +96,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||
# This can take longer than 60s and we already know
|
||||
# sense is online since get_discovered_device_data was
|
||||
# successful so we do it later.
|
||||
hass.loop.create_task(trends_coordinator.async_request_refresh())
|
||||
asyncio.create_task(trends_coordinator.async_request_refresh())
|
||||
|
||||
data = hass.data[DOMAIN][entry.entry_id] = {
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
|
||||
SENSE_DATA: gateway,
|
||||
SENSE_DEVICES_DATA: sense_devices_data,
|
||||
SENSE_TRENDS_COORDINATOR: trends_coordinator,
|
||||
@ -167,9 +127,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||
def _remove_update_callback_at_stop(event):
|
||||
remove_update_callback()
|
||||
|
||||
data[TRACK_TIME_REMOVE] = remove_update_callback
|
||||
data[EVENT_STOP_REMOVE] = hass.bus.async_listen_once(
|
||||
EVENT_HOMEASSISTANT_STOP, _remove_update_callback_at_stop
|
||||
entry.async_on_unload(remove_update_callback)
|
||||
entry.async_on_unload(
|
||||
hass.bus.async_listen_once(
|
||||
EVENT_HOMEASSISTANT_STOP, _remove_update_callback_at_stop
|
||||
)
|
||||
)
|
||||
|
||||
return True
|
||||
@ -178,11 +140,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
|
||||
"""Unload a config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
data = hass.data[DOMAIN][entry.entry_id]
|
||||
data[EVENT_STOP_REMOVE]()
|
||||
data[TRACK_TIME_REMOVE]()
|
||||
|
||||
if unload_ok:
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
|
@ -63,10 +63,3 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
return self.async_show_form(
|
||||
step_id="user", data_schema=DATA_SCHEMA, errors=errors
|
||||
)
|
||||
|
||||
async def async_step_import(self, user_input):
|
||||
"""Handle import."""
|
||||
await self.async_set_unique_id(user_input[CONF_EMAIL])
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
return await self.async_step_user(user_input)
|
||||
|
@ -14,9 +14,6 @@ SENSE_DEVICES_DATA = "sense_devices_data"
|
||||
SENSE_DISCOVERED_DEVICES_DATA = "sense_discovered_devices"
|
||||
SENSE_TRENDS_COORDINATOR = "sense_trends_coordinator"
|
||||
|
||||
TRACK_TIME_REMOVE = "track_time_remove_callback"
|
||||
EVENT_STOP_REMOVE = "event_stop_remove_callback"
|
||||
|
||||
ACTIVE_NAME = "Energy"
|
||||
ACTIVE_TYPE = "active"
|
||||
|
||||
|
@ -17,8 +17,6 @@ async def test_form(hass):
|
||||
assert result["errors"] == {}
|
||||
|
||||
with patch("sense_energy.ASyncSenseable.authenticate", return_value=True,), patch(
|
||||
"homeassistant.components.sense.async_setup", return_value=True
|
||||
) as mock_setup, patch(
|
||||
"homeassistant.components.sense.async_setup_entry",
|
||||
return_value=True,
|
||||
) as mock_setup_entry:
|
||||
@ -35,7 +33,6 @@ async def test_form(hass):
|
||||
"email": "test-email",
|
||||
"password": "test-password",
|
||||
}
|
||||
assert len(mock_setup.mock_calls) == 1
|
||||
assert len(mock_setup_entry.mock_calls) == 1
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user