コード例 #1
0
async def main():
    hosts = await discovery.async_discover()

    if len(hosts) > 0:
        gateway = ScreenLogicGateway(**hosts[0])
        await gateway.async_connect()
        await gateway.async_update()
        await gateway.async_disconnect()
        pprint.pprint(gateway.get_data())
    else:
        print("No gateways found")
コード例 #2
0
    def reconnect_gateway(self):
        """Instantiate a new ScreenLogicGateway, connect to it and update. Return new gateway to caller."""

        connect_info = get_connect_info(self.hass, self.config_entry)

        try:
            gateway = ScreenLogicGateway(**connect_info)
            gateway.update()
        except ScreenLogicError as error:
            raise UpdateFailed(error) from error

        return gateway
コード例 #3
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up Screenlogic from a config entry."""
    connect_info = await async_get_connect_info(hass, entry)

    gateway = ScreenLogicGateway(**connect_info)

    try:
        await gateway.async_connect()
    except ScreenLogicError as ex:
        _LOGGER.error("Error while connecting to the gateway %s: %s", connect_info, ex)
        raise ConfigEntryNotReady from ex

    coordinator = ScreenlogicDataUpdateCoordinator(
        hass, config_entry=entry, gateway=gateway
    )

    async_load_screenlogic_services(hass)

    await coordinator.async_config_entry_first_refresh()

    entry.async_on_unload(entry.add_update_listener(async_update_listener))

    hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator

    hass.config_entries.async_setup_platforms(entry, PLATFORMS)

    return True
コード例 #4
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
    """Set up Screenlogic from a config entry."""
    mac = entry.unique_id
    # Attempt to re-discover named gateway to follow IP changes
    discovered_gateways = hass.data[DOMAIN][DISCOVERED_GATEWAYS]
    if mac in discovered_gateways:
        connect_info = discovered_gateways[mac]
    else:
        _LOGGER.warning("Gateway rediscovery failed")
        # Static connection defined or fallback from discovery
        connect_info = {
            SL_GATEWAY_NAME: name_for_mac(mac),
            SL_GATEWAY_IP: entry.data[CONF_IP_ADDRESS],
            SL_GATEWAY_PORT: entry.data[CONF_PORT],
        }

    try:
        gateway = ScreenLogicGateway(**connect_info)
    except ScreenLogicError as ex:
        _LOGGER.error("Error while connecting to the gateway %s: %s",
                      connect_info, ex)
        raise ConfigEntryNotReady from ex

    coordinator = ScreenlogicDataUpdateCoordinator(hass,
                                                   config_entry=entry,
                                                   gateway=gateway)

    device_data = defaultdict(list)

    await coordinator.async_refresh()

    for circuit in coordinator.data["circuits"]:
        device_data["switch"].append(circuit)

    for sensor in coordinator.data["sensors"]:
        if sensor == "chem_alarm":
            device_data["binary_sensor"].append(sensor)
        else:
            if coordinator.data["sensors"][sensor]["value"] != 0:
                device_data["sensor"].append(sensor)

    for pump in coordinator.data["pumps"]:
        if (coordinator.data["pumps"][pump]["data"] != 0
                and "currentWatts" in coordinator.data["pumps"][pump]):
            device_data["pump"].append(pump)

    for body in coordinator.data["bodies"]:
        device_data["body"].append(body)

    hass.data[DOMAIN][entry.entry_id] = {
        "coordinator": coordinator,
        "devices": device_data,
        "listener": entry.add_update_listener(async_update_listener),
    }

    for component in PLATFORMS:
        hass.async_create_task(
            hass.config_entries.async_forward_entry_setup(entry, component))

    return True
コード例 #5
0
async def MockConnectedGateway(
    event_loop: asyncio.AbstractEventLoop, MockProtocolAdapter
):
    async with MockProtocolAdapter:
        gateway = ScreenLogicGateway(**FAKE_CONNECT_INFO)
        await gateway.async_connect()
        assert gateway.is_connected
        await gateway.async_update()
        yield gateway
コード例 #6
0
    async def _async_reconnect_update_data(self):
        """Attempt to reconnect to the gateway and fetch data."""
        try:
            # Clean up the previous connection as we're about to create a new one
            await self.gateway.async_disconnect()

            connect_info = await async_get_connect_info(self.hass, self.config_entry)
            self.gateway = ScreenLogicGateway(**connect_info)

            await self.gateway.async_update()

        except (ScreenLogicError, ScreenLogicWarning) as ex:
            raise UpdateFailed(ex) from ex
コード例 #7
0
def get_new_gateway(hass: HomeAssistant, entry: ConfigEntry):
    """Instantiate a new ScreenLogicGateway, connect to it and return it to caller."""

    connect_info = get_connect_info(hass, entry)

    try:
        gateway = ScreenLogicGateway(**connect_info)
    except ScreenLogicError as ex:
        _LOGGER.error("Error while connecting to the gateway %s: %s",
                      connect_info, ex)
        raise ConfigEntryNotReady from ex

    return gateway
コード例 #8
0
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    """Set up Screenlogic from a config entry."""
    mac = entry.unique_id
    # Attempt to re-discover named gateway to follow IP changes
    discovered_gateways = hass.data[DOMAIN][DISCOVERED_GATEWAYS]
    if mac in discovered_gateways:
        connect_info = discovered_gateways[mac]
    else:
        _LOGGER.warning("Gateway rediscovery failed")
        # Static connection defined or fallback from discovery
        connect_info = {
            SL_GATEWAY_NAME: name_for_mac(mac),
            SL_GATEWAY_IP: entry.data[CONF_IP_ADDRESS],
            SL_GATEWAY_PORT: entry.data[CONF_PORT],
        }

    try:
        gateway = ScreenLogicGateway(**connect_info)
    except ScreenLogicError as ex:
        _LOGGER.error("Error while connecting to the gateway %s: %s",
                      connect_info, ex)
        raise ConfigEntryNotReady from ex

    # The api library uses a shared socket connection and does not handle concurrent
    # requests very well.
    api_lock = asyncio.Lock()

    coordinator = ScreenlogicDataUpdateCoordinator(hass,
                                                   config_entry=entry,
                                                   gateway=gateway,
                                                   api_lock=api_lock)

    async_load_screenlogic_services(hass)

    await coordinator.async_config_entry_first_refresh()

    hass.data[DOMAIN][entry.entry_id] = {
        "coordinator": coordinator,
        "listener": entry.add_update_listener(async_update_listener),
    }

    hass.config_entries.async_setup_platforms(entry, PLATFORMS)

    return True