mirror of
https://github.com/home-assistant/core.git
synced 2025-11-06 17:40:11 +00:00
* Adding config flow and tests
* Removing update and adding to integrations.json
* Updating hassfest
* Removing comments
* Removing unique ID
* Putting the setup_platform out of order
* Adding feedback on issues and importing
* Removing uniqueID (again)
* Adjusting unload and typo
* Updating manifest properly
* Minor patching
* Removing hass.data.setdefault(DOMAIN, {})
* Moving load_platform to __init__.py
* Update homeassistant/components/fastdotcom/config_flow.py
Co-authored-by: G Johansson <goran.johansson@shiftit.se>
* Update homeassistant/components/fastdotcom/strings.json
Co-authored-by: G Johansson <goran.johansson@shiftit.se>
* Update homeassistant/components/fastdotcom/__init__.py
Co-authored-by: G Johansson <goran.johansson@shiftit.se>
* Update homeassistant/components/fastdotcom/config_flow.py
Co-authored-by: G Johansson <goran.johansson@shiftit.se>
* Adding an unload function for the timer
* Adding issue on setup platform in sensor
* Update homeassistant/components/fastdotcom/config_flow.py
Co-authored-by: G Johansson <goran.johansson@shiftit.se>
* Removing platform
* Fixing strings.json
* Fine-tuning
* Putting back last_state
---------
Co-authored-by: G Johansson <goran.johansson@shiftit.se>
68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
"""Support for Fast.com internet speed testing sensor."""
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from homeassistant.components.sensor import (
|
|
SensorDeviceClass,
|
|
SensorEntity,
|
|
SensorStateClass,
|
|
)
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import UnitOfDataRate
|
|
from homeassistant.core import HomeAssistant, callback
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
|
|
|
from .const import DATA_UPDATED, DOMAIN
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
"""Set up the Fast.com sensor."""
|
|
async_add_entities([SpeedtestSensor(hass.data[DOMAIN])])
|
|
|
|
|
|
# pylint: disable-next=hass-invalid-inheritance # needs fixing
|
|
class SpeedtestSensor(RestoreEntity, SensorEntity):
|
|
"""Implementation of a Fast.com sensor."""
|
|
|
|
_attr_name = "Fast.com Download"
|
|
_attr_device_class = SensorDeviceClass.DATA_RATE
|
|
_attr_native_unit_of_measurement = UnitOfDataRate.MEGABITS_PER_SECOND
|
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
|
_attr_icon = "mdi:speedometer"
|
|
_attr_should_poll = False
|
|
|
|
def __init__(self, speedtest_data: dict[str, Any]) -> None:
|
|
"""Initialize the sensor."""
|
|
self._speedtest_data = speedtest_data
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
"""Handle entity which will be added."""
|
|
await super().async_added_to_hass()
|
|
|
|
self.async_on_remove(
|
|
async_dispatcher_connect(
|
|
self.hass, DATA_UPDATED, self._schedule_immediate_update
|
|
)
|
|
)
|
|
|
|
if not (state := await self.async_get_last_state()):
|
|
return
|
|
self._attr_native_value = state.state
|
|
|
|
def update(self) -> None:
|
|
"""Get the latest data and update the states."""
|
|
if (data := self._speedtest_data.data) is None: # type: ignore[attr-defined]
|
|
return
|
|
self._attr_native_value = data["download"]
|
|
|
|
@callback
|
|
def _schedule_immediate_update(self) -> None:
|
|
self.async_schedule_update_ha_state(True)
|