hass-core/homeassistant/components/plugwise/__init__.py
Tom 7e693afcf3
Update plugwise to async and config_flow (#33691)
* Update plugwise async, config_flow and multi entity

* Update battery percentage

* Fix yamllint on services

* Fix yamllint on services

* Fix formatting for pyupgrade

* Update homeassistant/components/plugwise/__init__.py

Co-Authored-By: Robert Svensson <Kane610@users.noreply.github.com>

* Add try/except on setup

* Bump module version, battery version and valve position

* Removing sensor, switch, water_heater for later (child) PRs

* Catchup and version bump

* Remove title from strings.json

* Readd already reviewd await try/except

* Readd already reviewed config_flow

* Fix pylint

* Fix per 0.109 translations

* Remove unused import from merge

* Update plugwise async, config_flow and multi entity

* Update battery percentage

* Fix yamllint on services

* Fix yamllint on services

* Bump module version

* Bump module version, battery version and valve position

* Removing sensor, switch, water_heater for later (child) PRs

* Catchup and version bump

* Remove title from strings.json

* Fix pylint

* Fix per 0.109 translations

* Translations and config_flow, module version bump with required changes

* Translations and config_flow, module version bump with required changes

* Fix requirements

* Fix requirements

* Fix pylint

* Fix pylint

* Update homeassistant/components/plugwise/__init__.py

Improvement

Co-authored-by: J. Nick Koston <nick@koston.org>

* Update homeassistant/components/plugwise/__init__.py

Improvement

Co-authored-by: J. Nick Koston <nick@koston.org>

* Update homeassistant/components/plugwise/__init__.py

Improvement

Co-authored-by: J. Nick Koston <nick@koston.org>

* Include configentrynotready on import

* Update __init__.py

* DataUpdateCoordinator and comment non-PR-platforms

* Fix reqs

* Rename devices variable in favor of entities

* Rework updates with DataUpdateCoordinator

* Peer review

* Peer review second part

* Cleanup comments and redundant code

* Added required config_flow test

* Peer review third part

* Update service was replaced by DataUpdateCoordinator

* Corrected testing, version bump for InvalidAuth, move uniq_id

* Remove according to review

* Await connect (py38)

* Remove unneccesary code

* Show only when multiple

* Improve config_flow, rename consts

* Update homeassistant/components/plugwise/climate.py

Co-authored-by: J. Nick Koston <nick@koston.org>

* Update homeassistant/components/plugwise/climate.py

Co-authored-by: J. Nick Koston <nick@koston.org>

* Process review comments

Co-authored-by: Robert Svensson <Kane610@users.noreply.github.com>
Co-authored-by: J. Nick Koston <nick@koston.org>
2020-05-28 10:52:25 -05:00

161 lines
4.5 KiB
Python

"""Plugwise platform for Home Assistant Core."""
import asyncio
from datetime import timedelta
import logging
from Plugwise_Smile.Smile import Smile
import async_timeout
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import DOMAIN
CONFIG_SCHEMA = vol.Schema({DOMAIN: vol.Schema({})}, extra=vol.ALLOW_EXTRA)
_LOGGER = logging.getLogger(__name__)
ALL_PLATFORMS = ["climate"]
async def async_setup(hass: HomeAssistant, config: dict):
"""Set up the Plugwise platform."""
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Plugwise Smiles from a config entry."""
websession = async_get_clientsession(hass, verify_ssl=False)
api = Smile(
host=entry.data["host"], password=entry.data["password"], websession=websession
)
try:
connected = await api.connect()
if not connected:
_LOGGER.error("Unable to connect to Smile")
raise ConfigEntryNotReady
except Smile.InvalidAuthentication:
_LOGGER.error("Invalid Smile ID")
return False
except Smile.PlugwiseError:
_LOGGER.error("Error while communicating to device")
raise ConfigEntryNotReady
except asyncio.TimeoutError:
_LOGGER.error("Timeout while connecting to Smile")
raise ConfigEntryNotReady
if api.smile_type == "power":
update_interval = timedelta(seconds=10)
else:
update_interval = timedelta(seconds=60)
async def async_update_data():
"""Update data via API endpoint."""
try:
async with async_timeout.timeout(10):
await api.full_update_device()
return True
except Smile.XMLDataMissingError:
raise UpdateFailed("Smile update failed")
coordinator = DataUpdateCoordinator(
hass,
_LOGGER,
name="Smile",
update_method=async_update_data,
update_interval=update_interval,
)
await coordinator.async_refresh()
if not coordinator.last_update_success:
raise ConfigEntryNotReady
api.get_all_devices()
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
"api": api,
"coordinator": coordinator,
}
device_registry = await dr.async_get_registry(hass)
device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(DOMAIN, api.gateway_id)},
manufacturer="Plugwise",
name=entry.title,
model=f"Smile {api.smile_name}",
sw_version=api.smile_version[0],
)
for component in ALL_PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, component)
)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload a config entry."""
unload_ok = all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(entry, component)
for component in ALL_PLATFORMS
]
)
)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok
class SmileGateway(Entity):
"""Represent Smile Gateway."""
def __init__(self, api, coordinator):
"""Initialise the sensor."""
self._api = api
self._coordinator = coordinator
self._unique_id = None
@property
def unique_id(self):
"""Return a unique ID."""
return self._unique_id
@property
def should_poll(self):
"""Return False, updates are controlled via coordinator."""
return False
@property
def available(self):
"""Return True if entity is available."""
return self._coordinator.last_update_success
async def async_added_to_hass(self):
"""Subscribe to updates."""
self.async_on_remove(self._coordinator.async_add_listener(self._process_data))
def _process_data(self):
"""Interpret and process API data."""
raise NotImplementedError
async def async_update(self):
"""Update the entity."""
await self._coordinator.async_request_refresh()