1 """Support for Bbox Bouygues Modem Router."""
3 from __future__
import annotations
5 from datetime
import timedelta
10 import voluptuous
as vol
13 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
16 SensorEntityDescription,
27 _LOGGER = logging.getLogger(__name__)
29 ATTRIBUTION =
"Powered by Bouygues Telecom"
35 SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
37 key=
"down_max_bandwidth",
38 name=
"Maximum Download Bandwidth",
39 device_class=SensorDeviceClass.DATA_RATE,
40 native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
44 key=
"up_max_bandwidth",
45 name=
"Maximum Upload Bandwidth",
46 device_class=SensorDeviceClass.DATA_RATE,
47 native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
51 key=
"current_down_bandwidth",
52 name=
"Currently Used Download Bandwidth",
53 device_class=SensorDeviceClass.DATA_RATE,
54 native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
55 state_class=SensorStateClass.MEASUREMENT,
59 key=
"current_up_bandwidth",
60 name=
"Currently Used Upload Bandwidth",
61 device_class=SensorDeviceClass.DATA_RATE,
62 native_unit_of_measurement=UnitOfDataRate.MEGABITS_PER_SECOND,
63 state_class=SensorStateClass.MEASUREMENT,
67 key=
"number_of_reboots",
68 name=
"Number of reboot",
73 SENSOR_TYPES_UPTIME: tuple[SensorEntityDescription, ...] = (
81 SENSOR_KEYS: list[str] = [desc.key
for desc
in (*SENSOR_TYPES, *SENSOR_TYPES_UPTIME)]
83 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
85 vol.Required(CONF_MONITORED_VARIABLES): vol.All(
86 cv.ensure_list, [vol.In(SENSOR_KEYS)]
88 vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
96 add_entities: AddEntitiesCallback,
97 discovery_info: DiscoveryInfoType |
None =
None,
99 """Set up the Bbox sensor."""
105 except requests.exceptions.HTTPError
as error:
109 name = config[CONF_NAME]
111 monitored_variables = config[CONF_MONITORED_VARIABLES]
112 entities: list[BboxSensor | BboxUptimeSensor] = [
114 for description
in SENSOR_TYPES
115 if description.key
in monitored_variables
120 for description
in SENSOR_TYPES_UPTIME
121 if description.key
in monitored_variables
129 """Bbox uptime sensor."""
131 _attr_attribution = ATTRIBUTION
132 _attr_device_class = SensorDeviceClass.TIMESTAMP
134 def __init__(self, bbox_data, name, description: SensorEntityDescription) ->
None:
135 """Initialize the sensor."""
141 """Get the latest data from Bbox and update the state."""
144 seconds=self.
bbox_databbox_data.router_infos[
"device"][
"uptime"]
149 """Implementation of a Bbox sensor."""
151 _attr_attribution = ATTRIBUTION
153 def __init__(self, bbox_data, name, description: SensorEntityDescription) ->
None:
154 """Initialize the sensor."""
160 """Get the latest data from Bbox and update the state."""
163 if sensor_type ==
"down_max_bandwidth":
165 self.
bbox_databbox_data.data[
"rx"][
"maxBandwidth"] / 1000, 2
167 elif sensor_type ==
"up_max_bandwidth":
169 self.
bbox_databbox_data.data[
"tx"][
"maxBandwidth"] / 1000, 2
171 elif sensor_type ==
"current_down_bandwidth":
173 self.
bbox_databbox_data.data[
"rx"][
"bandwidth"] / 1000, 2
175 elif sensor_type ==
"current_up_bandwidth":
177 self.
bbox_databbox_data.data[
"tx"][
"bandwidth"] / 1000, 2
179 elif sensor_type ==
"number_of_reboots":
186 """Get data from the Bbox."""
189 """Initialize the data object."""
193 @Throttle(MIN_TIME_BETWEEN_UPDATES)
195 """Get the latest data from the Bbox."""
199 self.
datadata = box.get_ip_stats()
201 except requests.exceptions.HTTPError
as error:
None __init__(self, bbox_data, name, SensorEntityDescription description)
None __init__(self, bbox_data, name, SensorEntityDescription description)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
def add_entities(account, async_add_entities, tracked)