Convert ebox to use NamedTuple (#53272)

* Convert to use NamedTuple.

* Convert to NamedTuple.

* Use _attr variables.

* Review comments.
This commit is contained in:
jan iversen 2021-07-21 19:43:33 +02:00 committed by GitHub
parent aed7cb9120
commit 217c625c9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,8 +3,11 @@ Support for EBox.
Get data from 'My Usage Page' page: https://client.ebox.ca/myusage Get data from 'My Usage Page' page: https://client.ebox.ca/myusage
""" """
from __future__ import annotations
from datetime import timedelta from datetime import timedelta
import logging import logging
from typing import NamedTuple
from pyebox import EboxClient from pyebox import EboxClient
from pyebox.client import PyEboxError from pyebox.client import PyEboxError
@ -34,24 +37,81 @@ REQUESTS_TIMEOUT = 15
SCAN_INTERVAL = timedelta(minutes=15) SCAN_INTERVAL = timedelta(minutes=15)
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=15) MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=15)
class EboxSensorMetadata(NamedTuple):
"""Metadata for an individual ebox sensor."""
name: str
unit_of_measurement: str
icon: str
SENSOR_TYPES = { SENSOR_TYPES = {
"usage": ["Usage", PERCENTAGE, "mdi:percent"], "usage": EboxSensorMetadata(
"balance": ["Balance", PRICE, "mdi:cash-usd"], "Usage",
"limit": ["Data limit", DATA_GIGABITS, "mdi:download"], unit_of_measurement=PERCENTAGE,
"days_left": ["Days left", TIME_DAYS, "mdi:calendar-today"], icon="mdi:percent",
"before_offpeak_download": [ ),
"balance": EboxSensorMetadata(
"Balance",
unit_of_measurement=PRICE,
icon="mdi:cash-usd",
),
"limit": EboxSensorMetadata(
"Data limit",
unit_of_measurement=DATA_GIGABITS,
icon="mdi:download",
),
"days_left": EboxSensorMetadata(
"Days left",
unit_of_measurement=TIME_DAYS,
icon="mdi:calendar-today",
),
"before_offpeak_download": EboxSensorMetadata(
"Download before offpeak", "Download before offpeak",
DATA_GIGABITS, unit_of_measurement=DATA_GIGABITS,
"mdi:download", icon="mdi:download",
], ),
"before_offpeak_upload": ["Upload before offpeak", DATA_GIGABITS, "mdi:upload"], "before_offpeak_upload": EboxSensorMetadata(
"before_offpeak_total": ["Total before offpeak", DATA_GIGABITS, "mdi:download"], "Upload before offpeak",
"offpeak_download": ["Offpeak download", DATA_GIGABITS, "mdi:download"], unit_of_measurement=DATA_GIGABITS,
"offpeak_upload": ["Offpeak Upload", DATA_GIGABITS, "mdi:upload"], icon="mdi:upload",
"offpeak_total": ["Offpeak Total", DATA_GIGABITS, "mdi:download"], ),
"download": ["Download", DATA_GIGABITS, "mdi:download"], "before_offpeak_total": EboxSensorMetadata(
"upload": ["Upload", DATA_GIGABITS, "mdi:upload"], "Total before offpeak",
"total": ["Total", DATA_GIGABITS, "mdi:download"], unit_of_measurement=DATA_GIGABITS,
icon="mdi:download",
),
"offpeak_download": EboxSensorMetadata(
"Offpeak download",
unit_of_measurement=DATA_GIGABITS,
icon="mdi:download",
),
"offpeak_upload": EboxSensorMetadata(
"Offpeak Upload",
unit_of_measurement=DATA_GIGABITS,
icon="mdi:upload",
),
"offpeak_total": EboxSensorMetadata(
"Offpeak Total",
unit_of_measurement=DATA_GIGABITS,
icon="mdi:download",
),
"download": EboxSensorMetadata(
"Download",
unit_of_measurement=DATA_GIGABITS,
icon="mdi:download",
),
"upload": EboxSensorMetadata(
"Upload",
unit_of_measurement=DATA_GIGABITS,
icon="mdi:upload",
),
"total": EboxSensorMetadata(
"Total",
unit_of_measurement=DATA_GIGABITS,
icon="mdi:download",
),
} }
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
@ -94,34 +154,19 @@ class EBoxSensor(SensorEntity):
def __init__(self, ebox_data, sensor_type, name): def __init__(self, ebox_data, sensor_type, name):
"""Initialize the sensor.""" """Initialize the sensor."""
self.client_name = name
self.type = sensor_type self.type = sensor_type
self._name = SENSOR_TYPES[sensor_type][0] metadata = SENSOR_TYPES[sensor_type]
self._unit_of_measurement = SENSOR_TYPES[sensor_type][1] self._attr_name = f"{name} {metadata.name}"
self._icon = SENSOR_TYPES[sensor_type][2] self._attr_unit_of_measurement = metadata.unit_of_measurement
self._attr_icon = metadata.icon
self.ebox_data = ebox_data self.ebox_data = ebox_data
self._state = None self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return f"{self.client_name} {self._name}"
@property @property
def state(self): def state(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
return self._state return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
return self._unit_of_measurement
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return self._icon
async def async_update(self): async def async_update(self):
"""Get the latest data from EBox and update the state.""" """Get the latest data from EBox and update the state."""
await self.ebox_data.async_update() await self.ebox_data.async_update()