Ejemplo n.º 1
0
def homeData(auth, requests_mock):
    with open("fixtures/home_data_simple.json") as f:
        json_fixture = json.load(f)
    requests_mock.post(
        pyatmo.thermostat._GETHOMESDATA_REQ,
        json=json_fixture,
        headers={"content-type": "application/json"},
    )
    return pyatmo.HomeData(auth)
def test_HomeData_no_body(auth, requests_mock):
    with open("fixtures/home_data_empty.json") as f:
        json_fixture = json.load(f)
    requests_mock.post(
        pyatmo.thermostat._GETHOMESDATA_REQ,
        json=json_fixture,
        headers={"content-type": "application/json"},
    )
    with pytest.raises(pyatmo.NoDevice):
        assert pyatmo.HomeData(auth)
Ejemplo n.º 3
0
 def setup(self):
     """Retrieve HomeData by NetAtmo API."""
     import pyatmo
     try:
         self.homedata = pyatmo.HomeData(self.auth)
         self.home_id = self.homedata.gethomeId(self.home)
     except TypeError:
         _LOGGER.error("Error when getting home data.")
     except pyatmo.NoDevice:
         _LOGGER.debug("No thermostat devices available.")
def test_HomeData_no_home_name(auth, requests_mock):
    with open("fixtures/home_data_nohomename.json") as f:
        json_fixture = json.load(f)
    requests_mock.post(
        pyatmo.thermostat._GETHOMESDATA_REQ,
        json=json_fixture,
        headers={"content-type": "application/json"},
    )
    homeData = pyatmo.HomeData(auth)
    home_id = "91763b24c43d3e344f424e8b"
    assert homeData.homeById(home_id)["name"] == "Unknown"
Ejemplo n.º 5
0
 def setup(self):
     """Retrieve HomeData and HomeStatus by NetAtmo API."""
     import pyatmo
     try:
         self.homedata = pyatmo.HomeData(self.auth)
         self.homestatus = pyatmo.HomeStatus(self.auth, home=self.home)
         self.home_id = self.homedata.gethomeId(self.home)
         self.update()
     except TypeError:
         _LOGGER.error("ThermostatData::setup() got error.")
         return False
     return True
Ejemplo n.º 6
0
 def setup(self):
     """Retrieve HomeData by NetAtmo API."""
     try:
         self.homedata = pyatmo.HomeData(self.auth)
         self.home_id = self.homedata.gethomeId(self.home)
     except TypeError:
         _LOGGER.error("Error when getting home data")
     except AttributeError:
         _LOGGER.error("No default_home in HomeData")
     except pyatmo.NoDevice:
         _LOGGER.debug("No thermostat devices available")
     except pyatmo.InvalidHome:
         _LOGGER.debug("Invalid home %s", self.home)
Ejemplo n.º 7
0
 def setup(self):
     """Retrieve HomeData and HomeStatus by NetAtmo API."""
     try:
         self.homedata = pyatmo.HomeData(self.auth)
         self.homestatus = pyatmo.HomeStatus(self.auth,
                                             home_id=self.home_id)
         self.home_name = self.homedata.getHomeName(self.home_id)
         self.update()
     except TypeError:
         _LOGGER.error("ThermostatData::setup() got error")
         return False
     except pyatmo.exceptions.NoDevice:
         _LOGGER.debug("No climate devices for %s (%s)", self.home_name,
                       self.home_id)
         return False
     return True
def test_HomeData_no_data(auth, requests_mock):
    requests_mock.post(pyatmo.thermostat._GETHOMESDATA_REQ, text="None")
    with pytest.raises(pyatmo.NoDevice):
        assert pyatmo.HomeData(auth)
Ejemplo n.º 9
0
def setup(hass, config):
    """Set up the Netatmo devices."""

    hass.data[DATA_PERSONS] = {}
    try:
        auth = pyatmo.ClientAuth(
            config[DOMAIN][CONF_API_KEY],
            config[DOMAIN][CONF_SECRET_KEY],
            config[DOMAIN][CONF_USERNAME],
            config[DOMAIN][CONF_PASSWORD],
            "read_station read_camera access_camera "
            "read_thermostat write_thermostat "
            "read_presence access_presence read_homecoach",
        )
    except HTTPError:
        _LOGGER.error("Unable to connect to Netatmo API")
        return False

    try:
        home_data = pyatmo.HomeData(auth)
    except pyatmo.NoDevice:
        home_data = None
        _LOGGER.debug("No climate device. Disable %s service",
                      SERVICE_SETSCHEDULE)

    # Store config to be used during entry setup
    hass.data[DATA_NETATMO_AUTH] = auth

    if config[DOMAIN][CONF_DISCOVERY]:
        for component in "camera", "sensor", "binary_sensor", "climate":
            discovery.load_platform(hass, component, DOMAIN, {}, config)

    if config[DOMAIN][CONF_WEBHOOKS]:
        webhook_id = hass.components.webhook.async_generate_id()
        hass.data[
            DATA_WEBHOOK_URL] = hass.components.webhook.async_generate_url(
                webhook_id)
        hass.components.webhook.async_register(DOMAIN, "Netatmo", webhook_id,
                                               handle_webhook)
        auth.addwebhook(hass.data[DATA_WEBHOOK_URL])
        hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, dropwebhook)

    def _service_addwebhook(service):
        """Service to (re)add webhooks during runtime."""
        url = service.data.get(CONF_URL)
        if url is None:
            url = hass.data[DATA_WEBHOOK_URL]
        _LOGGER.info("Adding webhook for URL: %s", url)
        auth.addwebhook(url)

    hass.services.register(
        DOMAIN,
        SERVICE_ADDWEBHOOK,
        _service_addwebhook,
        schema=SCHEMA_SERVICE_ADDWEBHOOK,
    )

    def _service_dropwebhook(service):
        """Service to drop webhooks during runtime."""
        _LOGGER.info("Dropping webhook")
        auth.dropwebhook()

    hass.services.register(
        DOMAIN,
        SERVICE_DROPWEBHOOK,
        _service_dropwebhook,
        schema=SCHEMA_SERVICE_DROPWEBHOOK,
    )

    def _service_setschedule(service):
        """Service to change current home schedule."""
        schedule_name = service.data.get(ATTR_SCHEDULE)
        home_data.switchHomeSchedule(schedule=schedule_name)
        _LOGGER.info("Set home schedule to %s", schedule_name)

    if home_data is not None:
        hass.services.register(
            DOMAIN,
            SERVICE_SETSCHEDULE,
            _service_setschedule,
            schema=SCHEMA_SERVICE_SETSCHEDULE,
        )

    return True