Clean up Netatmo integration (#50904)
This commit is contained in:
parent
19d25cd901
commit
0623648309
6 changed files with 50 additions and 17 deletions
|
@ -44,6 +44,10 @@ from .const import (
|
|||
DOMAIN,
|
||||
OAUTH2_AUTHORIZE,
|
||||
OAUTH2_TOKEN,
|
||||
PLATFORMS,
|
||||
WEBHOOK_ACTIVATION,
|
||||
WEBHOOK_DEACTIVATION,
|
||||
WEBHOOK_PUSH_TYPE,
|
||||
)
|
||||
from .data_handler import NetatmoDataHandler
|
||||
from .webhook import async_handle_webhook
|
||||
|
@ -62,8 +66,6 @@ CONFIG_SCHEMA = vol.Schema(
|
|||
extra=vol.ALLOW_EXTRA,
|
||||
)
|
||||
|
||||
PLATFORMS = ["camera", "climate", "light", "sensor"]
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: dict):
|
||||
"""Set up the Netatmo component."""
|
||||
|
@ -126,7 +128,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||
async_dispatcher_send(
|
||||
hass,
|
||||
f"signal-{DOMAIN}-webhook-None",
|
||||
{"type": "None", "data": {"push_type": "webhook_deactivation"}},
|
||||
{"type": "None", "data": {WEBHOOK_PUSH_TYPE: WEBHOOK_DEACTIVATION}},
|
||||
)
|
||||
webhook_unregister(hass, entry.data[CONF_WEBHOOK_ID])
|
||||
await hass.data[DOMAIN][entry.entry_id][AUTH].async_dropwebhook()
|
||||
|
@ -150,9 +152,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||
entry.data[CONF_WEBHOOK_ID]
|
||||
)
|
||||
|
||||
if entry.data["auth_implementation"] == "cloud" and not webhook_url.startswith(
|
||||
"https://"
|
||||
):
|
||||
if entry.data[
|
||||
"auth_implementation"
|
||||
] == cloud.DOMAIN and not webhook_url.startswith("https://"):
|
||||
_LOGGER.warning(
|
||||
"Webhook not registered - "
|
||||
"https and port 443 is required to register the webhook"
|
||||
|
@ -170,7 +172,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
|
|||
|
||||
async def handle_event(event):
|
||||
"""Handle webhook events."""
|
||||
if event["data"]["push_type"] == "webhook_activation":
|
||||
if event["data"][WEBHOOK_PUSH_TYPE] == WEBHOOK_ACTIVATION:
|
||||
if activation_listener is not None:
|
||||
activation_listener()
|
||||
|
||||
|
|
|
@ -31,6 +31,9 @@ from .const import (
|
|||
SERVICE_SET_PERSON_AWAY,
|
||||
SERVICE_SET_PERSONS_HOME,
|
||||
SIGNAL_NAME,
|
||||
WEBHOOK_LIGHT_MODE,
|
||||
WEBHOOK_NACAMERA_CONNECTION,
|
||||
WEBHOOK_PUSH_TYPE,
|
||||
)
|
||||
from .data_handler import CAMERA_DATA_CLASS_NAME
|
||||
from .netatmo_entity_base import NetatmoBase
|
||||
|
@ -158,13 +161,16 @@ class NetatmoCamera(NetatmoBase, Camera):
|
|||
return
|
||||
|
||||
if data["home_id"] == self._home_id and data["camera_id"] == self._id:
|
||||
if data["push_type"] in ["NACamera-off", "NACamera-disconnection"]:
|
||||
if data[WEBHOOK_PUSH_TYPE] in ["NACamera-off", "NACamera-disconnection"]:
|
||||
self.is_streaming = False
|
||||
self._status = "off"
|
||||
elif data["push_type"] in ["NACamera-on", "NACamera-connection"]:
|
||||
elif data[WEBHOOK_PUSH_TYPE] in [
|
||||
"NACamera-on",
|
||||
WEBHOOK_NACAMERA_CONNECTION,
|
||||
]:
|
||||
self.is_streaming = True
|
||||
self._status = "on"
|
||||
elif data["push_type"] == "NOC-light_mode":
|
||||
elif data[WEBHOOK_PUSH_TYPE] == WEBHOOK_LIGHT_MODE:
|
||||
self._light_state = data["sub_type"]
|
||||
|
||||
self.async_write_ha_state()
|
||||
|
@ -176,8 +182,10 @@ class NetatmoCamera(NetatmoBase, Camera):
|
|||
return await self._data.async_get_live_snapshot(camera_id=self._id)
|
||||
except (
|
||||
aiohttp.ClientPayloadError,
|
||||
pyatmo.exceptions.ApiError,
|
||||
aiohttp.ContentTypeError,
|
||||
aiohttp.ServerDisconnectedError,
|
||||
aiohttp.ClientConnectorError,
|
||||
pyatmo.exceptions.ApiError,
|
||||
) as err:
|
||||
_LOGGER.debug("Could not fetch live camera image (%s)", err)
|
||||
return None
|
||||
|
|
|
@ -1,9 +1,16 @@
|
|||
"""Constants used by the Netatmo component."""
|
||||
from homeassistant.components.camera import DOMAIN as CAMERA_DOMAIN
|
||||
from homeassistant.components.climate import DOMAIN as CLIMATE_DOMAIN
|
||||
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
|
||||
API = "api"
|
||||
|
||||
DOMAIN = "netatmo"
|
||||
MANUFACTURER = "Netatmo"
|
||||
|
||||
PLATFORMS = [CAMERA_DOMAIN, CLIMATE_DOMAIN, LIGHT_DOMAIN, SENSOR_DOMAIN]
|
||||
|
||||
MODEL_NAPLUG = "Relay"
|
||||
MODEL_NATHERM1 = "Smart Thermostat"
|
||||
MODEL_NRV = "Smart Radiator Valves"
|
||||
|
@ -156,3 +163,9 @@ MODE_LIGHT_ON = "on"
|
|||
MODE_LIGHT_OFF = "off"
|
||||
MODE_LIGHT_AUTO = "auto"
|
||||
CAMERA_LIGHT_MODES = [MODE_LIGHT_ON, MODE_LIGHT_OFF, MODE_LIGHT_AUTO]
|
||||
|
||||
WEBHOOK_ACTIVATION = "webhook_activation"
|
||||
WEBHOOK_DEACTIVATION = "webhook_deactivation"
|
||||
WEBHOOK_NACAMERA_CONNECTION = "NACamera-connection"
|
||||
WEBHOOK_PUSH_TYPE = "push_type"
|
||||
WEBHOOK_LIGHT_MODE = "NOC-light_mode"
|
||||
|
|
|
@ -15,7 +15,15 @@ from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
|
|||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.event import async_track_time_interval
|
||||
|
||||
from .const import AUTH, DOMAIN, MANUFACTURER
|
||||
from .const import (
|
||||
AUTH,
|
||||
DOMAIN,
|
||||
MANUFACTURER,
|
||||
WEBHOOK_ACTIVATION,
|
||||
WEBHOOK_DEACTIVATION,
|
||||
WEBHOOK_NACAMERA_CONNECTION,
|
||||
WEBHOOK_PUSH_TYPE,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -108,15 +116,15 @@ class NetatmoDataHandler:
|
|||
|
||||
async def handle_event(self, event):
|
||||
"""Handle webhook events."""
|
||||
if event["data"]["push_type"] == "webhook_activation":
|
||||
if event["data"][WEBHOOK_PUSH_TYPE] == WEBHOOK_ACTIVATION:
|
||||
_LOGGER.info("%s webhook successfully registered", MANUFACTURER)
|
||||
self._webhook = True
|
||||
|
||||
elif event["data"]["push_type"] == "webhook_deactivation":
|
||||
elif event["data"][WEBHOOK_PUSH_TYPE] == WEBHOOK_DEACTIVATION:
|
||||
_LOGGER.info("%s webhook unregistered", MANUFACTURER)
|
||||
self._webhook = False
|
||||
|
||||
elif event["data"]["push_type"] == "NACamera-connection":
|
||||
elif event["data"][WEBHOOK_PUSH_TYPE] == WEBHOOK_NACAMERA_CONNECTION:
|
||||
_LOGGER.debug("%s camera reconnected", MANUFACTURER)
|
||||
self.async_force_update(CAMERA_DATA_CLASS_NAME)
|
||||
|
||||
|
|
|
@ -12,6 +12,8 @@ from .const import (
|
|||
EVENT_TYPE_LIGHT_MODE,
|
||||
MANUFACTURER,
|
||||
SIGNAL_NAME,
|
||||
WEBHOOK_LIGHT_MODE,
|
||||
WEBHOOK_PUSH_TYPE,
|
||||
)
|
||||
from .data_handler import CAMERA_DATA_CLASS_NAME, NetatmoDataHandler
|
||||
from .netatmo_entity_base import NetatmoBase
|
||||
|
@ -105,7 +107,7 @@ class NetatmoLight(NetatmoBase, LightEntity):
|
|||
if (
|
||||
data["home_id"] == self._home_id
|
||||
and data["camera_id"] == self._id
|
||||
and data["push_type"] == "NOC-light_mode"
|
||||
and data[WEBHOOK_PUSH_TYPE] == WEBHOOK_LIGHT_MODE
|
||||
):
|
||||
self._is_on = bool(data["sub_type"] == "on")
|
||||
|
||||
|
|
|
@ -159,7 +159,7 @@ def async_parse_identifier(
|
|||
item: MediaSourceItem,
|
||||
) -> tuple[str, str, int | None]:
|
||||
"""Parse identifier."""
|
||||
if "/" not in item.identifier:
|
||||
if not item.identifier or "/" not in item.identifier:
|
||||
return "events", "", None
|
||||
|
||||
source, path = item.identifier.lstrip("/").split("/", 1)
|
||||
|
|
Loading…
Add table
Reference in a new issue