Working on igd
This commit is contained in:
1 parent
50f63ed4c5
commit
8bec4a55d1
10 files changed
+206
-231
No files matched your search
@@ -32,11 +32,11 @@ SENSOR_TYPES = {
|
||||
},
|
||||
PACKETS_RECEIVED: {
|
||||
'name': 'packets received',
|
||||
'unit': '#',
|
||||
'unit': 'packets',
|
||||
},
|
||||
PACKETS_SENT: {
|
||||
'name': 'packets sent',
|
||||
'unit': '#',
|
||||
'unit': 'packets',
|
||||
},
|
||||
}
|
||||
|
||||
@@ -54,22 +54,20 @@ async def async_setup_platform(hass, config, async_add_devices,
|
||||
udn = discovery_info['udn']
|
||||
igd_device = hass.data[DOMAIN]['devices'][udn]
|
||||
|
||||
# raw sensors
|
||||
async_add_devices([
|
||||
# raw sensors + per-second sensors
|
||||
sensors = [
|
||||
RawIGDSensor(igd_device, name, sensor_type)
|
||||
for name, sensor_type in SENSOR_TYPES.items()],
|
||||
True
|
||||
)
|
||||
|
||||
# current traffic reporting
|
||||
async_add_devices(
|
||||
[
|
||||
KBytePerSecondIGDSensor(igd_device, IN),
|
||||
KBytePerSecondIGDSensor(igd_device, OUT),
|
||||
PacketsPerSecondIGDSensor(igd_device, IN),
|
||||
PacketsPerSecondIGDSensor(igd_device, OUT),
|
||||
], True
|
||||
)
|
||||
for name, sensor_type in SENSOR_TYPES.items()
|
||||
]
|
||||
sensors += [
|
||||
KBytePerSecondIGDSensor(igd_device, IN),
|
||||
KBytePerSecondIGDSensor(igd_device, OUT),
|
||||
PacketsPerSecondIGDSensor(igd_device, IN),
|
||||
PacketsPerSecondIGDSensor(igd_device, OUT),
|
||||
]
|
||||
hass.data[DOMAIN]['sensors'][udn] = sensors
|
||||
async_add_devices(sensors, True)
|
||||
return True
|
||||
|
||||
|
||||
class RawIGDSensor(Entity):
|
||||
@@ -80,7 +78,7 @@ class RawIGDSensor(Entity):
|
||||
self._device = device
|
||||
self._type_name = sensor_type_name
|
||||
self._type = sensor_type
|
||||
self._name = 'IGD {}'.format(sensor_type['name'])
|
||||
self._name = '{} {}'.format(device.name, sensor_type['name'])
|
||||
self._state = None
|
||||
|
||||
@property
|
||||
@@ -88,6 +86,11 @@ class RawIGDSensor(Entity):
|
||||
"""Return the name of the sensor."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return an unique ID."""
|
||||
return '{}_{}'.format(self._device.udn, self._type_name)
|
||||
|
||||
@property
|
||||
def state(self) -> str:
|
||||
"""Return the state of the device."""
|
||||
@@ -116,143 +119,121 @@ class RawIGDSensor(Entity):
|
||||
self._state = await self._device.async_get_total_packets_sent()
|
||||
|
||||
|
||||
class KBytePerSecondIGDSensor(Entity):
|
||||
class PerSecondIGDSensor(Entity):
|
||||
"""Abstract representation of a X Sent/Received per second sensor."""
|
||||
|
||||
def __init__(self, device, direction):
|
||||
"""Initializer."""
|
||||
self._device = device
|
||||
self._direction = direction
|
||||
|
||||
self._state = None
|
||||
self._last_value = None
|
||||
self._last_update_time = None
|
||||
|
||||
@property
|
||||
def unit(self) -> str:
|
||||
"""Unit we are measuring in."""
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def _async_fetch_value(self):
|
||||
"""Fetch a value from the IGD."""
|
||||
raise NotImplementedError()
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return an unique ID."""
|
||||
return '{}_{}/sec_{}'.format(self._device.udn,
|
||||
self.unit,
|
||||
self._direction)
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name of the sensor."""
|
||||
return '{} {}/sec {}'.format(self._device.name,
|
||||
self.unit,
|
||||
self._direction)
|
||||
|
||||
@property
|
||||
def icon(self) -> str:
|
||||
"""Icon to use in the frontend, if any."""
|
||||
return 'mdi:server-network'
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self) -> str:
|
||||
"""Return the unit of measurement of this entity, if any."""
|
||||
return '{}/sec'.format(self.unit)
|
||||
|
||||
def _is_overflowed(self, new_value) -> bool:
|
||||
"""Check if value has overflowed."""
|
||||
return new_value < self._last_value
|
||||
|
||||
async def async_update(self):
|
||||
"""Get the latest information from the IGD."""
|
||||
_LOGGER.debug('%s: async_update', self)
|
||||
new_value = await self._async_fetch_value()
|
||||
|
||||
if self._last_value is None:
|
||||
self._last_value = new_value
|
||||
self._last_update_time = datetime.now()
|
||||
return
|
||||
|
||||
now = datetime.now()
|
||||
if self._is_overflowed(new_value):
|
||||
self._state = None # temporarily report nothing
|
||||
else:
|
||||
delta_time = (now - self._last_update_time).seconds
|
||||
delta_value = new_value - self._last_value
|
||||
self._state = (delta_value / delta_time)
|
||||
|
||||
self._last_value = new_value
|
||||
self._last_update_time = now
|
||||
|
||||
|
||||
class KBytePerSecondIGDSensor(PerSecondIGDSensor):
|
||||
"""Representation of a KBytes Sent/Received per second sensor."""
|
||||
|
||||
def __init__(self, device, direction):
|
||||
"""Initializer."""
|
||||
self._device = device
|
||||
self._direction = direction
|
||||
|
||||
self._last_value = None
|
||||
self._last_update_time = None
|
||||
self._state = None
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return an unique ID."""
|
||||
return '{}:kbytes_{}'.format(self._device.udn, self._direction)
|
||||
def unit(self) -> str:
|
||||
"""Unit we are measuring in."""
|
||||
return 'kbyte'
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name of the sensor."""
|
||||
return '{} kbytes/sec {}'.format(self._device.name,
|
||||
self._direction)
|
||||
async def _async_fetch_value(self) -> float:
|
||||
""""""
|
||||
if self._direction == IN:
|
||||
return await self._device.async_get_total_bytes_received()
|
||||
|
||||
return await self._device.async_get_total_bytes_sent()
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the device."""
|
||||
return self._state
|
||||
if self._state is None:
|
||||
return None
|
||||
|
||||
@property
|
||||
def icon(self) -> str:
|
||||
"""Icon to use in the frontend, if any."""
|
||||
return 'mdi:server-network'
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self) -> str:
|
||||
"""Return the unit of measurement of this entity, if any."""
|
||||
return 'kbytes/sec'
|
||||
|
||||
def _is_overflowed(self, new_value) -> bool:
|
||||
"""Check if value has overflowed."""
|
||||
return new_value < self._last_value
|
||||
|
||||
async def async_update(self):
|
||||
"""Get the latest information from the IGD."""
|
||||
_LOGGER.debug('%s: async_update', self)
|
||||
|
||||
if self._direction == IN:
|
||||
new_value = await self._device.async_get_total_bytes_received()
|
||||
else:
|
||||
new_value = await self._device.async_get_total_bytes_sent()
|
||||
|
||||
if self._last_value is None:
|
||||
self._last_value = new_value
|
||||
self._last_update_time = datetime.now()
|
||||
return
|
||||
|
||||
now = datetime.now()
|
||||
if self._is_overflowed(new_value):
|
||||
_LOGGER.debug('%s: Overflow: old value: %s, new value: %s',
|
||||
self, self._last_value, new_value)
|
||||
self._state = None # temporarily report nothing
|
||||
else:
|
||||
delta_time = (now - self._last_update_time).seconds
|
||||
delta_value = new_value - self._last_value
|
||||
value = (delta_value / delta_time) / KBYTE
|
||||
self._state = format(float(value), '.1f')
|
||||
|
||||
self._last_value = new_value
|
||||
self._last_update_time = now
|
||||
return format(float(self._state / KBYTE), '.1f')
|
||||
|
||||
|
||||
class PacketsPerSecondIGDSensor(Entity):
|
||||
class PacketsPerSecondIGDSensor(PerSecondIGDSensor):
|
||||
"""Representation of a Packets Sent/Received per second sensor."""
|
||||
|
||||
def __init__(self, device, direction):
|
||||
"""Initializer."""
|
||||
self._device = device
|
||||
self._direction = direction
|
||||
|
||||
self._last_value = None
|
||||
self._last_update_time = None
|
||||
self._state = None
|
||||
|
||||
@property
|
||||
def unique_id(self) -> str:
|
||||
"""Return an unique ID."""
|
||||
return '{}:packets_{}'.format(self._device.udn, self._direction)
|
||||
def unit(self) -> str:
|
||||
"""Unit we are measuring in."""
|
||||
return 'packets'
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name of the sensor."""
|
||||
return '{} packets/sec {}'.format(self._device.name,
|
||||
self._direction)
|
||||
async def _async_fetch_value(self) -> float:
|
||||
""""""
|
||||
if self._direction == IN:
|
||||
return await self._device.async_get_total_packets_received()
|
||||
|
||||
return await self._device.async_get_total_packets_sent()
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the device."""
|
||||
return self._state
|
||||
if self._state is None:
|
||||
return None
|
||||
|
||||
@property
|
||||
def icon(self) -> str:
|
||||
"""Icon to use in the frontend, if any."""
|
||||
return 'mdi:server-network'
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self) -> str:
|
||||
"""Return the unit of measurement of this entity, if any."""
|
||||
return '#/sec'
|
||||
|
||||
def _is_overflowed(self, new_value) -> bool:
|
||||
"""Check if value has overflowed."""
|
||||
return new_value < self._last_value
|
||||
|
||||
async def async_update(self):
|
||||
"""Get the latest information from the IGD."""
|
||||
_LOGGER.debug('%s: async_update', self)
|
||||
|
||||
if self._direction == IN:
|
||||
new_value = await self._device.async_get_total_bytes_received()
|
||||
else:
|
||||
new_value = await self._device.async_get_total_bytes_sent()
|
||||
|
||||
if self._last_value is None:
|
||||
self._last_value = new_value
|
||||
self._last_update_time = datetime.now()
|
||||
return
|
||||
|
||||
now = datetime.now()
|
||||
if self._is_overflowed(new_value):
|
||||
_LOGGER.debug('%s: Overflow: old value: %s, new value: %s',
|
||||
self, self._last_value, new_value)
|
||||
self._state = None # temporarily report nothing
|
||||
else:
|
||||
delta_time = (now - self._last_update_time).seconds
|
||||
delta_value = new_value - self._last_value
|
||||
value = delta_value / delta_time
|
||||
self._state = format(float(value), '.1f')
|
||||
|
||||
self._last_value = new_value
|
||||
self._last_update_time = now
|
||||
return format(float(self._state), '.1f')
|
||||
Reference in new issue
Block a user