mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 22:27:07 +00:00
parent
09a85d2a5d
commit
09944d936d
@ -4,14 +4,14 @@ from __future__ import annotations
|
|||||||
from pyefergy import Efergy, exceptions
|
from pyefergy import Efergy, exceptions
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import ATTR_ATTRIBUTION, CONF_API_KEY, Platform
|
from homeassistant.const import CONF_API_KEY, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.entity import DeviceInfo, Entity
|
from homeassistant.helpers.entity import DeviceInfo, Entity
|
||||||
|
|
||||||
from .const import ATTRIBUTION, DATA_KEY_API, DEFAULT_NAME, DOMAIN
|
from .const import DEFAULT_NAME, DOMAIN
|
||||||
|
|
||||||
PLATFORMS = [Platform.SENSOR]
|
PLATFORMS = [Platform.SENSOR]
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
"API Key is no longer valid. Please reauthenticate"
|
"API Key is no longer valid. Please reauthenticate"
|
||||||
) from ex
|
) from ex
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {DATA_KEY_API: api}
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = api
|
||||||
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
@ -42,8 +42,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||||
if unload_ok:
|
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
hass.data[DOMAIN].pop(entry.entry_id)
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
|
||||||
@ -51,21 +50,17 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
class EfergyEntity(Entity):
|
class EfergyEntity(Entity):
|
||||||
"""Representation of a Efergy entity."""
|
"""Representation of a Efergy entity."""
|
||||||
|
|
||||||
def __init__(
|
_attr_attribution = "Data provided by Efergy"
|
||||||
self,
|
|
||||||
api: Efergy,
|
def __init__(self, api: Efergy, server_unique_id: str) -> None:
|
||||||
server_unique_id: str,
|
|
||||||
) -> None:
|
|
||||||
"""Initialize an Efergy entity."""
|
"""Initialize an Efergy entity."""
|
||||||
self.api = api
|
self.api = api
|
||||||
self._server_unique_id = server_unique_id
|
|
||||||
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
configuration_url="https://engage.efergy.com/user/login",
|
configuration_url="https://engage.efergy.com/user/login",
|
||||||
connections={(dr.CONNECTION_NETWORK_MAC, self.api.info["mac"])},
|
connections={(dr.CONNECTION_NETWORK_MAC, api.info["mac"])},
|
||||||
identifiers={(DOMAIN, self._server_unique_id)},
|
identifiers={(DOMAIN, server_unique_id)},
|
||||||
manufacturer=DEFAULT_NAME,
|
manufacturer=DEFAULT_NAME,
|
||||||
name=DEFAULT_NAME,
|
name=DEFAULT_NAME,
|
||||||
model=self.api.info["type"],
|
model=api.info["type"],
|
||||||
sw_version=self.api.info["version"],
|
sw_version=api.info["version"],
|
||||||
)
|
)
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
"""Config flow for Efergy integration."""
|
"""Config flow for Efergy integration."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from pyefergy import Efergy, exceptions
|
from pyefergy import Efergy, exceptions
|
||||||
@ -12,9 +11,7 @@ from homeassistant.const import CONF_API_KEY
|
|||||||
from homeassistant.data_entry_flow import FlowResult
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
from .const import DEFAULT_NAME, DOMAIN
|
from .const import DEFAULT_NAME, DOMAIN, LOGGER
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
class EfergyFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
class EfergyFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
@ -69,6 +66,6 @@ class EfergyFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
except exceptions.InvalidAuth:
|
except exceptions.InvalidAuth:
|
||||||
return None, "invalid_auth"
|
return None, "invalid_auth"
|
||||||
except Exception: # pylint: disable=broad-except
|
except Exception: # pylint: disable=broad-except
|
||||||
_LOGGER.exception("Unexpected exception")
|
LOGGER.exception("Unexpected exception")
|
||||||
return None, "unknown"
|
return None, "unknown"
|
||||||
return api.info["hid"], None
|
return api.info["hid"], None
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
"""Constants for the Efergy integration."""
|
"""Constants for the Efergy integration."""
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
import logging
|
||||||
ATTRIBUTION = "Data provided by Efergy"
|
from typing import Final
|
||||||
|
|
||||||
CONF_CURRENT_VALUES = "current_values"
|
CONF_CURRENT_VALUES = "current_values"
|
||||||
|
|
||||||
DATA_KEY_API = "api"
|
|
||||||
DEFAULT_NAME = "Efergy"
|
DEFAULT_NAME = "Efergy"
|
||||||
DOMAIN = "efergy"
|
DOMAIN: Final = "efergy"
|
||||||
|
|
||||||
|
LOGGER = logging.getLogger(__package__)
|
||||||
|
|
||||||
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
"""Support for Efergy sensors."""
|
"""Support for Efergy sensors."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
|
||||||
from re import sub
|
from re import sub
|
||||||
from typing import cast
|
from typing import cast
|
||||||
|
|
||||||
@ -21,9 +20,7 @@ from homeassistant.helpers import entity_platform
|
|||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
|
|
||||||
from . import EfergyEntity
|
from . import EfergyEntity
|
||||||
from .const import CONF_CURRENT_VALUES, DATA_KEY_API, DOMAIN
|
from .const import CONF_CURRENT_VALUES, DOMAIN, LOGGER
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
@ -112,7 +109,7 @@ async def async_setup_entry(
|
|||||||
async_add_entities: entity_platform.AddEntitiesCallback,
|
async_add_entities: entity_platform.AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Efergy sensors."""
|
"""Set up Efergy sensors."""
|
||||||
api: Efergy = hass.data[DOMAIN][entry.entry_id][DATA_KEY_API]
|
api: Efergy = hass.data[DOMAIN][entry.entry_id]
|
||||||
sensors = []
|
sensors = []
|
||||||
for description in SENSOR_TYPES:
|
for description in SENSOR_TYPES:
|
||||||
if description.key != CONF_CURRENT_VALUES:
|
if description.key != CONF_CURRENT_VALUES:
|
||||||
@ -174,8 +171,8 @@ class EfergySensor(EfergyEntity, SensorEntity):
|
|||||||
except (ConnectError, DataError, ServiceError) as ex:
|
except (ConnectError, DataError, ServiceError) as ex:
|
||||||
if self._attr_available:
|
if self._attr_available:
|
||||||
self._attr_available = False
|
self._attr_available = False
|
||||||
_LOGGER.error("Error getting data: %s", ex)
|
LOGGER.error("Error getting data: %s", ex)
|
||||||
return
|
return
|
||||||
if not self._attr_available:
|
if not self._attr_available:
|
||||||
self._attr_available = True
|
self._attr_available = True
|
||||||
_LOGGER.info("Connection has resumed")
|
LOGGER.info("Connection has resumed")
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
"config": {
|
"config": {
|
||||||
"step": {
|
"step": {
|
||||||
"user": {
|
"user": {
|
||||||
"title": "Efergy",
|
|
||||||
"data": {
|
"data": {
|
||||||
"api_key": "[%key:common::config_flow::data::api_key%]"
|
"api_key": "[%key:common::config_flow::data::api_key%]"
|
||||||
}
|
}
|
||||||
|
@ -13,8 +13,7 @@
|
|||||||
"user": {
|
"user": {
|
||||||
"data": {
|
"data": {
|
||||||
"api_key": "API Key"
|
"api_key": "API Key"
|
||||||
},
|
}
|
||||||
"title": "Efergy"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user