Skip to content

Commit

Permalink
Merge pull request #191 from mmaeusezahl/pydantic_2
Browse files Browse the repository at this point in the history
Bump pydantic version to ^2.0.0
  • Loading branch information
unixorn authored May 19, 2024
2 parents 0cb8015 + c3952a6 commit a3b20d5
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 103 deletions.
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

0 comments on commit a3b20d5

Please sign in to comment.