generated from BrewBlox/brewblox-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from BrewBlox/develop
Edge release
- Loading branch information
Showing
21 changed files
with
2,134 additions
and
1,144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import logging | ||
from contextlib import AsyncExitStack, asynccontextmanager | ||
from pprint import pformat | ||
|
||
from fastapi import FastAPI | ||
|
||
from . import mqtt, relay, utils | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def setup_logging(debug: bool): | ||
level = logging.DEBUG if debug else logging.INFO | ||
unimportant_level = logging.INFO if debug else logging.WARN | ||
format = '%(asctime)s.%(msecs)03d [%(levelname).1s:%(name)s:%(lineno)d] %(message)s' | ||
datefmt = '%Y/%m/%d %H:%M:%S' | ||
|
||
logging.basicConfig(level=level, format=format, datefmt=datefmt) | ||
logging.captureWarnings(True) | ||
|
||
logging.getLogger('gmqtt').setLevel(unimportant_level) | ||
logging.getLogger('httpx').setLevel(unimportant_level) | ||
logging.getLogger('httpcore').setLevel(logging.WARN) | ||
logging.getLogger('uvicorn.access').setLevel(unimportant_level) | ||
logging.getLogger('uvicorn.error').disabled = True | ||
logging.getLogger('bleak.backends.bluezdbus.manager').setLevel(unimportant_level) | ||
|
||
|
||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
LOGGER.info(utils.get_config()) | ||
LOGGER.debug('LOGGERS:\n' + pformat(logging.root.manager.loggerDict)) | ||
|
||
async with AsyncExitStack() as stack: | ||
await stack.enter_async_context(mqtt.lifespan()) | ||
yield | ||
|
||
|
||
def create_app() -> FastAPI: | ||
config = utils.get_config() | ||
setup_logging(config.debug) | ||
|
||
# Call setup functions for modules | ||
mqtt.setup() | ||
relay.setup() | ||
|
||
app = FastAPI(lifespan=lifespan) | ||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,37 @@ | ||
from typing import Optional | ||
from typing import Literal | ||
|
||
from brewblox_service.models import BaseServiceConfig, MqttProtocol | ||
from pydantic_settings import BaseSettings, SettingsConfigDict | ||
|
||
|
||
class ServiceConfig(BaseServiceConfig): | ||
hass_mqtt_protocol: MqttProtocol | ||
hass_mqtt_host: str | ||
hass_mqtt_port: Optional[int] | ||
hass_mqtt_path: str | ||
class ServiceConfig(BaseSettings): | ||
model_config = SettingsConfigDict( | ||
env_file='.appenv', | ||
env_prefix='brewblox_hass_', | ||
case_sensitive=False, | ||
json_schema_extra='ignore', | ||
) | ||
|
||
name: str = 'hass' | ||
debug: bool = False | ||
debugger: bool = False | ||
|
||
mqtt_protocol: Literal['mqtt', 'mqtts'] = 'mqtt' | ||
mqtt_host: str = 'eventbus' | ||
mqtt_port: int = 1883 | ||
|
||
hass_mqtt_protocol: Literal['mqtt', 'mqtts'] = 'mqtt' | ||
hass_mqtt_host: str = 'eventbus' | ||
hass_mqtt_port: int = 1883 | ||
|
||
state_topic: str = 'brewcast/state' | ||
|
||
|
||
class HassMqttCredentials(BaseSettings): | ||
model_config = SettingsConfigDict( | ||
env_prefix='hass_', | ||
case_sensitive=False, | ||
json_schema_extra='ignore', | ||
) | ||
|
||
mqtt_username: str | None = None | ||
mqtt_password: str | None = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from contextlib import AsyncExitStack, asynccontextmanager | ||
from contextvars import ContextVar | ||
|
||
from fastapi_mqtt.config import MQTTConfig | ||
from fastapi_mqtt.fastmqtt import FastMQTT | ||
|
||
from . import utils | ||
|
||
CV_LOCAL: ContextVar[FastMQTT] = ContextVar('mqtt.client.local') | ||
CV_HASS: ContextVar[FastMQTT] = ContextVar('mqtt.client.hass') | ||
|
||
|
||
def setup(): | ||
config = utils.get_config() | ||
hass_credentials = utils.get_hass_credentials() | ||
|
||
mqtt_config = MQTTConfig(host=config.mqtt_host, | ||
port=config.mqtt_port, | ||
ssl=(config.mqtt_protocol == 'mqtts'), | ||
reconnect_retries=-1) | ||
fmqtt = FastMQTT(config=mqtt_config) | ||
CV_LOCAL.set(fmqtt) | ||
|
||
hass_mqtt_config = MQTTConfig(host=config.hass_mqtt_host, | ||
port=config.hass_mqtt_port, | ||
ssl=(config.hass_mqtt_protocol == 'mqtts'), | ||
username=hass_credentials.mqtt_username, | ||
password=hass_credentials.mqtt_password, | ||
reconnect_retries=-1) | ||
hass_fmqtt = FastMQTT(config=hass_mqtt_config) | ||
CV_HASS.set(hass_fmqtt) | ||
|
||
|
||
@asynccontextmanager | ||
async def mqtt_lifespan(fmqtt: FastMQTT): | ||
await fmqtt.connection() | ||
yield | ||
await fmqtt.client.disconnect() | ||
|
||
|
||
@asynccontextmanager | ||
async def lifespan(): | ||
async with AsyncExitStack() as stack: | ||
# Order matters here: we want to be able to publish | ||
# before we start receiving messages | ||
await stack.enter_async_context(mqtt_lifespan(CV_HASS.get())) | ||
await stack.enter_async_context(mqtt_lifespan(CV_LOCAL.get())) | ||
yield |
Oops, something went wrong.