Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump pydantic version to ^2.0.0 #191

Merged
merged 2 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,6 @@ dmypy.json

# Mega-linter reports dir
megalinter-reports

# Only used for the build system
requirements.txt
11 changes: 5 additions & 6 deletions ha_mqtt_discoverable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

import paho.mqtt.client as mqtt
from paho.mqtt.client import MQTTMessageInfo
from pydantic import BaseModel, root_validator
from pydantic.generics import GenericModel
from pydantic import BaseModel, model_validator

# Read version from the package metadata
__version__ = metadata.version(__package__)
Expand Down Expand Up @@ -490,7 +489,7 @@ class DeviceInfo(BaseModel):
Assistant. Examples of such devices are hubs, or parent devices of a sub-device.
This is used to show device topology in Home Assistant."""

@root_validator
@model_validator(mode="before")
def must_have_identifiers_or_connection(cls, values):
"""Check that either `identifiers` or `connections` is set"""
identifiers, connections = values.get("identifiers"), values.get("connections")
Expand Down Expand Up @@ -530,7 +529,7 @@ class EntityInfo(BaseModel):
"""Set this to enable editing sensor from the HA ui and to integrate with a
device"""

@root_validator
@model_validator(mode="before")
def device_need_unique_id(cls, values):
"""Check that `unique_id` is set if `device` is provided,\
otherwise Home Assistant will not link the sensor to the device"""
Expand All @@ -543,7 +542,7 @@ def device_need_unique_id(cls, values):
EntityType = TypeVar("EntityType", bound=EntityInfo)


class Settings(GenericModel, Generic[EntityType]):
class Settings(BaseModel, Generic[EntityType]):
class MQTT(BaseModel):
"""Connection settings for the MQTT broker"""

Expand Down Expand Up @@ -793,7 +792,7 @@ def generate_config(self) -> dict[str, Any]:
automagically ingest the new sensor.
"""
# Automatically generate a dict using pydantic
config = self._entity.dict(exclude_none=True, by_alias=True)
config = self._entity.model_dump(exclude_none=True, by_alias=True)
# Add the MQTT topics to be discovered by HA
topics = {
"state_topic": self.state_topic,
Expand Down
Loading