1 """Component for wiffi support."""
3 from datetime
import timedelta
7 from wiffi
import WiffiTcpServer
17 CHECK_ENTITIES_SIGNAL,
22 from .entity
import generate_unique_id
24 _LOGGER = logging.getLogger(__name__)
27 PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
31 """Set up wiffi from a config entry, config_entry contains data from config entry database."""
32 if not entry.update_listeners:
33 entry.add_update_listener(async_update_options)
37 api.async_setup(entry)
40 hass.data.setdefault(DOMAIN, {})[entry.entry_id] = api
43 await api.server.start_server()
44 except OSError
as exc:
45 if exc.errno != errno.EADDRINUSE:
46 _LOGGER.error(
"Start_server failed, errno: %d", exc.errno)
48 _LOGGER.error(
"Port %s already in use", entry.data[CONF_PORT])
49 raise ConfigEntryNotReady
from exc
51 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
58 await hass.config_entries.async_reload(entry.entry_id)
62 """Unload a config entry."""
63 api: WiffiIntegrationApi = hass.data[DOMAIN][entry.entry_id]
64 await api.server.close_server()
66 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
68 api = hass.data[DOMAIN].pop(entry.entry_id)
75 """API object for wiffi handling. Stored in hass.data."""
78 """Initialize the instance."""
85 """Set up api instance."""
86 self.
_server_server = WiffiTcpServer(config_entry.data[CONF_PORT], self)
92 """Shutdown wiffi api.
94 Remove listener for periodic callbacks.
100 """Process callback from TCP server if new data arrives from a device."""
105 for metric
in metrics:
106 if metric.id
not in self.
_known_devices_known_devices[device.mac_address]:
112 f
"{UPDATE_ENTITY_SIGNAL}-{generate_unique_id(device, metric)}",
119 """Return TCP server instance for start + close."""
124 """Check if any entity has timed out because it has not been updated."""
def __call__(self, device, metrics)
def async_setup(self, config_entry)
def _periodic_tick(self, now=None)
bool add(self, _T matcher)
None async_update_options(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
None async_dispatcher_send(HomeAssistant hass, str signal, *Any args)
CALLBACK_TYPE async_track_time_interval(HomeAssistant hass, Callable[[datetime], Coroutine[Any, Any, None]|None] action, timedelta interval, *str|None name=None, bool|None cancel_on_shutdown=None)