Add Minecraft Server Integration (#30992)
* Add Minecraft Server integration
* Add unit test for config flow
* Fixed some review findings and increased unit test coverage
* Fixed docstrings of new test cases
* Removed unnecessary debug log messages
* Added unique IDs and device infos and removed duplicate name validation
* Attempt to fix unit test on CI
* Return state OFF instead of UNAVAILABLE in case connection to server drops
* Added property decorator to server properties, even less debug messages, improved sensor dispatcher connection and other review findings fixed
* Moved special property handling to sensors, fixed name confusion in sensor entity, switch to HA const for scan_interval, simplified building players list string
* Improved periodic update, speeded up unit tests
* Added type hints, added callback decorator to entity update callback, added const.py to unit test exclusions
* Changed state sensor to binary sensor, removed empty unit test file, added constants for icons and units
* Let HA handle unknown state, check for None in description and players list sensor
* Removed periods at end of log messages, removed constant for default host
* Updated requirements_test_pre_commit.txt, fixed codespell findings
* Use localhost as default host
* Removed passing hass to entities, moved log message from init, moved host lower to vol, use proper patch library, patch library instead of own code
* Replaced server properties with global instance attributes, removed config option scan_interval, switch back to async_track_time_interval
* Removed description and players list sensors, added players list as state attributes to online players sensor, raise OSError instead of deprecated IOError, other minor review findings fixed
* Use MAC address for unique_id in case of an IP address as host, added getmac to manifest.json, added invalid_ip to strings.json, added new test cases for changes in config_flow, replace all IOError's with OSError, other review findings fixed
* Removed double assignment
* Call get_mac_address async safe
* Handle unavailable and unknown states to reach silver quality scale, added quality scale to manifest.json
2020-02-08 08:28:35 +00:00
|
|
|
"""The Minecraft Server sensor platform."""
|
2024-03-08 14:01:29 +00:00
|
|
|
|
2021-03-18 12:07:04 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-04-10 01:06:11 +00:00
|
|
|
from collections.abc import Callable
|
2023-09-10 08:20:26 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
Add Minecraft Server Integration (#30992)
* Add Minecraft Server integration
* Add unit test for config flow
* Fixed some review findings and increased unit test coverage
* Fixed docstrings of new test cases
* Removed unnecessary debug log messages
* Added unique IDs and device infos and removed duplicate name validation
* Attempt to fix unit test on CI
* Return state OFF instead of UNAVAILABLE in case connection to server drops
* Added property decorator to server properties, even less debug messages, improved sensor dispatcher connection and other review findings fixed
* Moved special property handling to sensors, fixed name confusion in sensor entity, switch to HA const for scan_interval, simplified building players list string
* Improved periodic update, speeded up unit tests
* Added type hints, added callback decorator to entity update callback, added const.py to unit test exclusions
* Changed state sensor to binary sensor, removed empty unit test file, added constants for icons and units
* Let HA handle unknown state, check for None in description and players list sensor
* Removed periods at end of log messages, removed constant for default host
* Updated requirements_test_pre_commit.txt, fixed codespell findings
* Use localhost as default host
* Removed passing hass to entities, moved log message from init, moved host lower to vol, use proper patch library, patch library instead of own code
* Replaced server properties with global instance attributes, removed config option scan_interval, switch back to async_track_time_interval
* Removed description and players list sensors, added players list as state attributes to online players sensor, raise OSError instead of deprecated IOError, other minor review findings fixed
* Use MAC address for unique_id in case of an IP address as host, added getmac to manifest.json, added invalid_ip to strings.json, added new test cases for changes in config_flow, replace all IOError's with OSError, other review findings fixed
* Removed double assignment
* Call get_mac_address async safe
* Handle unavailable and unknown states to reach silver quality scale, added quality scale to manifest.json
2020-02-08 08:28:35 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2023-10-13 20:09:13 +00:00
|
|
|
from homeassistant.const import CONF_TYPE, EntityCategory, UnitOfTime
|
2023-09-19 13:59:58 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2022-01-07 15:27:49 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-09-10 08:20:26 +00:00
|
|
|
from homeassistant.helpers.typing import StateType
|
Add Minecraft Server Integration (#30992)
* Add Minecraft Server integration
* Add unit test for config flow
* Fixed some review findings and increased unit test coverage
* Fixed docstrings of new test cases
* Removed unnecessary debug log messages
* Added unique IDs and device infos and removed duplicate name validation
* Attempt to fix unit test on CI
* Return state OFF instead of UNAVAILABLE in case connection to server drops
* Added property decorator to server properties, even less debug messages, improved sensor dispatcher connection and other review findings fixed
* Moved special property handling to sensors, fixed name confusion in sensor entity, switch to HA const for scan_interval, simplified building players list string
* Improved periodic update, speeded up unit tests
* Added type hints, added callback decorator to entity update callback, added const.py to unit test exclusions
* Changed state sensor to binary sensor, removed empty unit test file, added constants for icons and units
* Let HA handle unknown state, check for None in description and players list sensor
* Removed periods at end of log messages, removed constant for default host
* Updated requirements_test_pre_commit.txt, fixed codespell findings
* Use localhost as default host
* Removed passing hass to entities, moved log message from init, moved host lower to vol, use proper patch library, patch library instead of own code
* Replaced server properties with global instance attributes, removed config option scan_interval, switch back to async_track_time_interval
* Removed description and players list sensors, added players list as state attributes to online players sensor, raise OSError instead of deprecated IOError, other minor review findings fixed
* Use MAC address for unique_id in case of an IP address as host, added getmac to manifest.json, added invalid_ip to strings.json, added new test cases for changes in config_flow, replace all IOError's with OSError, other review findings fixed
* Removed double assignment
* Call get_mac_address async safe
* Handle unavailable and unknown states to reach silver quality scale, added quality scale to manifest.json
2020-02-08 08:28:35 +00:00
|
|
|
|
2023-10-10 06:42:35 +00:00
|
|
|
from .api import MinecraftServerData, MinecraftServerType
|
2023-09-21 06:40:07 +00:00
|
|
|
from .const import DOMAIN, KEY_LATENCY, KEY_MOTD
|
2023-10-10 06:42:35 +00:00
|
|
|
from .coordinator import MinecraftServerCoordinator
|
2023-07-25 08:14:01 +00:00
|
|
|
from .entity import MinecraftServerEntity
|
Add Minecraft Server Integration (#30992)
* Add Minecraft Server integration
* Add unit test for config flow
* Fixed some review findings and increased unit test coverage
* Fixed docstrings of new test cases
* Removed unnecessary debug log messages
* Added unique IDs and device infos and removed duplicate name validation
* Attempt to fix unit test on CI
* Return state OFF instead of UNAVAILABLE in case connection to server drops
* Added property decorator to server properties, even less debug messages, improved sensor dispatcher connection and other review findings fixed
* Moved special property handling to sensors, fixed name confusion in sensor entity, switch to HA const for scan_interval, simplified building players list string
* Improved periodic update, speeded up unit tests
* Added type hints, added callback decorator to entity update callback, added const.py to unit test exclusions
* Changed state sensor to binary sensor, removed empty unit test file, added constants for icons and units
* Let HA handle unknown state, check for None in description and players list sensor
* Removed periods at end of log messages, removed constant for default host
* Updated requirements_test_pre_commit.txt, fixed codespell findings
* Use localhost as default host
* Removed passing hass to entities, moved log message from init, moved host lower to vol, use proper patch library, patch library instead of own code
* Replaced server properties with global instance attributes, removed config option scan_interval, switch back to async_track_time_interval
* Removed description and players list sensors, added players list as state attributes to online players sensor, raise OSError instead of deprecated IOError, other minor review findings fixed
* Use MAC address for unique_id in case of an IP address as host, added getmac to manifest.json, added invalid_ip to strings.json, added new test cases for changes in config_flow, replace all IOError's with OSError, other review findings fixed
* Removed double assignment
* Call get_mac_address async safe
* Handle unavailable and unknown states to reach silver quality scale, added quality scale to manifest.json
2020-02-08 08:28:35 +00:00
|
|
|
|
2023-09-21 06:40:07 +00:00
|
|
|
ATTR_PLAYERS_LIST = "players_list"
|
|
|
|
|
2023-10-10 06:42:35 +00:00
|
|
|
KEY_EDITION = "edition"
|
|
|
|
KEY_GAME_MODE = "game_mode"
|
|
|
|
KEY_MAP_NAME = "map_name"
|
2023-09-21 06:40:07 +00:00
|
|
|
KEY_PLAYERS_MAX = "players_max"
|
|
|
|
KEY_PLAYERS_ONLINE = "players_online"
|
|
|
|
KEY_PROTOCOL_VERSION = "protocol_version"
|
|
|
|
KEY_VERSION = "version"
|
|
|
|
|
|
|
|
UNIT_PLAYERS_MAX = "players"
|
|
|
|
UNIT_PLAYERS_ONLINE = "players"
|
|
|
|
|
Add Minecraft Server Integration (#30992)
* Add Minecraft Server integration
* Add unit test for config flow
* Fixed some review findings and increased unit test coverage
* Fixed docstrings of new test cases
* Removed unnecessary debug log messages
* Added unique IDs and device infos and removed duplicate name validation
* Attempt to fix unit test on CI
* Return state OFF instead of UNAVAILABLE in case connection to server drops
* Added property decorator to server properties, even less debug messages, improved sensor dispatcher connection and other review findings fixed
* Moved special property handling to sensors, fixed name confusion in sensor entity, switch to HA const for scan_interval, simplified building players list string
* Improved periodic update, speeded up unit tests
* Added type hints, added callback decorator to entity update callback, added const.py to unit test exclusions
* Changed state sensor to binary sensor, removed empty unit test file, added constants for icons and units
* Let HA handle unknown state, check for None in description and players list sensor
* Removed periods at end of log messages, removed constant for default host
* Updated requirements_test_pre_commit.txt, fixed codespell findings
* Use localhost as default host
* Removed passing hass to entities, moved log message from init, moved host lower to vol, use proper patch library, patch library instead of own code
* Replaced server properties with global instance attributes, removed config option scan_interval, switch back to async_track_time_interval
* Removed description and players list sensors, added players list as state attributes to online players sensor, raise OSError instead of deprecated IOError, other minor review findings fixed
* Use MAC address for unique_id in case of an IP address as host, added getmac to manifest.json, added invalid_ip to strings.json, added new test cases for changes in config_flow, replace all IOError's with OSError, other review findings fixed
* Removed double assignment
* Call get_mac_address async safe
* Handle unavailable and unknown states to reach silver quality scale, added quality scale to manifest.json
2020-02-08 08:28:35 +00:00
|
|
|
|
2024-03-11 10:29:30 +00:00
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
|
|
class MinecraftServerSensorEntityDescription(SensorEntityDescription):
|
|
|
|
"""Class describing Minecraft Server sensor entities."""
|
2023-09-10 08:20:26 +00:00
|
|
|
|
|
|
|
value_fn: Callable[[MinecraftServerData], StateType]
|
2024-04-10 01:06:11 +00:00
|
|
|
attributes_fn: Callable[[MinecraftServerData], dict[str, Any]] | None
|
2023-10-13 20:09:13 +00:00
|
|
|
supported_server_types: set[MinecraftServerType]
|
2023-09-10 08:20:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_extra_state_attributes_players_list(
|
|
|
|
data: MinecraftServerData,
|
|
|
|
) -> dict[str, list[str]]:
|
|
|
|
"""Return players list as extra state attributes, if available."""
|
2024-01-05 17:38:31 +00:00
|
|
|
extra_state_attributes: dict[str, Any] = {}
|
2023-09-10 08:20:26 +00:00
|
|
|
players_list = data.players_list
|
|
|
|
|
|
|
|
if players_list is not None and len(players_list) != 0:
|
|
|
|
extra_state_attributes[ATTR_PLAYERS_LIST] = players_list
|
|
|
|
|
|
|
|
return extra_state_attributes
|
|
|
|
|
|
|
|
|
|
|
|
SENSOR_DESCRIPTIONS = [
|
|
|
|
MinecraftServerSensorEntityDescription(
|
|
|
|
key=KEY_VERSION,
|
|
|
|
translation_key=KEY_VERSION,
|
|
|
|
value_fn=lambda data: data.version,
|
|
|
|
attributes_fn=None,
|
2023-10-13 20:09:13 +00:00
|
|
|
supported_server_types={
|
2023-10-10 06:42:35 +00:00
|
|
|
MinecraftServerType.JAVA_EDITION,
|
|
|
|
MinecraftServerType.BEDROCK_EDITION,
|
2023-10-13 20:09:13 +00:00
|
|
|
},
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2023-09-10 08:20:26 +00:00
|
|
|
),
|
|
|
|
MinecraftServerSensorEntityDescription(
|
|
|
|
key=KEY_PROTOCOL_VERSION,
|
|
|
|
translation_key=KEY_PROTOCOL_VERSION,
|
|
|
|
value_fn=lambda data: data.protocol_version,
|
|
|
|
attributes_fn=None,
|
2023-10-13 20:09:13 +00:00
|
|
|
supported_server_types={
|
2023-10-10 06:42:35 +00:00
|
|
|
MinecraftServerType.JAVA_EDITION,
|
|
|
|
MinecraftServerType.BEDROCK_EDITION,
|
2023-10-13 20:09:13 +00:00
|
|
|
},
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
entity_registry_enabled_default=False,
|
2023-09-10 08:20:26 +00:00
|
|
|
),
|
|
|
|
MinecraftServerSensorEntityDescription(
|
|
|
|
key=KEY_PLAYERS_MAX,
|
|
|
|
translation_key=KEY_PLAYERS_MAX,
|
|
|
|
native_unit_of_measurement=UNIT_PLAYERS_MAX,
|
|
|
|
value_fn=lambda data: data.players_max,
|
|
|
|
attributes_fn=None,
|
2023-10-13 20:09:13 +00:00
|
|
|
supported_server_types={
|
2023-10-10 06:42:35 +00:00
|
|
|
MinecraftServerType.JAVA_EDITION,
|
|
|
|
MinecraftServerType.BEDROCK_EDITION,
|
2023-10-13 20:09:13 +00:00
|
|
|
},
|
|
|
|
entity_registry_enabled_default=False,
|
2023-09-10 08:20:26 +00:00
|
|
|
),
|
|
|
|
MinecraftServerSensorEntityDescription(
|
|
|
|
key=KEY_LATENCY,
|
|
|
|
translation_key=KEY_LATENCY,
|
|
|
|
native_unit_of_measurement=UnitOfTime.MILLISECONDS,
|
|
|
|
suggested_display_precision=0,
|
|
|
|
value_fn=lambda data: data.latency,
|
|
|
|
attributes_fn=None,
|
2023-10-13 20:09:13 +00:00
|
|
|
supported_server_types={
|
2023-10-10 06:42:35 +00:00
|
|
|
MinecraftServerType.JAVA_EDITION,
|
|
|
|
MinecraftServerType.BEDROCK_EDITION,
|
2023-10-13 20:09:13 +00:00
|
|
|
},
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
2023-09-10 08:20:26 +00:00
|
|
|
),
|
|
|
|
MinecraftServerSensorEntityDescription(
|
|
|
|
key=KEY_MOTD,
|
|
|
|
translation_key=KEY_MOTD,
|
|
|
|
value_fn=lambda data: data.motd,
|
|
|
|
attributes_fn=None,
|
2023-10-13 20:09:13 +00:00
|
|
|
supported_server_types={
|
2023-10-10 06:42:35 +00:00
|
|
|
MinecraftServerType.JAVA_EDITION,
|
|
|
|
MinecraftServerType.BEDROCK_EDITION,
|
2023-10-13 20:09:13 +00:00
|
|
|
},
|
2023-09-10 08:20:26 +00:00
|
|
|
),
|
|
|
|
MinecraftServerSensorEntityDescription(
|
|
|
|
key=KEY_PLAYERS_ONLINE,
|
|
|
|
translation_key=KEY_PLAYERS_ONLINE,
|
|
|
|
native_unit_of_measurement=UNIT_PLAYERS_ONLINE,
|
|
|
|
value_fn=lambda data: data.players_online,
|
|
|
|
attributes_fn=get_extra_state_attributes_players_list,
|
2023-10-13 20:09:13 +00:00
|
|
|
supported_server_types={
|
2023-10-10 06:42:35 +00:00
|
|
|
MinecraftServerType.JAVA_EDITION,
|
|
|
|
MinecraftServerType.BEDROCK_EDITION,
|
2023-10-13 20:09:13 +00:00
|
|
|
},
|
2023-10-10 06:42:35 +00:00
|
|
|
),
|
|
|
|
MinecraftServerSensorEntityDescription(
|
|
|
|
key=KEY_EDITION,
|
|
|
|
translation_key=KEY_EDITION,
|
|
|
|
value_fn=lambda data: data.edition,
|
|
|
|
attributes_fn=None,
|
2023-10-13 20:09:13 +00:00
|
|
|
supported_server_types={
|
2023-10-10 06:42:35 +00:00
|
|
|
MinecraftServerType.BEDROCK_EDITION,
|
2023-10-13 20:09:13 +00:00
|
|
|
},
|
|
|
|
entity_category=EntityCategory.DIAGNOSTIC,
|
|
|
|
entity_registry_enabled_default=False,
|
2023-10-10 06:42:35 +00:00
|
|
|
),
|
|
|
|
MinecraftServerSensorEntityDescription(
|
|
|
|
key=KEY_GAME_MODE,
|
|
|
|
translation_key=KEY_GAME_MODE,
|
|
|
|
value_fn=lambda data: data.game_mode,
|
|
|
|
attributes_fn=None,
|
2023-10-13 20:09:13 +00:00
|
|
|
supported_server_types={
|
2023-10-10 06:42:35 +00:00
|
|
|
MinecraftServerType.BEDROCK_EDITION,
|
2023-10-13 20:09:13 +00:00
|
|
|
},
|
2023-10-10 06:42:35 +00:00
|
|
|
),
|
|
|
|
MinecraftServerSensorEntityDescription(
|
|
|
|
key=KEY_MAP_NAME,
|
|
|
|
translation_key=KEY_MAP_NAME,
|
|
|
|
value_fn=lambda data: data.map_name,
|
|
|
|
attributes_fn=None,
|
2023-10-13 20:09:13 +00:00
|
|
|
supported_server_types={
|
2023-10-10 06:42:35 +00:00
|
|
|
MinecraftServerType.BEDROCK_EDITION,
|
2023-10-13 20:09:13 +00:00
|
|
|
},
|
2023-09-10 08:20:26 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
Add Minecraft Server Integration (#30992)
* Add Minecraft Server integration
* Add unit test for config flow
* Fixed some review findings and increased unit test coverage
* Fixed docstrings of new test cases
* Removed unnecessary debug log messages
* Added unique IDs and device infos and removed duplicate name validation
* Attempt to fix unit test on CI
* Return state OFF instead of UNAVAILABLE in case connection to server drops
* Added property decorator to server properties, even less debug messages, improved sensor dispatcher connection and other review findings fixed
* Moved special property handling to sensors, fixed name confusion in sensor entity, switch to HA const for scan_interval, simplified building players list string
* Improved periodic update, speeded up unit tests
* Added type hints, added callback decorator to entity update callback, added const.py to unit test exclusions
* Changed state sensor to binary sensor, removed empty unit test file, added constants for icons and units
* Let HA handle unknown state, check for None in description and players list sensor
* Removed periods at end of log messages, removed constant for default host
* Updated requirements_test_pre_commit.txt, fixed codespell findings
* Use localhost as default host
* Removed passing hass to entities, moved log message from init, moved host lower to vol, use proper patch library, patch library instead of own code
* Replaced server properties with global instance attributes, removed config option scan_interval, switch back to async_track_time_interval
* Removed description and players list sensors, added players list as state attributes to online players sensor, raise OSError instead of deprecated IOError, other minor review findings fixed
* Use MAC address for unique_id in case of an IP address as host, added getmac to manifest.json, added invalid_ip to strings.json, added new test cases for changes in config_flow, replace all IOError's with OSError, other review findings fixed
* Removed double assignment
* Call get_mac_address async safe
* Handle unavailable and unknown states to reach silver quality scale, added quality scale to manifest.json
2020-02-08 08:28:35 +00:00
|
|
|
async def async_setup_entry(
|
2022-01-07 15:27:49 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
Add Minecraft Server Integration (#30992)
* Add Minecraft Server integration
* Add unit test for config flow
* Fixed some review findings and increased unit test coverage
* Fixed docstrings of new test cases
* Removed unnecessary debug log messages
* Added unique IDs and device infos and removed duplicate name validation
* Attempt to fix unit test on CI
* Return state OFF instead of UNAVAILABLE in case connection to server drops
* Added property decorator to server properties, even less debug messages, improved sensor dispatcher connection and other review findings fixed
* Moved special property handling to sensors, fixed name confusion in sensor entity, switch to HA const for scan_interval, simplified building players list string
* Improved periodic update, speeded up unit tests
* Added type hints, added callback decorator to entity update callback, added const.py to unit test exclusions
* Changed state sensor to binary sensor, removed empty unit test file, added constants for icons and units
* Let HA handle unknown state, check for None in description and players list sensor
* Removed periods at end of log messages, removed constant for default host
* Updated requirements_test_pre_commit.txt, fixed codespell findings
* Use localhost as default host
* Removed passing hass to entities, moved log message from init, moved host lower to vol, use proper patch library, patch library instead of own code
* Replaced server properties with global instance attributes, removed config option scan_interval, switch back to async_track_time_interval
* Removed description and players list sensors, added players list as state attributes to online players sensor, raise OSError instead of deprecated IOError, other minor review findings fixed
* Use MAC address for unique_id in case of an IP address as host, added getmac to manifest.json, added invalid_ip to strings.json, added new test cases for changes in config_flow, replace all IOError's with OSError, other review findings fixed
* Removed double assignment
* Call get_mac_address async safe
* Handle unavailable and unknown states to reach silver quality scale, added quality scale to manifest.json
2020-02-08 08:28:35 +00:00
|
|
|
) -> None:
|
|
|
|
"""Set up the Minecraft Server sensor platform."""
|
2023-09-19 13:59:58 +00:00
|
|
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
Add Minecraft Server Integration (#30992)
* Add Minecraft Server integration
* Add unit test for config flow
* Fixed some review findings and increased unit test coverage
* Fixed docstrings of new test cases
* Removed unnecessary debug log messages
* Added unique IDs and device infos and removed duplicate name validation
* Attempt to fix unit test on CI
* Return state OFF instead of UNAVAILABLE in case connection to server drops
* Added property decorator to server properties, even less debug messages, improved sensor dispatcher connection and other review findings fixed
* Moved special property handling to sensors, fixed name confusion in sensor entity, switch to HA const for scan_interval, simplified building players list string
* Improved periodic update, speeded up unit tests
* Added type hints, added callback decorator to entity update callback, added const.py to unit test exclusions
* Changed state sensor to binary sensor, removed empty unit test file, added constants for icons and units
* Let HA handle unknown state, check for None in description and players list sensor
* Removed periods at end of log messages, removed constant for default host
* Updated requirements_test_pre_commit.txt, fixed codespell findings
* Use localhost as default host
* Removed passing hass to entities, moved log message from init, moved host lower to vol, use proper patch library, patch library instead of own code
* Replaced server properties with global instance attributes, removed config option scan_interval, switch back to async_track_time_interval
* Removed description and players list sensors, added players list as state attributes to online players sensor, raise OSError instead of deprecated IOError, other minor review findings fixed
* Use MAC address for unique_id in case of an IP address as host, added getmac to manifest.json, added invalid_ip to strings.json, added new test cases for changes in config_flow, replace all IOError's with OSError, other review findings fixed
* Removed double assignment
* Call get_mac_address async safe
* Handle unavailable and unknown states to reach silver quality scale, added quality scale to manifest.json
2020-02-08 08:28:35 +00:00
|
|
|
|
|
|
|
# Add sensor entities.
|
2023-09-10 08:20:26 +00:00
|
|
|
async_add_entities(
|
|
|
|
[
|
2023-10-10 06:42:35 +00:00
|
|
|
MinecraftServerSensorEntity(coordinator, description, config_entry)
|
2023-09-10 08:20:26 +00:00
|
|
|
for description in SENSOR_DESCRIPTIONS
|
2023-10-10 06:42:35 +00:00
|
|
|
if config_entry.data.get(CONF_TYPE, MinecraftServerType.JAVA_EDITION)
|
|
|
|
in description.supported_server_types
|
2023-09-19 13:59:58 +00:00
|
|
|
]
|
2023-09-10 08:20:26 +00:00
|
|
|
)
|
Add Minecraft Server Integration (#30992)
* Add Minecraft Server integration
* Add unit test for config flow
* Fixed some review findings and increased unit test coverage
* Fixed docstrings of new test cases
* Removed unnecessary debug log messages
* Added unique IDs and device infos and removed duplicate name validation
* Attempt to fix unit test on CI
* Return state OFF instead of UNAVAILABLE in case connection to server drops
* Added property decorator to server properties, even less debug messages, improved sensor dispatcher connection and other review findings fixed
* Moved special property handling to sensors, fixed name confusion in sensor entity, switch to HA const for scan_interval, simplified building players list string
* Improved periodic update, speeded up unit tests
* Added type hints, added callback decorator to entity update callback, added const.py to unit test exclusions
* Changed state sensor to binary sensor, removed empty unit test file, added constants for icons and units
* Let HA handle unknown state, check for None in description and players list sensor
* Removed periods at end of log messages, removed constant for default host
* Updated requirements_test_pre_commit.txt, fixed codespell findings
* Use localhost as default host
* Removed passing hass to entities, moved log message from init, moved host lower to vol, use proper patch library, patch library instead of own code
* Replaced server properties with global instance attributes, removed config option scan_interval, switch back to async_track_time_interval
* Removed description and players list sensors, added players list as state attributes to online players sensor, raise OSError instead of deprecated IOError, other minor review findings fixed
* Use MAC address for unique_id in case of an IP address as host, added getmac to manifest.json, added invalid_ip to strings.json, added new test cases for changes in config_flow, replace all IOError's with OSError, other review findings fixed
* Removed double assignment
* Call get_mac_address async safe
* Handle unavailable and unknown states to reach silver quality scale, added quality scale to manifest.json
2020-02-08 08:28:35 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:59:03 +00:00
|
|
|
class MinecraftServerSensorEntity(MinecraftServerEntity, SensorEntity):
|
Add Minecraft Server Integration (#30992)
* Add Minecraft Server integration
* Add unit test for config flow
* Fixed some review findings and increased unit test coverage
* Fixed docstrings of new test cases
* Removed unnecessary debug log messages
* Added unique IDs and device infos and removed duplicate name validation
* Attempt to fix unit test on CI
* Return state OFF instead of UNAVAILABLE in case connection to server drops
* Added property decorator to server properties, even less debug messages, improved sensor dispatcher connection and other review findings fixed
* Moved special property handling to sensors, fixed name confusion in sensor entity, switch to HA const for scan_interval, simplified building players list string
* Improved periodic update, speeded up unit tests
* Added type hints, added callback decorator to entity update callback, added const.py to unit test exclusions
* Changed state sensor to binary sensor, removed empty unit test file, added constants for icons and units
* Let HA handle unknown state, check for None in description and players list sensor
* Removed periods at end of log messages, removed constant for default host
* Updated requirements_test_pre_commit.txt, fixed codespell findings
* Use localhost as default host
* Removed passing hass to entities, moved log message from init, moved host lower to vol, use proper patch library, patch library instead of own code
* Replaced server properties with global instance attributes, removed config option scan_interval, switch back to async_track_time_interval
* Removed description and players list sensors, added players list as state attributes to online players sensor, raise OSError instead of deprecated IOError, other minor review findings fixed
* Use MAC address for unique_id in case of an IP address as host, added getmac to manifest.json, added invalid_ip to strings.json, added new test cases for changes in config_flow, replace all IOError's with OSError, other review findings fixed
* Removed double assignment
* Call get_mac_address async safe
* Handle unavailable and unknown states to reach silver quality scale, added quality scale to manifest.json
2020-02-08 08:28:35 +00:00
|
|
|
"""Representation of a Minecraft Server sensor base entity."""
|
|
|
|
|
2023-09-10 08:20:26 +00:00
|
|
|
entity_description: MinecraftServerSensorEntityDescription
|
|
|
|
|
Add Minecraft Server Integration (#30992)
* Add Minecraft Server integration
* Add unit test for config flow
* Fixed some review findings and increased unit test coverage
* Fixed docstrings of new test cases
* Removed unnecessary debug log messages
* Added unique IDs and device infos and removed duplicate name validation
* Attempt to fix unit test on CI
* Return state OFF instead of UNAVAILABLE in case connection to server drops
* Added property decorator to server properties, even less debug messages, improved sensor dispatcher connection and other review findings fixed
* Moved special property handling to sensors, fixed name confusion in sensor entity, switch to HA const for scan_interval, simplified building players list string
* Improved periodic update, speeded up unit tests
* Added type hints, added callback decorator to entity update callback, added const.py to unit test exclusions
* Changed state sensor to binary sensor, removed empty unit test file, added constants for icons and units
* Let HA handle unknown state, check for None in description and players list sensor
* Removed periods at end of log messages, removed constant for default host
* Updated requirements_test_pre_commit.txt, fixed codespell findings
* Use localhost as default host
* Removed passing hass to entities, moved log message from init, moved host lower to vol, use proper patch library, patch library instead of own code
* Replaced server properties with global instance attributes, removed config option scan_interval, switch back to async_track_time_interval
* Removed description and players list sensors, added players list as state attributes to online players sensor, raise OSError instead of deprecated IOError, other minor review findings fixed
* Use MAC address for unique_id in case of an IP address as host, added getmac to manifest.json, added invalid_ip to strings.json, added new test cases for changes in config_flow, replace all IOError's with OSError, other review findings fixed
* Removed double assignment
* Call get_mac_address async safe
* Handle unavailable and unknown states to reach silver quality scale, added quality scale to manifest.json
2020-02-08 08:28:35 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2023-09-19 13:59:58 +00:00
|
|
|
coordinator: MinecraftServerCoordinator,
|
2023-09-10 08:20:26 +00:00
|
|
|
description: MinecraftServerSensorEntityDescription,
|
2023-10-10 06:42:35 +00:00
|
|
|
config_entry: ConfigEntry,
|
Add Minecraft Server Integration (#30992)
* Add Minecraft Server integration
* Add unit test for config flow
* Fixed some review findings and increased unit test coverage
* Fixed docstrings of new test cases
* Removed unnecessary debug log messages
* Added unique IDs and device infos and removed duplicate name validation
* Attempt to fix unit test on CI
* Return state OFF instead of UNAVAILABLE in case connection to server drops
* Added property decorator to server properties, even less debug messages, improved sensor dispatcher connection and other review findings fixed
* Moved special property handling to sensors, fixed name confusion in sensor entity, switch to HA const for scan_interval, simplified building players list string
* Improved periodic update, speeded up unit tests
* Added type hints, added callback decorator to entity update callback, added const.py to unit test exclusions
* Changed state sensor to binary sensor, removed empty unit test file, added constants for icons and units
* Let HA handle unknown state, check for None in description and players list sensor
* Removed periods at end of log messages, removed constant for default host
* Updated requirements_test_pre_commit.txt, fixed codespell findings
* Use localhost as default host
* Removed passing hass to entities, moved log message from init, moved host lower to vol, use proper patch library, patch library instead of own code
* Replaced server properties with global instance attributes, removed config option scan_interval, switch back to async_track_time_interval
* Removed description and players list sensors, added players list as state attributes to online players sensor, raise OSError instead of deprecated IOError, other minor review findings fixed
* Use MAC address for unique_id in case of an IP address as host, added getmac to manifest.json, added invalid_ip to strings.json, added new test cases for changes in config_flow, replace all IOError's with OSError, other review findings fixed
* Removed double assignment
* Call get_mac_address async safe
* Handle unavailable and unknown states to reach silver quality scale, added quality scale to manifest.json
2020-02-08 08:28:35 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize sensor base entity."""
|
2023-10-10 06:42:35 +00:00
|
|
|
super().__init__(coordinator, config_entry)
|
2023-09-10 08:20:26 +00:00
|
|
|
self.entity_description = description
|
2023-10-10 06:42:35 +00:00
|
|
|
self._attr_unique_id = f"{config_entry.entry_id}-{description.key}"
|
2023-09-19 13:59:58 +00:00
|
|
|
self._update_properties()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self) -> None:
|
|
|
|
"""Handle updated data from the coordinator."""
|
|
|
|
self._update_properties()
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _update_properties(self) -> None:
|
|
|
|
"""Update sensor properties."""
|
|
|
|
self._attr_native_value = self.entity_description.value_fn(
|
|
|
|
self.coordinator.data
|
|
|
|
)
|
|
|
|
|
|
|
|
if func := self.entity_description.attributes_fn:
|
|
|
|
self._attr_extra_state_attributes = func(self.coordinator.data)
|