mirror of
https://github.com/ZaneThePython/openjibo-haint.git
synced 2026-06-13 01:35:58 +00:00
Add new connectivity checker
This commit is contained in:
@@ -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
|
||||
|
||||
63
custom_components/jibo/binary_sensor.py
Normal file
63
custom_components/jibo/binary_sensor.py
Normal file
@@ -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
|
||||
@@ -1 +1,2 @@
|
||||
DOMAIN = "jibo"
|
||||
DOMAIN = "jibo"
|
||||
PLATFORMS = ["binary_sensor"]
|
||||
|
||||
@@ -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": [],
|
||||
|
||||
@@ -17,5 +17,12 @@
|
||||
"abort": {
|
||||
"already_configured": "A Jibo robot with this IP address is already configured."
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
"binary_sensor": {
|
||||
"connectivity": {
|
||||
"name": "Online"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user