diff --git a/custom_components/jibo/__init__.py b/custom_components/jibo/__init__.py index aff53c0..7e9dfda 100644 --- a/custom_components/jibo/__init__.py +++ b/custom_components/jibo/__init__.py @@ -3,7 +3,7 @@ import voluptuous as vol from homeassistant.core import HomeAssistant, ServiceCall import logging -from .const import DOMAIN +from .const import DOMAIN, PLATFORMS _LOGGER = logging.getLogger(__name__) @@ -80,13 +80,17 @@ async def async_setup_entry(hass: HomeAssistant, entry): hass.services.async_register(DOMAIN, "say", handle_say, schema=_SAY_SCHEMA) + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True async def async_unload_entry(hass: HomeAssistant, entry): - hass.data[DOMAIN].pop(entry.entry_id, None) + unloaded = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) - if not hass.data[DOMAIN]: - hass.services.async_remove(DOMAIN, "say") + if unloaded: + hass.data[DOMAIN].pop(entry.entry_id, None) - return True + if not hass.data[DOMAIN]: + hass.services.async_remove(DOMAIN, "say") + + return unloaded diff --git a/custom_components/jibo/binary_sensor.py b/custom_components/jibo/binary_sensor.py new file mode 100644 index 0000000..435e4eb --- /dev/null +++ b/custom_components/jibo/binary_sensor.py @@ -0,0 +1,63 @@ +import asyncio +import logging +from datetime import timedelta + +from homeassistant.components.binary_sensor import ( + BinarySensorDeviceClass, + BinarySensorEntity, +) +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +from .const import DOMAIN + +_LOGGER = logging.getLogger(__name__) + +SCAN_INTERVAL = timedelta(seconds=30) +_CONNECT_TIMEOUT = 5.0 +_JIBO_PORT = 8089 + + +async def async_setup_entry( + hass: HomeAssistant, + entry: ConfigEntry, + async_add_entities: AddEntitiesCallback, +) -> None: + data = hass.data[DOMAIN][entry.entry_id] + async_add_entities( + [JiboConnectivitySensor(entry, data["jibo_ip"], data["name"])], + update_before_add=True, + ) + + +class JiboConnectivitySensor(BinarySensorEntity): + _attr_device_class = BinarySensorDeviceClass.CONNECTIVITY + _attr_should_poll = True + + def __init__(self, entry: ConfigEntry, ip: str, name: str) -> None: + self._ip = ip + self._attr_unique_id = f"{entry.entry_id}_connectivity" + self._attr_name = f"{name} Online" + self._attr_is_on = False + self._attr_device_info = { + "identifiers": {(DOMAIN, entry.entry_id)}, + "name": name, + "manufacturer": "Jibo Inc.", + "model": "Jibo", + } + + async def async_update(self) -> None: + try: + _, writer = await asyncio.wait_for( + asyncio.open_connection(self._ip, _JIBO_PORT), + timeout=_CONNECT_TIMEOUT, + ) + writer.close() + try: + await writer.wait_closed() + except Exception: + pass + self._attr_is_on = True + except (asyncio.TimeoutError, OSError): + self._attr_is_on = False diff --git a/custom_components/jibo/const.py b/custom_components/jibo/const.py index c148a37..3aa79e4 100644 --- a/custom_components/jibo/const.py +++ b/custom_components/jibo/const.py @@ -1 +1,2 @@ -DOMAIN = "jibo" \ No newline at end of file +DOMAIN = "jibo" +PLATFORMS = ["binary_sensor"] diff --git a/custom_components/jibo/manifest.json b/custom_components/jibo/manifest.json index b3e6160..d71d00f 100644 --- a/custom_components/jibo/manifest.json +++ b/custom_components/jibo/manifest.json @@ -1,7 +1,7 @@ { "domain": "jibo", "name": "OpenJibo", - "version": "0.1.0.3", + "version": "0.1.0.4", "documentation": "https://jibohacks.zane.org/homeassistant/int", "requirements": [], "dependencies": [], diff --git a/custom_components/jibo/translations/en.json b/custom_components/jibo/translations/en.json index 9a69655..7a3ae9c 100644 --- a/custom_components/jibo/translations/en.json +++ b/custom_components/jibo/translations/en.json @@ -17,5 +17,12 @@ "abort": { "already_configured": "A Jibo robot with this IP address is already configured." } + }, + "entity": { + "binary_sensor": { + "connectivity": { + "name": "Online" + } + } } }