Switch Netatmo integration to dispatcher for internal communication (#38590)

* Switch to dispatcher for internal communication

* Fix method call

* Update homeassistant/components/netatmo/camera.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/netatmo/camera.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/netatmo/climate.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/netatmo/climate.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Rename variables

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
cgtobiandMartin Hjelmare authored and GitHub committed 2020-08-07 09:25:59 +02:00
1 parent 3546a82cfb
commit 6930aebea2
7 files changed
+170 -165

No files matched your search

+28 -46
View File
@@ -5,14 +5,10 @@ import pyatmo
import requests
import voluptuous as vol
from homeassistant.components.camera import (
DOMAIN as CAMERA_DOMAIN,
SUPPORT_STREAM,
Camera,
)
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.components.camera import SUPPORT_STREAM, Camera
from homeassistant.core import callback
from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from .const import (
ATTR_PERSON,
@@ -21,10 +17,12 @@ from .const import (
DATA_HANDLER,
DATA_PERSONS,
DOMAIN,
EVENT_TYPE_OFF,
EVENT_TYPE_ON,
MANUFACTURER,
MODELS,
SERVICE_SETPERSONAWAY,
SERVICE_SETPERSONSHOME,
SERVICE_SET_PERSON_AWAY,
SERVICE_SET_PERSONS_HOME,
SIGNAL_NAME,
)
from .data_handler import CAMERA_DATA_CLASS_NAME
@@ -34,20 +32,6 @@ _LOGGER = logging.getLogger(__name__)
DEFAULT_QUALITY = "high"
SCHEMA_SERVICE_SETPERSONSHOME = vol.Schema(
{
vol.Required(ATTR_ENTITY_ID): cv.entity_domain(CAMERA_DOMAIN),
vol.Required(ATTR_PERSONS): vol.All(cv.ensure_list, [cv.string]),
}
)
SCHEMA_SERVICE_SETPERSONAWAY = vol.Schema(
{
vol.Required(ATTR_ENTITY_ID): cv.entity_domain(CAMERA_DOMAIN),
vol.Optional(ATTR_PERSON): cv.string,
}
)
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up the Netatmo camera platform."""
@@ -108,22 +92,17 @@ async def async_setup_entry(hass, entry, async_add_entities):
if data_handler.data[CAMERA_DATA_CLASS_NAME] is not None:
platform.async_register_entity_service(
SERVICE_SETPERSONSHOME,
SCHEMA_SERVICE_SETPERSONSHOME,
"_service_setpersonshome",
SERVICE_SET_PERSONS_HOME,
{vol.Required(ATTR_PERSONS): vol.All(cv.ensure_list, [cv.string])},
"_service_set_persons_home",
)
platform.async_register_entity_service(
SERVICE_SETPERSONAWAY,
SCHEMA_SERVICE_SETPERSONAWAY,
"_service_setpersonaway",
SERVICE_SET_PERSON_AWAY,
{vol.Optional(ATTR_PERSON): cv.string},
"_service_set_person_away",
)
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the Netatmo camera platform."""
return
class NetatmoCamera(NetatmoBase, Camera):
"""Representation of a Netatmo camera."""
@@ -156,16 +135,19 @@ class NetatmoCamera(NetatmoBase, Camera):
"""Entity created."""
await super().async_added_to_hass()
self._listeners.append(
self.hass.bus.async_listen("netatmo_event", self.handle_event)
)
for event_type in (EVENT_TYPE_OFF, EVENT_TYPE_ON):
self._listeners.append(
async_dispatcher_connect(
self.hass,
f"signal-{DOMAIN}-webhook-{event_type}",
self.handle_event,
)
)
async def handle_event(self, event):
@callback
def handle_event(self, event):
"""Handle webhook events."""
data = event.data["data"]
if not data.get("event_type"):
return
data = event["data"]
if not data.get("camera_id"):
return
@@ -278,7 +260,7 @@ class NetatmoCamera(NetatmoBase, Camera):
self._is_local = camera.get("is_local")
self.is_streaming = bool(self._status == "on")
def _service_setpersonshome(self, **kwargs):
def _service_set_persons_home(self, **kwargs):
"""Service to change current home schedule."""
persons = kwargs.get(ATTR_PERSONS)
person_ids = []
@@ -288,9 +270,9 @@ class NetatmoCamera(NetatmoBase, Camera):
person_ids.append(pid)
self._data.set_persons_home(person_ids=person_ids, home_id=self._home_id)
_LOGGER.info("Set %s as at home", persons)
_LOGGER.debug("Set %s as at home", persons)
def _service_setpersonaway(self, **kwargs):
def _service_set_person_away(self, **kwargs):
"""Service to mark a person as away or set the home as empty."""
person = kwargs.get(ATTR_PERSON)
person_id = None
@@ -303,10 +285,10 @@ class NetatmoCamera(NetatmoBase, Camera):
self._data.set_persons_away(
person_id=person_id, home_id=self._home_id,
)
_LOGGER.info("Set %s as away", person)
_LOGGER.debug("Set %s as away", person)
else:
self._data.set_persons_away(
person_id=person_id, home_id=self._home_id,
)
_LOGGER.info("Set home as empty")
_LOGGER.debug("Set home as empty")