Skip to content

Documentation

SolarPlusIntelbras

A class to represent a SolarPlusIntelbras.

Source code in solar_plus_intelbras/solar_plus_intelbras.py
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
class SolarPlusIntelbras:
    """A class to represent a SolarPlusIntelbras."""

    __BASE_API_URL = "https://ens-server.intelbras.com.br/api/"

    def __init__(
        self,
        email: EmailStr,
        plus: str,
    ) -> None:
        """Construct a SolarPlusIntelbras object.

        Args:
            email (EmailStr): A valid email address.
            plus (str): A string.

        Returns:
            None: The constructor does not return anything.
        """
        self.__email = email
        self.__plus = plus
        self.__token = None
        self._token_expiration = None

    def __str__(self) -> str:
        """Return a string representation of the object.

        Returns:
            str: A string representation of the object.
        """
        return f"<SolarPlusIntelbras {self.email}>"

    def __repr__(self) -> str:
        """Return a string representation of the object.

        Returns:
            str: A string representation of the object.
        """
        return self.__str__()

    @property
    def email(self) -> EmailStr:
        """Return the email attribute.

        Returns:
            EmailStr: A valid email address.
        """
        return self.__email

    @property
    def plus(self) -> str:
        """Return the plus attribute.

        Returns:
            str: A string.
        """
        return self.__plus

    @property
    def base_api_url(self) -> str:
        """Return the base_api_url attribute.

        Returns:
            str: The base API URL.
        """
        return self.__BASE_API_URL

    @property
    def token(self) -> str:
        """Returns the token. If the token is missing or expired, requests a new one.

        Returns:
            str: The token.
        """
        if not self.__token or self._is_token_expired():
            self._login()
        return self.__token

    def _is_token_expired(self) -> bool:
        """Checks if the token has expired by comparing the current time with the expiration time.

        Returns:
            bool: True if the token has expired, False otherwise.
        """
        if not self._token_expiration:
            return True
        return datetime.now(timezone.utc) >= self._token_expiration

    def _login(self) -> dict:
        """Faz a requisição de login, armazena o token e tempo de expiração.

        Returns:
            dict: A dictionary with the login response.
        """
        response = requests.post(
            f"{self.base_api_url}{EndpointEnum.LOGIN.value}",
            json={"email": self.email},
            headers={"plus": self.plus},
        )
        data = response.json()

        access_data = data["accessToken"]
        self.__token = access_data["accessJWT"]

        if "exp" in access_data:
            expires_ts = access_data["exp"]
            self._token_expiration = datetime.utcfromtimestamp(expires_ts).replace(
                tzinfo=timezone.utc
            )
        else:
            self._token_expiration = datetime.now(timezone.utc)

        return data

    def plants(self) -> dict:
        """Return the plants.

        Returns:
            dict: A dictionary with the plants.
        """
        response = requests.get(
            f"{self.base_api_url}{EndpointEnum.PLANTS.value}",
            headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
        )
        return response.json()

    def plants_detail(self, plant_id: int) -> dict:
        """Return the plant.

        Args:
            plant_id (int): A plant id.

        Returns:
            dict: A dictionary with the plants.
        """
        response = requests.get(
            f"{self.base_api_url}{EndpointEnum.PLANTS.value}/{plant_id}",
            headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
        )
        return response.json()

    def records(
        self,
        plant_id: int,
        period: PeriodEnum,
        key: KeyEnum,
        start_date: str,
        end_date: str,
    ) -> dict:
        """Return the records.

        Args:
            plant_id (int): A plant id.
            period (PeriodEnum): A period.
            key (KeyEnum): A key.
            start_date (str): A start date.
            end_date (str): An end date.

        Returns:
            dict: A dictionary with the records.
        """
        params = {
            "period": period,
            "key": key,
        }

        try:
            datetime.strptime(start_date, "%Y-%m-%d")
            params["start_date"] = start_date
        except ValueError:
            raise ValueError("start_date must be in the format YYYY-MM-DD.")

        try:
            datetime.strptime(end_date, "%Y-%m-%d")
            params["end_date"] = end_date
        except ValueError:
            raise ValueError("end_date must be in the format YYYY-MM-DD.")

        response = requests.get(
            f"{self.base_api_url}{EndpointEnum.PLANTS.value}/{plant_id}/{EndpointEnum.RECORDS.value}",
            headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
            params=params,
        )
        return response.json()

    def records_year(
        self,
        year: int,
        plant_id: int,
    ) -> dict:
        """Return the records of a year.

        Args:
            year (int): A year.
            plant_id (int): A plant id.

        Returns:
            dict: A dictionary with the records.
        """
        params = {
            "key": KeyEnum.ENERGY_TODAY.value,
            "year": year,
            "period": PeriodEnum.YEAR.value,
        }

        response = requests.get(
            f"{self.base_api_url}{EndpointEnum.PLANTS.value}/{plant_id}/{EndpointEnum.RECORDS_YEAR.value}",
            headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
            params=params,
        )
        return response.json()

    def records_years(
        self,
        start_year: int,
        end_year: int,
        plant_id: int,
    ) -> dict:
        """Return the records of a year.

        Args:
            start_year (int): A year.
            end_year (int): A year.
            plant_id (int): A plant id.

        Returns:
            dict: A dictionary with the records.
        """

        params = {
            "start_year": start_year,
            "end_year": end_year,
            "key": KeyEnum.ENERGY_TODAY.value,
        }

        response = requests.get(
            f"{self.base_api_url}{EndpointEnum.PLANTS.value}/{plant_id}/{EndpointEnum.RECORDS_YEARS.value}",
            headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
            params=params,
        )
        return response.json()

    def inverters(self, plant_id: int, limit: int = 20, page: int = 1) -> dict:
        """Return the inverters.

        Args:
            limit (int): A limit.
            page (int): A page.

        Returns:
            dict: A dictionary with the inverters.
        """
        params = {"limit": limit, "page": page}

        response = requests.get(
            f"{self.base_api_url}{EndpointEnum.PLANTS.value}/{plant_id}/{EndpointEnum.INVERTERS.value}",
            headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
            params=params,
        )
        return response.json()

    def alerts(
        self,
        plant_id: int,
        start_date: str,
        end_date: str,
        limit: int = 20,
        page: int = 1,
    ) -> dict:
        """Return the alerts.

        Args:
            plant_id (int): A plant id.
            start_date (str): A start date.
            end_date (str): An end date.
            limit (int, optional): A limit. Defaults to 20.
            page (int, optional): A page. Defaults to 1.

        Returns:
            dict: A dictionary with the alerts.
        """
        params = {
            "limit": limit,
            "page": page,
        }

        if start_date:
            try:
                datetime.strptime(start_date, "%Y-%m-%d")
                params["start_date"] = start_date
            except ValueError:
                raise ValueError("start_date must be in the format YYYY-MM-DD.")

        if end_date:
            try:
                datetime.strptime(end_date, "%Y-%m-%d")
                params["end_date"] = end_date
            except ValueError:
                raise ValueError("end_date must be in the format YYYY-MM-DD.")

        response = requests.get(
            f"{self.base_api_url}{EndpointEnum.PLANTS.value}/{plant_id}/alerts",
            headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
            params=params,
        )
        return response.json()

    def notifications(
        self,
        start_date: str,
        end_date: str,
        pendings: bool = True,
        page: int = 1,
    ) -> dict:
        """Return the notifications.

        Args:
            start_date (str): A start date.
            end_date (str): An end date.
            pendings (bool, optional): A boolean. Defaults to True.
            page (int, optional): A page. Defaults to 1.

        Returns:
            dict: A dictionary with the notifications.
        """
        params = {
            "pendings": pendings,
            "page": page,
        }

        if start_date:
            try:
                datetime.strptime(start_date, "%Y-%m-%d")
                params["start_date"] = start_date
            except ValueError:
                raise ValueError("start_date must be in the format YYYY-MM-DD.")

        if end_date:
            try:
                datetime.strptime(end_date, "%Y-%m-%d")
                params["end_date"] = end_date
            except ValueError:
                raise ValueError("end_date must be in the format YYYY-MM-DD.")

        response = requests.get(
            f"{self.base_api_url}{EndpointEnum.USER.value}/{EndpointEnum.NOTIFICATIONS.value}",
            headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
            params=params,
        )
        return response.json()

base_api_url property

Return the base_api_url attribute.

Returns:

Name Type Description
str str

The base API URL.

email property

Return the email attribute.

Returns:

Name Type Description
EmailStr EmailStr

A valid email address.

plus property

Return the plus attribute.

Returns:

Name Type Description
str str

A string.

token property

Returns the token. If the token is missing or expired, requests a new one.

Returns:

Name Type Description
str str

The token.

__init__(email, plus)

Construct a SolarPlusIntelbras object.

Parameters:

Name Type Description Default
email EmailStr

A valid email address.

required
plus str

A string.

required

Returns:

Name Type Description
None None

The constructor does not return anything.

Source code in solar_plus_intelbras/solar_plus_intelbras.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def __init__(
    self,
    email: EmailStr,
    plus: str,
) -> None:
    """Construct a SolarPlusIntelbras object.

    Args:
        email (EmailStr): A valid email address.
        plus (str): A string.

    Returns:
        None: The constructor does not return anything.
    """
    self.__email = email
    self.__plus = plus
    self.__token = None
    self._token_expiration = None

__repr__()

Return a string representation of the object.

Returns:

Name Type Description
str str

A string representation of the object.

Source code in solar_plus_intelbras/solar_plus_intelbras.py
41
42
43
44
45
46
47
def __repr__(self) -> str:
    """Return a string representation of the object.

    Returns:
        str: A string representation of the object.
    """
    return self.__str__()

__str__()

Return a string representation of the object.

Returns:

Name Type Description
str str

A string representation of the object.

Source code in solar_plus_intelbras/solar_plus_intelbras.py
33
34
35
36
37
38
39
def __str__(self) -> str:
    """Return a string representation of the object.

    Returns:
        str: A string representation of the object.
    """
    return f"<SolarPlusIntelbras {self.email}>"

alerts(plant_id, start_date, end_date, limit=20, page=1)

Return the alerts.

Parameters:

Name Type Description Default
plant_id int

A plant id.

required
start_date str

A start date.

required
end_date str

An end date.

required
limit int

A limit. Defaults to 20.

20
page int

A page. Defaults to 1.

1

Returns:

Name Type Description
dict dict

A dictionary with the alerts.

Source code in solar_plus_intelbras/solar_plus_intelbras.py
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
def alerts(
    self,
    plant_id: int,
    start_date: str,
    end_date: str,
    limit: int = 20,
    page: int = 1,
) -> dict:
    """Return the alerts.

    Args:
        plant_id (int): A plant id.
        start_date (str): A start date.
        end_date (str): An end date.
        limit (int, optional): A limit. Defaults to 20.
        page (int, optional): A page. Defaults to 1.

    Returns:
        dict: A dictionary with the alerts.
    """
    params = {
        "limit": limit,
        "page": page,
    }

    if start_date:
        try:
            datetime.strptime(start_date, "%Y-%m-%d")
            params["start_date"] = start_date
        except ValueError:
            raise ValueError("start_date must be in the format YYYY-MM-DD.")

    if end_date:
        try:
            datetime.strptime(end_date, "%Y-%m-%d")
            params["end_date"] = end_date
        except ValueError:
            raise ValueError("end_date must be in the format YYYY-MM-DD.")

    response = requests.get(
        f"{self.base_api_url}{EndpointEnum.PLANTS.value}/{plant_id}/alerts",
        headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
        params=params,
    )
    return response.json()

inverters(plant_id, limit=20, page=1)

Return the inverters.

Parameters:

Name Type Description Default
limit int

A limit.

20
page int

A page.

1

Returns:

Name Type Description
dict dict

A dictionary with the inverters.

Source code in solar_plus_intelbras/solar_plus_intelbras.py
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
def inverters(self, plant_id: int, limit: int = 20, page: int = 1) -> dict:
    """Return the inverters.

    Args:
        limit (int): A limit.
        page (int): A page.

    Returns:
        dict: A dictionary with the inverters.
    """
    params = {"limit": limit, "page": page}

    response = requests.get(
        f"{self.base_api_url}{EndpointEnum.PLANTS.value}/{plant_id}/{EndpointEnum.INVERTERS.value}",
        headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
        params=params,
    )
    return response.json()

notifications(start_date, end_date, pendings=True, page=1)

Return the notifications.

Parameters:

Name Type Description Default
start_date str

A start date.

required
end_date str

An end date.

required
pendings bool

A boolean. Defaults to True.

True
page int

A page. Defaults to 1.

1

Returns:

Name Type Description
dict dict

A dictionary with the notifications.

Source code in solar_plus_intelbras/solar_plus_intelbras.py
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
def notifications(
    self,
    start_date: str,
    end_date: str,
    pendings: bool = True,
    page: int = 1,
) -> dict:
    """Return the notifications.

    Args:
        start_date (str): A start date.
        end_date (str): An end date.
        pendings (bool, optional): A boolean. Defaults to True.
        page (int, optional): A page. Defaults to 1.

    Returns:
        dict: A dictionary with the notifications.
    """
    params = {
        "pendings": pendings,
        "page": page,
    }

    if start_date:
        try:
            datetime.strptime(start_date, "%Y-%m-%d")
            params["start_date"] = start_date
        except ValueError:
            raise ValueError("start_date must be in the format YYYY-MM-DD.")

    if end_date:
        try:
            datetime.strptime(end_date, "%Y-%m-%d")
            params["end_date"] = end_date
        except ValueError:
            raise ValueError("end_date must be in the format YYYY-MM-DD.")

    response = requests.get(
        f"{self.base_api_url}{EndpointEnum.USER.value}/{EndpointEnum.NOTIFICATIONS.value}",
        headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
        params=params,
    )
    return response.json()

plants()

Return the plants.

Returns:

Name Type Description
dict dict

A dictionary with the plants.

Source code in solar_plus_intelbras/solar_plus_intelbras.py
123
124
125
126
127
128
129
130
131
132
133
def plants(self) -> dict:
    """Return the plants.

    Returns:
        dict: A dictionary with the plants.
    """
    response = requests.get(
        f"{self.base_api_url}{EndpointEnum.PLANTS.value}",
        headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
    )
    return response.json()

plants_detail(plant_id)

Return the plant.

Parameters:

Name Type Description Default
plant_id int

A plant id.

required

Returns:

Name Type Description
dict dict

A dictionary with the plants.

Source code in solar_plus_intelbras/solar_plus_intelbras.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
def plants_detail(self, plant_id: int) -> dict:
    """Return the plant.

    Args:
        plant_id (int): A plant id.

    Returns:
        dict: A dictionary with the plants.
    """
    response = requests.get(
        f"{self.base_api_url}{EndpointEnum.PLANTS.value}/{plant_id}",
        headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
    )
    return response.json()

records(plant_id, period, key, start_date, end_date)

Return the records.

Parameters:

Name Type Description Default
plant_id int

A plant id.

required
period PeriodEnum

A period.

required
key KeyEnum

A key.

required
start_date str

A start date.

required
end_date str

An end date.

required

Returns:

Name Type Description
dict dict

A dictionary with the records.

Source code in solar_plus_intelbras/solar_plus_intelbras.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
def records(
    self,
    plant_id: int,
    period: PeriodEnum,
    key: KeyEnum,
    start_date: str,
    end_date: str,
) -> dict:
    """Return the records.

    Args:
        plant_id (int): A plant id.
        period (PeriodEnum): A period.
        key (KeyEnum): A key.
        start_date (str): A start date.
        end_date (str): An end date.

    Returns:
        dict: A dictionary with the records.
    """
    params = {
        "period": period,
        "key": key,
    }

    try:
        datetime.strptime(start_date, "%Y-%m-%d")
        params["start_date"] = start_date
    except ValueError:
        raise ValueError("start_date must be in the format YYYY-MM-DD.")

    try:
        datetime.strptime(end_date, "%Y-%m-%d")
        params["end_date"] = end_date
    except ValueError:
        raise ValueError("end_date must be in the format YYYY-MM-DD.")

    response = requests.get(
        f"{self.base_api_url}{EndpointEnum.PLANTS.value}/{plant_id}/{EndpointEnum.RECORDS.value}",
        headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
        params=params,
    )
    return response.json()

records_year(year, plant_id)

Return the records of a year.

Parameters:

Name Type Description Default
year int

A year.

required
plant_id int

A plant id.

required

Returns:

Name Type Description
dict dict

A dictionary with the records.

Source code in solar_plus_intelbras/solar_plus_intelbras.py
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
def records_year(
    self,
    year: int,
    plant_id: int,
) -> dict:
    """Return the records of a year.

    Args:
        year (int): A year.
        plant_id (int): A plant id.

    Returns:
        dict: A dictionary with the records.
    """
    params = {
        "key": KeyEnum.ENERGY_TODAY.value,
        "year": year,
        "period": PeriodEnum.YEAR.value,
    }

    response = requests.get(
        f"{self.base_api_url}{EndpointEnum.PLANTS.value}/{plant_id}/{EndpointEnum.RECORDS_YEAR.value}",
        headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
        params=params,
    )
    return response.json()

records_years(start_year, end_year, plant_id)

Return the records of a year.

Parameters:

Name Type Description Default
start_year int

A year.

required
end_year int

A year.

required
plant_id int

A plant id.

required

Returns:

Name Type Description
dict dict

A dictionary with the records.

Source code in solar_plus_intelbras/solar_plus_intelbras.py
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
def records_years(
    self,
    start_year: int,
    end_year: int,
    plant_id: int,
) -> dict:
    """Return the records of a year.

    Args:
        start_year (int): A year.
        end_year (int): A year.
        plant_id (int): A plant id.

    Returns:
        dict: A dictionary with the records.
    """

    params = {
        "start_year": start_year,
        "end_year": end_year,
        "key": KeyEnum.ENERGY_TODAY.value,
    }

    response = requests.get(
        f"{self.base_api_url}{EndpointEnum.PLANTS.value}/{plant_id}/{EndpointEnum.RECORDS_YEARS.value}",
        headers={"Authorization": f"Bearer {self.token}", "plus": self.plus},
        params=params,
    )
    return response.json()